]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/terraform/tflint.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit '76265755a1add77121c8f9dabb3e9bb70fe9a972' as '.vim/bundle/ale'
[etc/vim.git] / .vim / bundle / ale / ale_linters / terraform / tflint.vim
1 " Author: Nat Williams <nat.williams@gmail.com>
2 " Description: tflint for Terraform files
3 "
4 " See: https://www.terraform.io/
5 "      https://github.com/wata727/tflint
6
7 call ale#Set('terraform_tflint_options', '')
8 call ale#Set('terraform_tflint_executable', 'tflint')
9
10 function! ale_linters#terraform#tflint#Handle(buffer, lines) abort
11     let l:output = []
12     let l:pattern = '\v^(.*):(\d+),(\d+)-(\d+)?,?(\d+): (.{-1,}); (.+)$'
13     let l:json = ale#util#FuzzyJSONDecode(a:lines, {})
14
15     " This is a rough test for tflint's output format
16     " On versions prior to 0.11 it outputs all errors as a single level list
17     if type(l:json) is v:t_list
18         for l:error in l:json
19             if l:error.type is# 'ERROR'
20                 let l:type = 'E'
21             elseif l:error.type is# 'NOTICE'
22                 let l:type = 'I'
23             else
24                 let l:type = 'W'
25             endif
26
27             call add(l:output, {
28             \   'lnum': l:error.line,
29             \   'text': l:error.message,
30             \   'type': l:type,
31             \   'code': l:error.detector,
32             \})
33         endfor
34     else
35         for l:error in get(l:json, 'errors', [])
36             for l:match in ale#util#GetMatches(l:error.message, [l:pattern])
37                 if l:match[4] is# ''
38                     let l:match[4] = l:match[2]
39                 endif
40
41                 call add(l:output, {
42                 \   'filename': l:match[1],
43                 \   'lnum': str2nr(l:match[2]),
44                 \   'col': str2nr(l:match[3]),
45                 \   'end_lnum': str2nr(l:match[4]),
46                 \   'end_col': str2nr(l:match[5]),
47                 \   'text': l:match[7],
48                 \   'code': l:match[6],
49                 \   'type': 'E',
50                 \})
51             endfor
52         endfor
53
54         for l:error in get(l:json, 'issues', [])
55             if l:error.rule.severity is# 'ERROR'
56                 let l:type = 'E'
57             elseif l:error.rule.severity is# 'NOTICE'
58                 let l:type = 'I'
59             else
60                 let l:type = 'W'
61             endif
62
63             call add(l:output, {
64             \   'filename': l:error.range.filename,
65             \   'lnum': l:error.range.start.line,
66             \   'col': l:error.range.start.column,
67             \   'end_lnum': l:error.range.end.line,
68             \   'end_col': l:error.range.end.column,
69             \   'text': l:error.message,
70             \   'code': l:error.rule.name,
71             \   'type': l:type,
72             \})
73         endfor
74     endif
75
76     return l:output
77 endfunction
78
79 function! ale_linters#terraform#tflint#GetCommand(buffer) abort
80     let l:cmd = '%e'
81
82     let l:config_file = ale#path#FindNearestFile(a:buffer, '.tflint.hcl')
83
84     if !empty(l:config_file)
85         let l:cmd .= ' --config ' . ale#Escape(l:config_file)
86     endif
87
88     let l:opts = ale#Var(a:buffer, 'terraform_tflint_options')
89
90     if !empty(l:opts)
91         let l:cmd .= ' ' . l:opts
92     endif
93
94     let l:cmd .= ' -f json'
95
96     return l:cmd
97 endfunction
98
99 call ale#linter#Define('terraform', {
100 \   'name': 'tflint',
101 \   'executable': {b -> ale#Var(b, 'terraform_tflint_executable')},
102 \   'cwd': '%s:h',
103 \   'command': function('ale_linters#terraform#tflint#GetCommand'),
104 \   'callback': 'ale_linters#terraform#tflint#Handle',
105 \})