Use rubocop's JSON output format (resolves #339) (#738)

* Use rubocop's JSON output format (resolves #339)

Rubocop's emacs formatter seems to have changed format in some
not-so-ancient version. The JSON formatter should provide a more stable
interface than parsing lines with a regex.

The JSON formatter was introduced in mid-2013, so it should be safe to
assume available in any reasonably-modern environment. The oldest
currently-supported version of ruby (according to ruby-lang.org) was
not supported by rubocop until 2014.

* Rubocop: Use global function for GetType

* Rubocop: Use scope prefix in GetType

* Rubocop: Update command_callback test

* Rubocop: add end_col to Handle
This commit is contained in:
Eddie Lebow 2017-07-09 10:48:04 -04:00 committed by w0rp
parent 836a2cfe3b
commit ab0e76dbd5
3 changed files with 41 additions and 24 deletions

View file

@ -1,5 +1,5 @@
" Author: ynonp - https://github.com/ynonp " Author: ynonp - https://github.com/ynonp, Eddie Lebow https://github.com/elebow
" Description: rubocop for Ruby files " Description: RuboCop, a code style analyzer for Ruby files
function! ale_linters#ruby#rubocop#GetCommand(buffer) abort function! ale_linters#ruby#rubocop#GetCommand(buffer) abort
let l:executable = ale#handlers#rubocop#GetExecutable(a:buffer) let l:executable = ale#handlers#rubocop#GetExecutable(a:buffer)
@ -8,34 +8,51 @@ function! ale_linters#ruby#rubocop#GetCommand(buffer) abort
\ : '' \ : ''
return ale#Escape(l:executable) . l:exec_args return ale#Escape(l:executable) . l:exec_args
\ . ' --format emacs --force-exclusion ' \ . ' --format json --force-exclusion '
\ . ale#Var(a:buffer, 'ruby_rubocop_options') \ . ale#Var(a:buffer, 'ruby_rubocop_options')
\ . ' --stdin ' . bufname(a:buffer) \ . ' --stdin ' . bufname(a:buffer)
endfunction endfunction
function! ale_linters#ruby#rubocop#Handle(buffer, lines) abort function! ale_linters#ruby#rubocop#Handle(buffer, lines) abort
" Matches patterns line the following: if len(a:lines) == 0
" return []
" <path>:83:29: C: Prefer single-quoted strings when you don't endif
" need string interpolation or special symbols.
let l:pattern = '\v:(\d+):(\d+): (.): (.+)' let l:errors = json_decode(a:lines[0])
let l:output = [] let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern) for l:error in l:errors['files'][0]['offenses']
let l:text = l:match[4] let l:start_col = l:error['location']['column'] + 0
let l:type = l:match[3]
call add(l:output, { call add(l:output, {
\ 'lnum': l:match[1] + 0, \ 'lnum': l:error['location']['line'] + 0,
\ 'col': l:match[2] + 0, \ 'col': l:start_col,
\ 'text': l:text, \ 'end_col': l:start_col + l:error['location']['length'] - 1,
\ 'type': index(['F', 'E'], l:type) != -1 ? 'E' : 'W', \ 'text': l:error['message'],
\ 'type': ale_linters#ruby#rubocop#GetType(l:error['severity']),
\}) \})
endfor endfor
return l:output return l:output
endfunction endfunction
function! ale_linters#ruby#rubocop#GetType(severity) abort
if a:severity ==? 'refactor'
return 'W'
elseif a:severity ==? 'convention'
return 'W'
elseif a:severity ==? 'warning'
return 'W'
elseif a:severity ==? 'error'
return 'E'
elseif a:severity ==? 'fatal'
return 'E'
else
echo 'Rubocop offense type unrecognized by ALE: ' + a:severity
return ''
endif
endfunction
call ale#linter#Define('ruby', { call ale#linter#Define('ruby', {
\ 'name': 'rubocop', \ 'name': 'rubocop',
\ 'executable_callback': 'ale#handlers#rubocop#GetExecutable', \ 'executable_callback': 'ale#handlers#rubocop#GetExecutable',

View file

@ -3,17 +3,17 @@ Before:
Execute(Executable should default to rubocop): Execute(Executable should default to rubocop):
AssertEqual AssertEqual
\ '''rubocop'' --format emacs --force-exclusion --stdin ''dummy.py''', \ '''rubocop'' --format json --force-exclusion --stdin ''dummy.py''',
\ ale_linters#ruby#rubocop#GetCommand(bufnr('')) \ ale_linters#ruby#rubocop#GetCommand(bufnr(''))
Execute(Should be able to set a custom executable): Execute(Should be able to set a custom executable):
let g:ale_ruby_rubocop_executable = 'bin/rubocop' let g:ale_ruby_rubocop_executable = 'bin/rubocop'
AssertEqual AssertEqual
\ '''bin/rubocop'' --format emacs --force-exclusion --stdin ''dummy.py''', \ '''bin/rubocop'' --format json --force-exclusion --stdin ''dummy.py''',
\ ale_linters#ruby#rubocop#GetCommand(bufnr('')) \ ale_linters#ruby#rubocop#GetCommand(bufnr(''))
Execute(Setting bundle appends 'exec rubocop'): Execute(Setting bundle appends 'exec rubocop'):
let g:ale_ruby_rubocop_executable = 'path to/bundle' let g:ale_ruby_rubocop_executable = 'path to/bundle'
AssertEqual AssertEqual
\ '''path to/bundle'' exec rubocop --format emacs --force-exclusion --stdin ''dummy.py''', \ '''path to/bundle'' exec rubocop --format json --force-exclusion --stdin ''dummy.py''',
\ ale_linters#ruby#rubocop#GetCommand(bufnr('')) \ ale_linters#ruby#rubocop#GetCommand(bufnr(''))

View file

@ -6,34 +6,34 @@ Execute(The rubocop handler should parse lines correctly):
\ { \ {
\ 'lnum': 83, \ 'lnum': 83,
\ 'col': 29, \ 'col': 29,
\ 'end_col': 35,
\ 'text': 'Prefer single-quoted strings...', \ 'text': 'Prefer single-quoted strings...',
\ 'type': 'W', \ 'type': 'W',
\ }, \ },
\ { \ {
\ 'lnum': 12, \ 'lnum': 12,
\ 'col': 2, \ 'col': 2,
\ 'end_col': 2,
\ 'text': 'Some error', \ 'text': 'Some error',
\ 'type': 'E', \ 'type': 'E',
\ }, \ },
\ { \ {
\ 'lnum': 10, \ 'lnum': 10,
\ 'col': 5, \ 'col': 5,
\ 'end_col': 12,
\ 'text': 'Regular warning', \ 'text': 'Regular warning',
\ 'type': 'W', \ 'type': 'W',
\ }, \ },
\ { \ {
\ 'lnum': 11, \ 'lnum': 11,
\ 'col': 1, \ 'col': 1,
\ 'end_col': 1,
\ 'text': 'Another error', \ 'text': 'Another error',
\ 'type': 'E', \ 'type': 'E',
\ }, \ },
\ ], \ ],
\ ale_linters#ruby#rubocop#Handle(347, [ \ ale_linters#ruby#rubocop#Handle(347, [
\ 'This line should be ignored completely', \ '{"metadata":{"rubocop_version":"0.47.1","ruby_engine":"ruby","ruby_version":"2.1.5","ruby_patchlevel":"273","ruby_platform":"x86_64-linux-gnu"},"files":[{"path":"my_great_file.rb","offenses":[{"severity":"convention","message":"Prefer single-quoted strings...","cop_name":"Style/SomeCop","corrected":false,"location":{"line":83,"column":29,"length":7}},{"severity":"fatal","message":"Some error","cop_name":"Style/SomeOtherCop","corrected":false,"location":{"line":12,"column":2,"length":1}},{"severity":"warning","message":"Regular warning","cop_name":"Style/WarningCop","corrected":false,"location":{"line":10,"column":5,"length":8}},{"severity":"error","message":"Another error","cop_name":"Style/SpaceBeforeBlockBraces","corrected":false,"location":{"line":11,"column":1,"length":1}}]}],"summary":{"offense_count":4,"target_file_count":1,"inspected_file_count":1}}'
\ 'whatever:83:29: C: Prefer single-quoted strings...',
\ 'whatever:12:2: F: Some error',
\ 'whatever:10:5: W: Regular warning',
\ 'whatever:11:1: E: Another error',
\ ]) \ ])
After: After: