Fix #684 - Use the JSON format for tslint, for consistency betwen versions, and handling of end line and column numbers

This commit is contained in:
w0rp 2017-06-25 13:56:51 +01:00
parent 8da5641355
commit 4eaa990fe8
4 changed files with 123 additions and 51 deletions

View File

@ -12,27 +12,19 @@ function! ale_linters#typescript#tslint#GetExecutable(buffer) abort
endfunction endfunction
function! ale_linters#typescript#tslint#Handle(buffer, lines) abort function! ale_linters#typescript#tslint#Handle(buffer, lines) abort
" Matches patterns like the following:
"
" WARNING: hello.ts[113, 6]: Unnecessary semicolon
" ERROR: hello.ts[133, 10]: Missing semicolon
let l:ext = '.' . fnamemodify(bufname(a:buffer), ':e')
let l:pattern = '\<\(WARNING\|ERROR\)\>: .\+' . l:ext . '\[\(\d\+\), \(\d\+\)\]: \(.\+\)'
let l:output = [] let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern) for l:error in json_decode(join(a:lines, ''))
let l:type = l:match[1] if ale#path#IsBufferPath(a:buffer, l:error.name)
let l:line = l:match[2] + 0 call add(l:output, {
let l:column = l:match[3] + 0 \ 'type': (l:error.ruleSeverity ==# 'WARNING' ? 'W' : 'E'),
let l:text = l:match[4] \ 'text': l:error.failure,
\ 'lnum': l:error.startPosition.line + 1,
call add(l:output, { \ 'col': l:error.startPosition.position + 1,
\ 'type': (l:type ==# 'WARNING' ? 'W' : 'E'), \ 'end_lnum': l:error.endPosition.line + 1,
\ 'lnum': l:line, \ 'end_col': l:error.endPosition.position + 1,
\ 'col': l:column, \})
\ 'text': l:text, endif
\})
endfor endfor
return l:output return l:output
@ -46,11 +38,12 @@ function! ale_linters#typescript#tslint#BuildLintCommand(buffer) abort
\) \)
let l:tslint_config_option = !empty(l:tslint_config_path) let l:tslint_config_option = !empty(l:tslint_config_path)
\ ? '-c ' . ale#Escape(l:tslint_config_path) \ ? ' -c ' . ale#Escape(l:tslint_config_path)
\ : '' \ : ''
return ale_linters#typescript#tslint#GetExecutable(a:buffer) return ale_linters#typescript#tslint#GetExecutable(a:buffer)
\ . ' ' . l:tslint_config_option \ . ' --format json'
\ . l:tslint_config_option
\ . ' %t' \ . ' %t'
endfunction endfunction

View File

@ -78,6 +78,16 @@ function! ale#path#IsBufferPath(buffer, complex_filename) abort
let l:test_filename = l:test_filename[2:] let l:test_filename = l:test_filename[2:]
endif endif
if l:test_filename[:1] ==# '..'
" Remove ../../ etc. from the front of the path.
let l:test_filename = substitute(l:test_filename, '\v^(\.\.[/\\])+', '/', '')
endif
" Use the basename for files in /tmp, as they are likely our files.
if l:test_filename[:len($TMPDIR) - 1] ==# $TMPDIR
let l:test_filename = fnamemodify(l:test_filename, ':t')
endif
let l:buffer_filename = expand('#' . a:buffer . ':p') let l:buffer_filename = expand('#' . a:buffer . ':p')
return l:buffer_filename ==# l:test_filename return l:buffer_filename ==# l:test_filename

View File

@ -1,41 +1,99 @@
Before: Before:
runtime ale_linters/typescript/tslint.vim runtime ale_linters/typescript/tslint.vim
silent! cd /testplugin/test/handler
let g:dir = getcwd()
After:
silent execute 'cd ' . fnameescape(g:dir)
unlet! g:dir
call ale#linter#Reset()
Execute(The tslint handler should parse lines correctly): Execute(The tslint handler should parse lines correctly):
call ale#test#SetFilename('app/test.ts')
AssertEqual AssertEqual
\ [ \ [
\ { \ {
\ 'lnum': 235, \ 'lnum': 1,
\ 'col': 21, \ 'col': 15,
\ 'text': 'unused expression, expected an assignment or function call', \ 'end_lnum': 1,
\ 'type': 'W', \ 'end_col': 15,
\ },
\ {
\ 'lnum': 35,
\ 'col': 6,
\ 'text': 'Missing semicolon', \ 'text': 'Missing semicolon',
\ 'type': 'E', \ 'type': 'E',
\ }, \ },
\ { \ {
\ 'lnum': 147, \ 'lnum': 2,
\ 'col': 10, \ 'col': 15,
\ 'text': 'Unnecessary semicolon', \ 'end_lnum': 3,
\ 'end_col': 23,
\ 'text': 'Something else',
\ 'type': 'W', \ 'type': 'W',
\ }, \ },
\ {
\ 'lnum': 101,
\ 'col': 25,
\ 'text': 'Unnecessary trailing comma',
\ 'type': 'E',
\ },
\ ], \ ],
\ ale_linters#typescript#tslint#Handle(347, [ \ ale_linters#typescript#tslint#Handle(bufnr(''), [json_encode([
\ 'This line should be ignored completely', \ {
\ 'WARNING: hello.ts[235, 21]: unused expression, expected an assignment or function call', \ 'endPosition': {
\ 'ERROR: hello.ts[35, 6]: Missing semicolon', \ 'character': 14,
\ 'WARNING: hello.ts[147, 10]: Unnecessary semicolon', \ 'line': 0,
\ 'ERROR: hello.ts[101, 25]: Unnecessary trailing comma' \ 'position': 14
\ ]) \ },
\ 'failure': 'Missing semicolon',
After: \ 'fix': {
call ale#linter#Reset() \ 'innerLength': 0,
\ 'innerStart': 14,
\ 'innerText': ';'
\ },
\ 'name': 'app/test.ts',
\ 'ruleName': 'semicolon',
\ 'ruleSeverity': 'ERROR',
\ 'startPosition': {
\ 'character': 14,
\ 'line': 0,
\ 'position': 14
\ }
\ },
\ {
\ 'endPosition': {
\ 'character': 11,
\ 'line': 2,
\ 'position': 22
\ },
\ 'failure': 'Something else',
\ 'fix': {
\ 'innerLength': 0,
\ 'innerStart': 14,
\ 'innerText': ';'
\ },
\ 'name': 'app/test.ts',
\ 'ruleName': 'something',
\ 'ruleSeverity': 'WARNING',
\ 'startPosition': {
\ 'character': 7,
\ 'line': 1,
\ 'position': 14
\ }
\ },
\ {
\ 'endPosition': {
\ 'character': 11,
\ 'line': 2,
\ 'position': 22
\ },
\ 'failure': 'Something else',
\ 'fix': {
\ 'innerLength': 0,
\ 'innerStart': 14,
\ 'innerText': ';'
\ },
\ 'name': 'app/something-else.ts',
\ 'ruleName': 'something',
\ 'ruleSeverity': 'WARNING',
\ 'startPosition': {
\ 'character': 7,
\ 'line': 1,
\ 'position': 14
\ }
\ },
\])])

View File

@ -1,8 +1,13 @@
Execute(ale#path#IsBufferPath should match simple relative paths): Execute(ale#path#IsBufferPath should match simple relative paths):
silent file! foo.txt call ale#test#SetFilename('app/foo.txt')
Assert ale#path#IsBufferPath(bufnr(''), 'foo.txt'), 'No match for foo.txt' Assert ale#path#IsBufferPath(bufnr(''), 'app/foo.txt'), 'No match for foo.txt'
Assert !ale#path#IsBufferPath(bufnr(''), 'bar.txt'), 'Bad match for bar.txt' Assert !ale#path#IsBufferPath(bufnr(''), 'app/bar.txt'), 'Bad match for bar.txt'
Execute(ale#path#IsBufferPath should match relative paths with dots):
call ale#test#SetFilename('app/foo.txt')
Assert ale#path#IsBufferPath(bufnr(''), '../../app/foo.txt'), 'No match for ../../app/foo.txt'
Execute(ale#path#IsBufferPath should match absolute paths): Execute(ale#path#IsBufferPath should match absolute paths):
silent file! foo.txt silent file! foo.txt
@ -30,3 +35,9 @@ Execute(ale#path#IsBufferPath should accept various names for stdin):
Assert ale#path#IsBufferPath(bufnr(''), 'stdin') Assert ale#path#IsBufferPath(bufnr(''), 'stdin')
Assert ale#path#IsBufferPath(bufnr(''), '<stdin>') Assert ale#path#IsBufferPath(bufnr(''), '<stdin>')
Assert ale#path#IsBufferPath(bufnr(''), '<somethingelse>') Assert ale#path#IsBufferPath(bufnr(''), '<somethingelse>')
Execute(ale#path#IsBufferPath should match files in /tmp):
call ale#test#SetFilename('app/test.ts')
Assert ale#path#IsBufferPath(bufnr(''), '../../../../../../../../tmp/vG0hKyD/1/test.ts')
Assert ale#path#IsBufferPath(bufnr(''), '/tmp/vG0hKyD/1/test.ts')