summaryrefslogtreecommitdiff
path: root/ale_linters/eruby/ruumba.vim
blob: f415f1ab3f534a7acbeb5fdd3562e7da638c2fad (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
62
" Author: aclemons - https://github.com/aclemons
" based on the ale rubocop linter
" Description: Ruumba, RuboCop linting for ERB templates.

call ale#Set('eruby_ruumba_executable', 'ruumba')
call ale#Set('eruby_ruumba_options', '')

function! ale_linters#eruby#ruumba#GetCommand(buffer) abort
    let l:executable = ale#Var(a:buffer, 'eruby_ruumba_executable')

    return ale#ruby#EscapeExecutable(l:executable, 'ruumba')
    \   . ' --format json --force-exclusion '
    \   . ale#Var(a:buffer, 'eruby_ruumba_options')
    \   . ' --stdin %s'
endfunction

function! ale_linters#eruby#ruumba#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#eruby#ruumba#GetType(l:error['severity']),
        \})
    endfor

    return l:output
endfunction

function! ale_linters#eruby#ruumba#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('eruby', {
\   'name': 'ruumba',
\   'executable': {b -> ale#Var(b, 'eruby_ruumba_executable')},
\   'command': function('ale_linters#eruby#ruumba#GetCommand'),
\   'callback': 'ale_linters#eruby#ruumba#Handle',
\})