ale/ale_linters/ruby/rubocop.vim

62 lines
1.8 KiB
VimL
Raw Normal View History

" Author: ynonp - https://github.com/ynonp, Eddie Lebow https://github.com/elebow
" Description: RuboCop, a code style analyzer for Ruby files
2017-05-18 04:22:34 -04:00
function! ale_linters#ruby#rubocop#GetCommand(buffer) abort
let l:executable = ale#handlers#rubocop#GetExecutable(a:buffer)
2017-05-18 04:22:34 -04:00
let l:exec_args = l:executable =~? 'bundle$'
\ ? ' exec rubocop'
\ : ''
return ale#Escape(l:executable) . l:exec_args
\ . ' --format json --force-exclusion '
2017-05-18 04:22:34 -04:00
\ . ale#Var(a:buffer, 'ruby_rubocop_options')
\ . ' --stdin ' . ale#Escape(expand('#' . a:buffer . ':p'))
2017-05-18 04:22:34 -04:00
endfunction
function! ale_linters#ruby#rubocop#Handle(buffer, lines) abort
try
let l:errors = json_decode(a:lines[0])
catch
return []
endtry
2016-09-15 03:37:53 -04:00
if !has_key(l:errors, 'summary')
\|| l:errors['summary']['offense_count'] == 0
\|| empty(l:errors['files'])
return []
endif
let l:output = []
2016-09-15 03:37:53 -04:00
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,
2017-11-14 11:50:15 -05:00
\ 'code': l:error['cop_name'],
\ 'text': l:error['message'],
\ 'type': ale_linters#ruby#rubocop#GetType(l:error['severity']),
2016-09-15 03:37:53 -04:00
\})
endfor
return l:output
2016-09-15 03:37:53 -04:00
endfunction
function! ale_linters#ruby#rubocop#GetType(severity) abort
if a:severity is? 'convention'
\|| a:severity is? 'warning'
\|| a:severity is? 'refactor'
2017-07-09 10:50:36 -04:00
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',
2016-09-15 03:37:53 -04:00
\ 'callback': 'ale_linters#ruby#rubocop#Handle',
\})