This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
ale/ale_linters/ruby/rubocop.vim
Eddie Lebow dcbb0ffee5 Rubocop: handle empty 'files' array in output
The handler previously assumed there would be at least one entry in the
'files' array in the output JSON. It looks like this in the normal case:

  "files":[{"path":"app/models/image.rb","offenses":[]}]

But if RuboCop's config excludes the specified input files, causing no
files to be linted, the output is emptier:

  "files":[]

This change causes the handler to treat that case correctly, and also
exit early if the reported offense_count is zero.
2017-07-12 19:53:58 -04:00

67 lines
2 KiB
VimL

" Author: ynonp - https://github.com/ynonp, Eddie Lebow https://github.com/elebow
" Description: RuboCop, a code style analyzer for Ruby files
function! ale_linters#ruby#rubocop#GetCommand(buffer) abort
let l:executable = ale#handlers#rubocop#GetExecutable(a:buffer)
let l:exec_args = l:executable =~? 'bundle$'
\ ? ' exec rubocop'
\ : ''
return ale#Escape(l:executable) . l:exec_args
\ . ' --format json --force-exclusion '
\ . ale#Var(a:buffer, 'ruby_rubocop_options')
\ . ' --stdin ' . ale#Escape(expand('#' . a:buffer . ':p'))
endfunction
function! ale_linters#ruby#rubocop#Handle(buffer, lines) abort
if empty(a:lines)
return []
endif
try
let l:errors = json_decode(a:lines[0])
catch
return [{
\ 'lnum': 1,
\ 'text': 'rubocop configuration error (type :ALEDetail for more information)',
\ 'detail': join(a:lines, "\n"),
\}]
endtry
if l:errors['summary']['offense_count'] == 0 || empty(l:errors['files'])
return []
endif
let l:output = []
for l:error in l:errors['files'][0]['offenses']
let l:start_col = l:error['location']['column'] + 0
call add(l:output, {
\ 'lnum': l:error['location']['line'] + 0,
\ 'col': l:start_col,
\ 'end_col': l:start_col + l:error['location']['length'] - 1,
\ 'text': l:error['message'],
\ 'type': ale_linters#ruby#rubocop#GetType(l:error['severity']),
\})
endfor
return l:output
endfunction
function! ale_linters#ruby#rubocop#GetType(severity) abort
if a:severity ==? 'convention'
\|| a:severity ==? 'warning'
\|| a:severity ==? 'refactor'
return 'W'
endif
return 'E'
endfunction
call ale#linter#Define('ruby', {
\ 'name': 'rubocop',
\ 'executable_callback': 'ale#handlers#rubocop#GetExecutable',
\ 'command_callback': 'ale_linters#ruby#rubocop#GetCommand',
\ 'callback': 'ale_linters#ruby#rubocop#Handle',
\ 'output_stream': 'both',
\})