summaryrefslogtreecommitdiff
path: root/ale_linters/ruby/rubocop.vim
blob: 1756f450ddd52b3f1aec312652cf950e83271860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
" 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 ' . bufname(a:buffer)
endfunction

function! ale_linters#ruby#rubocop#Handle(buffer, lines) abort
    if len(a:lines) == 0
      return []
    endif

    let l:errors = json_decode(a:lines[0])

    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 ==? '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',
\   'command_callback': 'ale_linters#ruby#rubocop#GetCommand',
\   'callback': 'ale_linters#ruby#rubocop#Handle',
\})