#653 Update tslint to set the filename key for problems in other files

This commit is contained in:
w0rp 2017-08-20 00:05:15 +01:00
parent fb0adc602e
commit 7112776d1b
7 changed files with 96 additions and 34 deletions

View File

@ -12,11 +12,12 @@ 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
let l:dir = expand('#' . a:buffer . ':p:h')
let l:output = [] let l:output = []
for l:error in ale#util#FuzzyJSONDecode(a:lines, []) for l:error in ale#util#FuzzyJSONDecode(a:lines, [])
if ale#path#IsBufferPath(a:buffer, l:error.name)
call add(l:output, { call add(l:output, {
\ 'filename': ale#path#GetAbsPath(l:dir, l:error.name),
\ 'type': (get(l:error, 'ruleSeverity', '') is# 'WARNING' ? 'W' : 'E'), \ 'type': (get(l:error, 'ruleSeverity', '') is# 'WARNING' ? 'W' : 'E'),
\ 'text': has_key(l:error, 'ruleName') \ 'text': has_key(l:error, 'ruleName')
\ ? l:error.ruleName . ': ' . l:error.failure \ ? l:error.ruleName . ': ' . l:error.failure
@ -26,13 +27,12 @@ function! ale_linters#typescript#tslint#Handle(buffer, lines) abort
\ 'end_lnum': l:error.endPosition.line + 1, \ 'end_lnum': l:error.endPosition.line + 1,
\ 'end_col': l:error.endPosition.character + 1, \ 'end_col': l:error.endPosition.character + 1,
\}) \})
endif
endfor endfor
return l:output return l:output
endfunction endfunction
function! ale_linters#typescript#tslint#BuildLintCommand(buffer) abort function! ale_linters#typescript#tslint#GetCommand(buffer) abort
let l:tslint_config_path = ale#path#ResolveLocalPath( let l:tslint_config_path = ale#path#ResolveLocalPath(
\ a:buffer, \ a:buffer,
\ 'tslint.json', \ 'tslint.json',
@ -43,7 +43,8 @@ function! ale_linters#typescript#tslint#BuildLintCommand(buffer) abort
\ ? ' -c ' . ale#Escape(l:tslint_config_path) \ ? ' -c ' . ale#Escape(l:tslint_config_path)
\ : '' \ : ''
return ale_linters#typescript#tslint#GetExecutable(a:buffer) return ale#path#BufferCdString(a:buffer)
\ . ale_linters#typescript#tslint#GetExecutable(a:buffer)
\ . ' --format json' \ . ' --format json'
\ . l:tslint_config_option \ . l:tslint_config_option
\ . ' %t' \ . ' %t'
@ -52,6 +53,6 @@ endfunction
call ale#linter#Define('typescript', { call ale#linter#Define('typescript', {
\ 'name': 'tslint', \ 'name': 'tslint',
\ 'executable_callback': 'ale_linters#typescript#tslint#GetExecutable', \ 'executable_callback': 'ale_linters#typescript#tslint#GetExecutable',
\ 'command_callback': 'ale_linters#typescript#tslint#BuildLintCommand', \ 'command_callback': 'ale_linters#typescript#tslint#GetCommand',
\ 'callback': 'ale_linters#typescript#tslint#Handle', \ 'callback': 'ale_linters#typescript#tslint#Handle',
\}) \})

View File

@ -362,6 +362,7 @@ function! ale#engine#FixLocList(buffer, linter_name, loclist) abort
" The linter_name will be set on the errors so it can be used in " The linter_name will be set on the errors so it can be used in
" output, filtering, etc.. " output, filtering, etc..
let l:item = { let l:item = {
\ 'bufnr': a:buffer,
\ 'text': l:old_item.text, \ 'text': l:old_item.text,
\ 'lnum': str2nr(l:old_item.lnum), \ 'lnum': str2nr(l:old_item.lnum),
\ 'col': str2nr(get(l:old_item, 'col', 0)), \ 'col': str2nr(get(l:old_item, 'col', 0)),
@ -372,7 +373,11 @@ function! ale#engine#FixLocList(buffer, linter_name, loclist) abort
\} \}
if has_key(l:old_item, 'filename') if has_key(l:old_item, 'filename')
\&& l:old_item.filename[:len(s:temp_dir) - 1] isnot# s:temp_dir
" Use the filename given. " Use the filename given.
" Temporary files are assumed to be for this buffer,
" and the filename is not included then, because it looks bad
" in the loclist window.
let l:filename = l:old_item.filename let l:filename = l:old_item.filename
let l:item.filename = l:filename let l:item.filename = l:filename
@ -384,10 +389,6 @@ function! ale#engine#FixLocList(buffer, linter_name, loclist) abort
elseif has_key(l:bufnr_map, l:filename) elseif has_key(l:bufnr_map, l:filename)
" Get the buffer number from the map, which can be faster. " Get the buffer number from the map, which can be faster.
let l:item.bufnr = l:bufnr_map[l:filename] let l:item.bufnr = l:bufnr_map[l:filename]
elseif l:filename[:len(s:temp_dir) - 1] is# s:temp_dir
" Assume that any temporary files are for this buffer.
let l:item.bufnr = a:buffer
let l:bufnr_map[l:filename] = a:buffer
else else
" Look up the buffer number. " Look up the buffer number.
let l:item.bufnr = bufnr(l:filename) let l:item.bufnr = bufnr(l:filename)
@ -395,8 +396,6 @@ function! ale#engine#FixLocList(buffer, linter_name, loclist) abort
endif endif
elseif has_key(l:old_item, 'bufnr') elseif has_key(l:old_item, 'bufnr')
let l:item.bufnr = l:old_item.bufnr let l:item.bufnr = l:old_item.bufnr
else
let l:item.bufnr = a:buffer
endif endif
if has_key(l:old_item, 'detail') if has_key(l:old_item, 'detail')

View File

@ -86,6 +86,19 @@ function! ale#path#IsTempName(filename) abort
return 0 return 0
endfunction endfunction
" Given a base directory, which must not have a trailing slash, and a
" filename, which may have an absolute path a path relative to the base
" directory, return the absolute path to the file.
function! ale#path#GetAbsPath(base_directory, filename) abort
if ale#path#IsAbsolute(a:filename)
return a:filename
endif
let l:sep = has('win32') ? '\' : '/'
return ale#path#Simplify(a:base_directory . l:sep . a:filename)
endfunction
" Given a buffer number and a relative or absolute path, return 1 if the " Given a buffer number and a relative or absolute path, return 1 if the
" two paths represent the same file on disk. " two paths represent the same file on disk.
function! ale#path#IsBufferPath(buffer, complex_filename) abort function! ale#path#IsBufferPath(buffer, complex_filename) abort

View File

@ -0,0 +1,24 @@
Before:
Save g:typescript_tslint_executable
Save g:typescript_tslint_config_path
Save g:typescript_tslint_use_global
unlet! g:typescript_tslint_executable
unlet! g:typescript_tslint_config_path
unlet! g:typescript_tslint_use_global
runtime ale_linters/typescript/tslint.vim
call ale#test#SetDirectory('/testplugin/test/command_callback')
After:
Restore
call ale#test#RestoreDirectory()
call ale#linter#Reset()
Execute(The default tslint command should be correct):
AssertEqual
\ 'cd ''' . expand('%:p:h') . ''' && '
\ . 'tslint --format json %t',
\ ale_linters#typescript#tslint#GetCommand(bufnr(''))

View File

@ -15,28 +15,40 @@ Execute(The tslint handler should parse lines correctly):
\ { \ {
\ 'lnum': 1, \ 'lnum': 1,
\ 'col': 15, \ 'col': 15,
\ 'filename': expand('%:p:h') . '/test.ts',
\ 'end_lnum': 1, \ 'end_lnum': 1,
\ 'end_col': 15,
\ 'text': 'semicolon: Missing semicolon',
\ 'type': 'E', \ 'type': 'E',
\ 'end_col': 15,
\ 'text': 'semicolon: Missing semicolon'
\ }, \ },
\ { \ {
\ 'lnum': 2, \ 'lnum': 2,
\ 'col': 8, \ 'col': 8,
\ 'filename': expand('%:p:h') . '/test.ts',
\ 'end_lnum': 3, \ 'end_lnum': 3,
\ 'end_col': 12,
\ 'text': 'Something else',
\ 'type': 'W', \ 'type': 'W',
\ 'end_col': 12,
\ 'text': 'Something else'
\ },
\ {
\ 'lnum': 2,
\ 'col': 8,
\ 'filename': expand('%:p:h') . '/something-else.ts',
\ 'end_lnum': 3,
\ 'type': 'W',
\ 'end_col': 12,
\ 'text': 'something: Something else'
\ }, \ },
\ { \ {
\ 'lnum': 31, \ 'lnum': 31,
\ 'col': 9, \ 'col': 9,
\ 'filename': expand('%:p:h') . '/test.ts',
\ 'end_lnum': 31, \ 'end_lnum': 31,
\ 'end_col': 20,
\ 'text': 'no-console: Calls to console.log are not allowed.',
\ 'type': 'E', \ 'type': 'E',
\ 'end_col': 20,
\ 'text': 'no-console: Calls to console.log are not allowed.'
\ }, \ },
\ ], \ ] ,
\ ale_linters#typescript#tslint#Handle(bufnr(''), [json_encode([ \ ale_linters#typescript#tslint#Handle(bufnr(''), [json_encode([
\ { \ {
\ 'endPosition': { \ 'endPosition': {
@ -50,7 +62,7 @@ Execute(The tslint handler should parse lines correctly):
\ 'innerStart': 14, \ 'innerStart': 14,
\ 'innerText': ';' \ 'innerText': ';'
\ }, \ },
\ 'name': 'app/test.ts', \ 'name': 'test.ts',
\ 'ruleName': 'semicolon', \ 'ruleName': 'semicolon',
\ 'ruleSeverity': 'ERROR', \ 'ruleSeverity': 'ERROR',
\ 'startPosition': { \ 'startPosition': {
@ -71,7 +83,7 @@ Execute(The tslint handler should parse lines correctly):
\ 'innerStart': 14, \ 'innerStart': 14,
\ 'innerText': ';' \ 'innerText': ';'
\ }, \ },
\ 'name': 'app/test.ts', \ 'name': 'test.ts',
\ 'ruleSeverity': 'WARNING', \ 'ruleSeverity': 'WARNING',
\ 'startPosition': { \ 'startPosition': {
\ 'character': 7, \ 'character': 7,
@ -91,7 +103,7 @@ Execute(The tslint handler should parse lines correctly):
\ 'innerStart': 14, \ 'innerStart': 14,
\ 'innerText': ';' \ 'innerText': ';'
\ }, \ },
\ 'name': 'app/something-else.ts', \ 'name': 'something-else.ts',
\ 'ruleName': 'something', \ 'ruleName': 'something',
\ 'ruleSeverity': 'WARNING', \ 'ruleSeverity': 'WARNING',
\ 'startPosition': { \ 'startPosition': {
@ -107,7 +119,7 @@ Execute(The tslint handler should parse lines correctly):
\ "position": 14590 \ "position": 14590
\ }, \ },
\ "failure": "Calls to console.log are not allowed.", \ "failure": "Calls to console.log are not allowed.",
\ 'name': 'app/test.ts', \ 'name': 'test.ts',
\ "ruleName": "no-console", \ "ruleName": "no-console",
\ "startPosition": { \ "startPosition": {
\ "character": 8, \ "character": 8,

View File

@ -0,0 +1,15 @@
Execute(Relative paths should be resolved correctly):
AssertEqual
\ '/foo/bar/baz/whatever.txt',
\ ale#path#GetAbsPath('/foo/bar/xyz', '../baz/whatever.txt')
AssertEqual
\ '/foo/bar/xyz/whatever.txt',
\ ale#path#GetAbsPath('/foo/bar/xyz', './whatever.txt')
AssertEqual
\ '/foo/bar/xyz/whatever.txt',
\ ale#path#GetAbsPath('/foo/bar/xyz', 'whatever.txt')
Execute(Absolute paths should be resolved correctly):
AssertEqual
\ '/ding/dong',
\ ale#path#GetAbsPath('/foo/bar/xyz', '/ding/dong')

View File

@ -303,7 +303,6 @@ Execute(FixLocList should interpret temporary filenames as being the current buf
\ 'lnum': 2, \ 'lnum': 2,
\ 'col': 0, \ 'col': 0,
\ 'bufnr': bufnr(''), \ 'bufnr': bufnr(''),
\ 'filename': b:temp_name,
\ 'vcol': 0, \ 'vcol': 0,
\ 'type': 'E', \ 'type': 'E',
\ 'nr': -1, \ 'nr': -1,
@ -314,7 +313,6 @@ Execute(FixLocList should interpret temporary filenames as being the current buf
\ 'lnum': 3, \ 'lnum': 3,
\ 'col': 0, \ 'col': 0,
\ 'bufnr': bufnr(''), \ 'bufnr': bufnr(''),
\ 'filename': b:temp_name,
\ 'vcol': 0, \ 'vcol': 0,
\ 'type': 'E', \ 'type': 'E',
\ 'nr': -1, \ 'nr': -1,