2018-03-31 10:55:20 -04:00
|
|
|
" Author: ynonp - https://github.com/ynonp, Eddie Lebow https://github.com/elebow
|
|
|
|
" Description: RuboCop, a code style analyzer for Ruby files
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
call ale#Set('ruby_rubocop_executable', 'rubocop')
|
|
|
|
call ale#Set('ruby_rubocop_options', '')
|
|
|
|
|
2018-03-31 10:55:20 -04:00
|
|
|
function! ale_linters#ruby#rubocop#GetCommand(buffer) abort
|
2018-09-24 20:40:17 -04:00
|
|
|
let l:executable = ale#Var(a:buffer, 'ruby_rubocop_executable')
|
2018-03-31 10:55:20 -04:00
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
return ale#handlers#ruby#EscapeExecutable(l:executable, 'rubocop')
|
2018-03-31 10:55:20 -04:00
|
|
|
\ . ' --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
|
|
|
|
try
|
|
|
|
let l:errors = json_decode(a:lines[0])
|
|
|
|
catch
|
|
|
|
return []
|
|
|
|
endtry
|
|
|
|
|
|
|
|
if !has_key(l:errors, 'summary')
|
|
|
|
\|| 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,
|
|
|
|
\ 'code': l:error['cop_name'],
|
|
|
|
\ '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 is? 'convention'
|
|
|
|
\|| a:severity is? 'warning'
|
|
|
|
\|| a:severity is? 'refactor'
|
|
|
|
return 'W'
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 'E'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call ale#linter#Define('ruby', {
|
|
|
|
\ 'name': 'rubocop',
|
2018-09-24 20:40:17 -04:00
|
|
|
\ 'executable_callback': ale#VarFunc('ruby_rubocop_executable'),
|
2018-03-31 10:55:20 -04:00
|
|
|
\ 'command_callback': 'ale_linters#ruby#rubocop#GetCommand',
|
|
|
|
\ 'callback': 'ale_linters#ruby#rubocop#Handle',
|
|
|
|
\})
|