Upgrade Elm linter to support 0.19 error reports

This commit is contained in:
Héctor Ramón Jiménez 2018-05-11 19:15:40 +02:00
parent 38c66d33fe
commit d40f447931
4 changed files with 138 additions and 69 deletions

View File

@ -1,46 +1,66 @@
" Author: buffalocoder - https://github.com/buffalocoder, soywod - https://github.com/soywod " Author: buffalocoder - https://github.com/buffalocoder, soywod - https://github.com/soywod
" Description: Elm linting in Ale. Closely follows the Syntastic checker in https://github.com/ElmCast/elm-vim. " Description: Elm linting in Ale. Closely follows the Syntastic checker in https://github.com/ElmCast/elm-vim.
call ale#Set('elm_make_executable', 'elm-make') call ale#Set('elm_executable', 'elm')
call ale#Set('elm_make_use_global', get(g:, 'ale_use_global_executables', 0)) call ale#Set('elm_use_global', get(g:, 'ale_use_global_executables', 0))
function! ale_linters#elm#make#GetExecutable(buffer) abort function! ale_linters#elm#make#GetExecutable(buffer) abort
return ale#node#FindExecutable(a:buffer, 'elm_make', [ return ale#node#FindExecutable(a:buffer, 'elm', [
\ 'node_modules/.bin/elm-make', \ 'node_modules/.bin/elm',
\]) \])
endfunction endfunction
function! ale_linters#elm#make#Handle(buffer, lines) abort function! ale_linters#elm#make#Handle(buffer, lines) abort
let l:output = [] let l:output = []
let l:is_windows = has('win32')
let l:temp_dir = l:is_windows ? $TMP : $TMPDIR
let l:unparsed_lines = [] let l:unparsed_lines = []
for l:line in a:lines for l:line in a:lines
if l:line[0] is# '[' if l:line[0] is# '{'
let l:errors = json_decode(l:line) let l:report = json_decode(l:line)
for l:error in l:errors if l:report.type is? 'error'
" Check if file is from the temp directory. " General problem
" Filters out any errors not related to the buffer. let l:details = map(copy(l:report.message), 'ale_linters#elm#make#ParseMessageItem(v:val)')
if l:is_windows
let l:file_is_buffer = l:error.file[0:len(l:temp_dir) - 1] is? l:temp_dir
else
let l:file_is_buffer = l:error.file[0:len(l:temp_dir) - 1] is# l:temp_dir
endif
if l:file_is_buffer call add(l:output, {
call add(l:output, { \ 'lnum': 1,
\ 'lnum': l:error.region.start.line, \ 'type': 'E',
\ 'col': l:error.region.start.column, \ 'text': l:report.title,
\ 'end_lnum': l:error.region.end.line, \ 'detail': join(l:details, '')
\ 'end_col': l:error.region.end.column, \})
\ 'type': (l:error.type is? 'error') ? 'E' : 'W', else
\ 'text': l:error.overview, " Compilation errors
\ 'detail': l:error.overview . "\n\n" . l:error.details for l:error in l:report.errors
\}) let l:file_is_buffer = ale_linters#elm#make#FileIsBuffer(l:error.path)
endif
endfor for l:problem in l:error.problems
elseif l:line isnot# 'Successfully generated /dev/null' let l:details = map(copy(l:problem.message), 'ale_linters#elm#make#ParseMessageItem(v:val)')
if l:file_is_buffer
" Buffer module has problems
call add(l:output, {
\ 'lnum': l:problem.region.start.line,
\ 'col': l:problem.region.start.column,
\ 'end_lnum': l:problem.region.end.line,
\ 'end_col': l:problem.region.end.column,
\ 'type': 'E',
\ 'text': l:problem.title,
\ 'detail': join(l:details, '')
\})
else
" Imported module has problems
let l:location = l:error.path .':'. l:problem.region.start.line
call add(l:output, {
\ 'lnum': 1,
\ 'type': 'E',
\ 'text': l:location .' - '. l:problem.title,
\ 'detail': l:location ." -------\n\n" . join(l:details, '')
\})
endif
endfor
endfor
endif
else
call add(l:unparsed_lines, l:line) call add(l:unparsed_lines, l:line)
endif endif
endfor endfor
@ -57,23 +77,44 @@ function! ale_linters#elm#make#Handle(buffer, lines) abort
return l:output return l:output
endfunction endfunction
function! ale_linters#elm#make#FileIsBuffer(path) abort
let l:is_windows = has('win32')
let l:temp_dir = l:is_windows ? $TMP : $TMPDIR
if has('win32')
return a:path[0:len(l:temp_dir) - 1] is? l:temp_dir
else
return a:path[0:len(l:temp_dir) - 1] is# l:temp_dir
endif
endfunction
function! ale_linters#elm#make#ParseMessageItem(item) abort
if type(a:item) == type('')
return a:item
else
return a:item.string
endif
endfunction
" Return the command to execute the linter in the projects directory. " Return the command to execute the linter in the projects directory.
" If it doesn't, then this will fail when imports are needed. " If it doesn't, then this will fail when imports are needed.
function! ale_linters#elm#make#GetCommand(buffer) abort function! ale_linters#elm#make#GetCommand(buffer) abort
let l:elm_package = ale#path#FindNearestFile(a:buffer, 'elm-package.json') let l:elm_json = ale#path#FindNearestFile(a:buffer, 'elm.json')
let l:elm_exe = ale_linters#elm#make#GetExecutable(a:buffer) let l:elm_exe = ale_linters#elm#make#GetExecutable(a:buffer)
if empty(l:elm_package)
if empty(l:elm_json)
let l:dir_set_cmd = '' let l:dir_set_cmd = ''
else else
let l:root_dir = fnamemodify(l:elm_package, ':p:h') let l:root_dir = fnamemodify(l:elm_json, ':p:h')
let l:dir_set_cmd = 'cd ' . ale#Escape(l:root_dir) . ' && ' let l:dir_set_cmd = 'cd ' . ale#Escape(l:root_dir) . ' && '
endif endif
" The elm-make compiler, at the time of this writing, uses '/dev/null' as " The elm compiler, at the time of this writing, uses '/dev/null' as
" a sort of flag to tell the compiler not to generate an output file, " a sort of flag to tell the compiler not to generate an output file,
" which is why this is hard coded here. It does not use NUL on Windows. " which is why this is hard coded here.
" Source: https://github.com/elm-lang/elm-make/blob/master/src/Flags.hs " Source: https://github.com/elm-lang/elm-compiler/blob/19d5a769b30ec0b2fc4475985abb4cd94cd1d6c3/builder/src/Generate/Output.hs#L253
let l:elm_cmd = ale#Escape(l:elm_exe) let l:elm_cmd = ale#Escape(l:elm_exe)
\ . ' make'
\ . ' --report=json' \ . ' --report=json'
\ . ' --output=/dev/null' \ . ' --output=/dev/null'

View File

@ -13,22 +13,13 @@ Execute(The elm-make handler should parse lines correctly):
AssertEqual AssertEqual
\ [ \ [
\ { \ {
\ 'lnum': 33,
\ 'col': 1,
\ 'end_lnum': 33,
\ 'end_col': 19,
\ 'type': 'W',
\ 'text': 'warning overview',
\ 'detail': "warning overview\n\nwarning details",
\ },
\ {
\ 'lnum': 404, \ 'lnum': 404,
\ 'col': 1, \ 'col': 1,
\ 'end_lnum': 408, \ 'end_lnum': 408,
\ 'end_col': 18, \ 'end_col': 18,
\ 'type': 'E', \ 'type': 'E',
\ 'text': 'error overview 1', \ 'text': 'TYPE MISMATCH',
\ 'detail': "error overview 1\n\nerror details 1", \ 'detail': "error details 1 styled details"
\ }, \ },
\ { \ {
\ 'lnum': 406, \ 'lnum': 406,
@ -36,8 +27,8 @@ Execute(The elm-make handler should parse lines correctly):
\ 'end_lnum': 407, \ 'end_lnum': 407,
\ 'end_col': 17, \ 'end_col': 17,
\ 'type': 'E', \ 'type': 'E',
\ 'text': 'error overview 2', \ 'text': 'TYPE MISMATCH',
\ 'detail': "error overview 2\n\nerror details 2", \ 'detail': "error details 2",
\ }, \ },
\ { \ {
\ 'lnum': 406, \ 'lnum': 406,
@ -45,26 +36,49 @@ Execute(The elm-make handler should parse lines correctly):
\ 'end_lnum': 406, \ 'end_lnum': 406,
\ 'end_col': 93, \ 'end_col': 93,
\ 'type': 'E', \ 'type': 'E',
\ 'text': 'error overview 3', \ 'text': 'TYPE MISMATCH',
\ 'detail': "error overview 3\n\nerror details 3", \ 'detail': "error details 3",
\ }, \ },
\ ], \ ],
\ ale_linters#elm#make#Handle(347, [ \ ale_linters#elm#make#Handle(347, [
\ '[{"tag":"unused import","overview":"warning overview","details":"warning details","region":{"start":{"line":33,"column":1},"end":{"line":33,"column":19}},"type":"warning","file":"' . b:tmp . 'Module.elm"}]', \ '{
\ '[{"tag":"TYPE MISMATCH","overview":"error overview 1","subregion":{"start":{"line":406,"column":5},"end":{"line":408,"column":18}},"details":"error details 1","region":{"start":{"line":404,"column":1},"end":{"line":408,"column":18}},"type":"error","file":"' . b:tmp . 'Module.elm"},{"tag":"TYPE MISMATCH","overview":"error overview 2","subregion":{"start":{"line":407,"column":12},"end":{"line":407,"column":17}},"details":"error details 2","region":{"start":{"line":406,"column":5},"end":{"line":407,"column":17}},"type":"error","file":"' . b:tmp . 'Module.elm"},{"tag":"TYPE MISMATCH","overview":"error overview 3","subregion":{"start":{"line":406,"column":88},"end":{"line":406,"column":93}},"details":"error details 3","region":{"start":{"line":406,"column":5},"end":{"line":406,"column":93}},"type":"error","file":"' . b:tmp . 'Module.elm"}]' \ "type":"compile-errors",
\ "errors": [
\ {
\ "path": "' . b:tmp . 'Module.elm",
\ "problems": [
\ {
\ "title": "TYPE MISMATCH",
\ "message": ["error details 1 ", { "string": "styled details" }],
\ "region": { "start": { "line": 404, "column":1 }, "end": { "line":408, "column":18} }
\ },
\ {
\ "title": "TYPE MISMATCH",
\ "message": ["error details 2"],
\ "region": { "start": {"line": 406, "column": 5}, "end": {"line": 407, "column": 17} }
\ },
\ {
\ "title": "TYPE MISMATCH",
\ "message": ["error details 3"],
\ "region": { "start": { "line": 406, "column": 5}, "end": {"line": 406, "column":93 } }
\ }
\ ]
\ }
\ ]
\ }'
\ ]) \ ])
Execute(The elm-make handler should put an error on the first line if a line cannot be parsed): Execute(The elm-make handler should put an error on the first line if a line cannot be parsed):
AssertEqual AssertEqual
\ [ \ [
\ { \ {
\ 'lnum': 33, \ 'lnum': 404,
\ 'col': 1, \ 'col': 1,
\ 'end_lnum': 33, \ 'end_lnum': 408,
\ 'end_col': 19, \ 'end_col': 18,
\ 'type': 'W', \ 'type': 'E',
\ 'text': 'warning overview', \ 'text': 'TYPE MISMATCH',
\ 'detail': "warning overview\n\nwarning details", \ 'detail': "error details 1 styled details"
\ }, \ },
\ { \ {
\ 'lnum': 1, \ 'lnum': 1,
@ -74,7 +88,21 @@ Execute(The elm-make handler should put an error on the first line if a line can
\ }, \ },
\ ], \ ],
\ ale_linters#elm#make#Handle(347, [ \ ale_linters#elm#make#Handle(347, [
\ '[{"tag":"unused import","overview":"warning overview","details":"warning details","region":{"start":{"line":33,"column":1},"end":{"line":33,"column":19}},"type":"warning","file":"' . b:tmp . 'Module.elm"}]', \ '{
\ "Not JSON", \ "type":"compile-errors",
\ "Also not JSON", \ "errors": [
\ {
\ "path": "' . b:tmp . 'Module.elm",
\ "problems": [
\ {
\ "title": "TYPE MISMATCH",
\ "message": ["error details 1 ", { "string": "styled details" }],
\ "region": { "start": { "line": 404, "column":1 }, "end": { "line":408, "column":18} }
\ }
\ ]
\ }
\ ]
\ }',
\ "Not JSON",
\ "Also not JSON",
\ ]) \ ])

View File

@ -3,8 +3,8 @@ Before:
runtime ale_linters/elm/make.vim runtime ale_linters/elm/make.vim
After: After:
unlet! g:ale_elm_make_use_global unlet! g:ale_elm_use_global
unlet! g:ale_elm_make_executable unlet! g:ale_elm_executable
call ale#test#RestoreDirectory() call ale#test#RestoreDirectory()
@ -12,25 +12,25 @@ Execute(should get valid executable with default params):
call ale#test#SetFilename('elm-test-files/app/testfile.elm') call ale#test#SetFilename('elm-test-files/app/testfile.elm')
AssertEqual AssertEqual
\ ale#path#Simplify(g:dir . '/elm-test-files/app/node_modules/.bin/elm-make'), \ ale#path#Simplify(g:dir . '/elm-test-files/app/node_modules/.bin/elm'),
\ ale_linters#elm#make#GetExecutable(bufnr('')) \ ale_linters#elm#make#GetExecutable(bufnr(''))
Execute(should get valid executable with 'use_global' params): Execute(should get valid executable with 'use_global' params):
let g:ale_elm_make_use_global = 1 let g:ale_elm_use_global = 1
call ale#test#SetFilename('elm-test-files/app/testfile.elm') call ale#test#SetFilename('elm-test-files/app/testfile.elm')
AssertEqual AssertEqual
\ 'elm-make', \ 'elm',
\ ale_linters#elm#make#GetExecutable(bufnr('')) \ ale_linters#elm#make#GetExecutable(bufnr(''))
Execute(should get valid executable with 'use_global' and 'executable' params): Execute(should get valid executable with 'use_global' and 'executable' params):
let g:ale_elm_make_executable = 'other-elm-make' let g:ale_elm_executable = 'other-elm'
let g:ale_elm_make_use_global = 1 let g:ale_elm_use_global = 1
call ale#test#SetFilename('elm-test-files/app/testfile.elm') call ale#test#SetFilename('elm-test-files/app/testfile.elm')
AssertEqual AssertEqual
\ 'other-elm-make', \ 'other-elm',
\ ale_linters#elm#make#GetExecutable(bufnr('')) \ ale_linters#elm#make#GetExecutable(bufnr(''))