summaryrefslogtreecommitdiff
path: root/ale_linters/typescript/tslint.vim
blob: 4db5094f629a43b9f658dfd1e6c545b3a2e7287d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
" Author: Prashanth Chandra https://github.com/prashcr
" Description: tslint for TypeScript files

call ale#Set('typescript_tslint_executable', 'tslint')
call ale#Set('typescript_tslint_config_path', '')
call ale#Set('typescript_tslint_use_global', 0)

function! ale_linters#typescript#tslint#GetExecutable(buffer) abort
    return ale#node#FindExecutable(a:buffer, 'typescript_tslint', [
    \   'node_modules/.bin/tslint',
    \])
endfunction

function! ale_linters#typescript#tslint#Handle(buffer, lines) abort
    let l:output = []

    for l:error in ale#util#FuzzyJSONDecode(a:lines, [])
        if ale#path#IsBufferPath(a:buffer, l:error.name)
            call add(l:output, {
            \   'type': (get(l:error, 'ruleSeverity', '') is# 'WARNING' ? 'W' : 'E'),
            \   'text': has_key(l:error, 'ruleName')
            \       ? l:error.ruleName . ': ' . l:error.failure
            \       : l:error.failure,
            \   'lnum': l:error.startPosition.line + 1,
            \   'col': l:error.startPosition.character + 1,
            \   'end_lnum': l:error.endPosition.line + 1,
            \   'end_col': l:error.endPosition.character + 1,
            \})
        endif
    endfor

    return l:output
endfunction

function! ale_linters#typescript#tslint#BuildLintCommand(buffer) abort
    let l:tslint_config_path = ale#path#ResolveLocalPath(
    \   a:buffer,
    \   'tslint.json',
    \   ale#Var(a:buffer, 'typescript_tslint_config_path')
    \)

    let l:tslint_config_option = !empty(l:tslint_config_path)
    \   ? ' -c ' . ale#Escape(l:tslint_config_path)
    \   : ''

    return ale_linters#typescript#tslint#GetExecutable(a:buffer)
    \   . ' --format json'
    \   . l:tslint_config_option
    \   . ' %t'
endfunction

call ale#linter#Define('typescript', {
\   'name': 'tslint',
\   'executable_callback': 'ale_linters#typescript#tslint#GetExecutable',
\   'command_callback': 'ale_linters#typescript#tslint#BuildLintCommand',
\   'callback': 'ale_linters#typescript#tslint#Handle',
\})