Fix crashes with incomplete errors

This commit is contained in:
Travis Gibson 2018-10-03 14:13:34 -07:00
parent 2b2e766dc6
commit adad9a21ab
2 changed files with 112 additions and 18 deletions

View file

@ -21,9 +21,9 @@ function! ale_linters#perl6#perl6#GetCommand(buffer) abort
\ . ' %t' \ . ' %t'
endfunction endfunction
function! ale_linters#perl6#perl6#ExtractError(dict, item, type) abort function! ale_linters#perl6#perl6#ExtractError(dict, item, type, buffer) abort
let l:file = '' let l:file = ''
let l:line = '' let l:line = 1
let l:column = '' let l:column = ''
let l:text = '' let l:text = ''
let l:pre = '' let l:pre = ''
@ -32,26 +32,26 @@ function! ale_linters#perl6#perl6#ExtractError(dict, item, type) abort
let l:linepatternmessage = 'at\s\+line\s\+\(\d\+\)' let l:linepatternmessage = 'at\s\+line\s\+\(\d\+\)'
if has_key(a:dict[a:item], 'filename') && !empty(a:dict[a:item]['filename']) if has_key(a:dict[a:item], 'filename') && !empty(a:dict[a:item]['filename'])
let l:file .= a:dict[a:item]['filename'] let l:file = a:dict[a:item]['filename']
endif endif
if has_key(a:dict[a:item], 'line') && !empty(a:dict[a:item]['line']) if has_key(a:dict[a:item], 'line') && !empty(a:dict[a:item]['line'])
let l:line .= a:dict[a:item]['line'] let l:line = a:dict[a:item]['line']
let l:counter -= 1 let l:counter -= 1
endif endif
if has_key(a:dict[a:item], 'column') && !empty(a:dict[a:item]['column']) if has_key(a:dict[a:item], 'column') && !empty(a:dict[a:item]['column'])
let l:column .= a:dict[a:item]['column'] let l:column = a:dict[a:item]['column']
endif endif
if has_key(a:dict[a:item], 'message') && !empty(a:dict[a:item]['message']) if has_key(a:dict[a:item], 'message') && !empty(a:dict[a:item]['message'])
let l:text .= substitute(a:dict[a:item]['message'], '\s*\n\s*', ' ', 'g') let l:text = substitute(a:dict[a:item]['message'], '\s*\n\s*', ' ', 'g')
let l:counter -= 1 let l:counter -= 1
endif endif
if has_key(a:dict[a:item], 'line-real') && !empty(a:dict[a:item]['line-real']) if has_key(a:dict[a:item], 'line-real') && !empty(a:dict[a:item]['line-real'])
let l:end_line = l:line let l:end_line = l:line
let l:line .= a:dict[a:item]['line-real'] let l:line = a:dict[a:item]['line-real']
endif endif
for l:match in ale#util#GetMatches(l:text, l:linepatternmessage) for l:match in ale#util#GetMatches(l:text, l:linepatternmessage)
@ -59,9 +59,11 @@ function! ale_linters#perl6#perl6#ExtractError(dict, item, type) abort
let l:counter -= 1 let l:counter -= 1
endfor endfor
if l:counter < 1 " Currently, filenames and line numbers are not always given in the error output
if l:counter < 2
\&& ( ale#path#IsBufferPath(a:buffer, l:file) || l:file is# '' )
return { return {
\ 'lnum': l:line, \ 'lnum': '' . l:line,
\ 'text': l:text, \ 'text': l:text,
\ 'type': a:type, \ 'type': a:type,
\ 'col': l:column, \ 'col': l:column,
@ -69,6 +71,8 @@ function! ale_linters#perl6#perl6#ExtractError(dict, item, type) abort
\ 'code': a:item, \ 'code': a:item,
\} \}
endif endif
return ''
endfunction endfunction
function! ale_linters#perl6#perl6#Handle(buffer, lines) abort function! ale_linters#perl6#perl6#Handle(buffer, lines) abort
@ -102,13 +106,17 @@ function! ale_linters#perl6#perl6#Handle(buffer, lines) abort
if !empty(l:json[l:key]['sorrows']) if !empty(l:json[l:key]['sorrows'])
for l:dictionary in get(l:json[l:key], 'sorrows') for l:dictionary in get(l:json[l:key], 'sorrows')
for l:item in keys(l:dictionary) for l:item in keys(l:dictionary)
call add(l:output, let l:result =
\ ale_linters#perl6#perl6#ExtractError( \ ale_linters#perl6#perl6#ExtractError(
\ l:dictionary, \ l:dictionary,
\ l:item, \ l:item,
\ 'E' \ 'E',
\ ) \ a:buffer,
\ ) \ )
if l:result isnot# ''
call add(l:output, l:result)
endif
endfor endfor
endfor endfor
endif endif
@ -116,20 +124,31 @@ function! ale_linters#perl6#perl6#Handle(buffer, lines) abort
if !empty(l:json[l:key]['worries']) if !empty(l:json[l:key]['worries'])
for l:dictionary in get(l:json[l:key], 'worries') for l:dictionary in get(l:json[l:key], 'worries')
for l:item in keys(l:dictionary) for l:item in keys(l:dictionary)
call add(l:output, let l:result =
\ ale_linters#perl6#perl6#ExtractError( \ ale_linters#perl6#perl6#ExtractError(
\ l:dictionary, \ l:dictionary,
\ l:item, \ l:item,
\ 'W' \ 'W',
\ ) \ a:buffer,
\ ) \ )
if l:result isnot# ''
call add(l:output, l:result)
endif
endfor endfor
endfor endfor
endif endif
else else
call add(l:output, let l:result = ale_linters#perl6#perl6#ExtractError(
\ ale_linters#perl6#perl6#ExtractError(l:json, l:key, 'E') \ l:json,
\ l:key,
\ 'E',
\ a:buffer,
\ ) \ )
if l:result isnot# ''
call add(l:output, l:result)
endif
endif endif
endfor endfor
endif endif

View file

@ -200,3 +200,78 @@ Execute(The Perl6 linter should gracefully handle non-JSON messages):
\ ' ------> /win<HERE> 3/', \ ' ------> /win<HERE> 3/',
\ 'Syntax OK' \ 'Syntax OK'
\ ]) \ ])
Execute(The Perl6 linter should gracefully handle messages without a line number):
call ale#test#SetFilename('bar.pl6')
AssertEqual
\ [
\ {
\ 'lnum': '1',
\ 'end_lnum': '',
\ 'text': 'Cannot find method ''has_compile_time_value'' on object of type NQPMu',
\ 'type': 'E',
\ 'col' : '',
\ 'code': 'X::AdHoc',
\ }
\ ],
\ ale_linters#perl6#perl6#Handle(bufnr(''), [
\ '{',
\ '"X::AdHoc" : {',
\ '"message" : "Cannot find method ''has_compile_time_value'' on object of type NQPMu",',
\ '"payload" : "Cannot find method ''has_compile_time_value'' on object of type NQPMu"',
\ '}',
\ '}',
\ ])
Execute(The Perl6 linter should not include errors from a known separate file):
call ale#test#SetFilename('bar.pl6')
AssertEqual
\ [],
\ ale_linters#perl6#perl6#Handle(bufnr(''), [
\ '{
\ "X::Undeclared" : {
\ "highexpect" : [ ],
\ "is-compile-time" : 1,
\ "modules" : [ ],
\ "column" : null,
\ "pos" : 18,
\ "symbol" : "$tes",
\ "filename" : "foo.pl6",
\ "what" : "Variable",
\ "pre" : "my $test = 0; say ",
\ "post" : "$tes",
\ "suggestions" : [
\ "$res",
\ "$test"
\ ],
\ "line" : 6,
\ "message" : "Variable ''$tes'' is not declared. Did you mean any of these?\n $res\n $test\n"
\ }
\ }'
\ ])
Execute(The Perl6 linter should not ignore errors without a filename):
call ale#test#SetFilename('bar.pl6')
AssertEqual
\ [
\ {
\ 'lnum': '3',
\ 'end_lnum': '',
\ 'text': 'Cannot find method ''has_compile_time_value'' on object of type NQPMu',
\ 'type': 'E',
\ 'col' : '',
\ 'code': 'X::AdHoc',
\ }
\ ],
\ ale_linters#perl6#perl6#Handle(bufnr(''), [
\ '{',
\ '"X::AdHoc" : {',
\ '"line" : 3,',
\ '"message" : "Cannot find method ''has_compile_time_value'' on object of type NQPMu",',
\ '"payload" : "Cannot find method ''has_compile_time_value'' on object of type NQPMu"',
\ '}',
\ '}',
\ ])