summaryrefslogtreecommitdiff
path: root/ale_linters/chef/cookstyle.vim
blob: 50bae2aaa61213329037fc2a61fed16934f677c3 (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
" Author: Raphael Hoegger - https://github.com/pfuender
" Description: Cookstyle (RuboCop based), a code style analyzer for Ruby files

call ale#Set('chef_cookstyle_executable', 'cookstyle')
call ale#Set('chef_cookstyle_options', '')

function! ale_linters#chef#cookstyle#GetCommand(buffer) abort
    let l:options = ale#Var(a:buffer, 'chef_cookstyle_options')

    return '%e' . ale#Pad(escape(l:options, '~')) . ' --force-exclusion --format json --stdin ' . ' %s'
endfunction

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

    let l:errors = ale#util#FuzzyJSONDecode(a:lines[0], {})

    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 = str2nr(l:error['location']['start_column'])
        let l:end_col = str2nr(l:error['location']['last_column'])

        if !l:end_col
            let l:end_col = l:start_col + 1
        endif

        call add(l:output, {
        \   'lnum': str2nr(l:error['location']['line']),
        \   'col': l:start_col,
        \   'end_col': l:end_col,
        \   'code': l:error['cop_name'],
        \   'text': l:error['message'],
        \   'type': l:error['severity'] is? 'convention' ? 'W' : 'E',
        \})
    endfor

    return l:output
endfunction

call ale#linter#Define('chef', {
\   'name': 'cookstyle',
\   'executable': {b -> ale#Var(b, 'chef_cookstyle_executable')},
\   'command': function('ale_linters#chef#cookstyle#GetCommand'),
\   'callback': 'ale_linters#chef#cookstyle#Handle',
\})