summaryrefslogtreecommitdiff
path: root/ale_linters/ruby/rubocop.vim
diff options
context:
space:
mode:
authorEddie Lebow <elebow@users.noreply.github.com>2017-07-09 10:48:04 -0400
committerw0rp <w0rp@users.noreply.github.com>2017-07-09 15:48:04 +0100
commitab0e76dbd50dbe5bae08817baa56432889335b99 (patch)
treeacb7f0999e71e800921581a8feea7b1720574840 /ale_linters/ruby/rubocop.vim
parent836a2cfe3b5ba2761291564cbf412ca79f9a52a9 (diff)
downloadale-ab0e76dbd50dbe5bae08817baa56432889335b99.zip
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
Diffstat (limited to 'ale_linters/ruby/rubocop.vim')
-rw-r--r--ale_linters/ruby/rubocop.vim49
1 files changed, 33 insertions, 16 deletions
diff --git a/ale_linters/ruby/rubocop.vim b/ale_linters/ruby/rubocop.vim
index 9b77f896..1756f450 100644
--- a/ale_linters/ruby/rubocop.vim
+++ b/ale_linters/ruby/rubocop.vim
@@ -1,5 +1,5 @@
-" Author: ynonp - https://github.com/ynonp
-" Description: rubocop for Ruby files
+" 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)
@@ -8,34 +8,51 @@ function! ale_linters#ruby#rubocop#GetCommand(buffer) abort
\ : ''
return ale#Escape(l:executable) . l:exec_args
- \ . ' --format emacs --force-exclusion '
+ \ . ' --format json --force-exclusion '
\ . ale#Var(a:buffer, 'ruby_rubocop_options')
\ . ' --stdin ' . bufname(a:buffer)
endfunction
function! ale_linters#ruby#rubocop#Handle(buffer, lines) abort
- " Matches patterns line the following:
- "
- " <path>:83:29: C: Prefer single-quoted strings when you don't
- " need string interpolation or special symbols.
- let l:pattern = '\v:(\d+):(\d+): (.): (.+)'
- let l:output = []
+ if len(a:lines) == 0
+ return []
+ endif
+
+ let l:errors = json_decode(a:lines[0])
- for l:match in ale#util#GetMatches(a:lines, l:pattern)
- let l:text = l:match[4]
- let l:type = l:match[3]
+ 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:match[1] + 0,
- \ 'col': l:match[2] + 0,
- \ 'text': l:text,
- \ 'type': index(['F', 'E'], l:type) != -1 ? 'E' : 'W',
+ \ '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 ==? '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', {
\ 'name': 'rubocop',
\ 'executable_callback': 'ale#handlers#rubocop#GetExecutable',