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.
1 " Authors: Bjorn Neergaard <bjorn@neersighted.com>, Vytautas Macionis <vytautas.macionis@manomail.de>
2 " Description: ansible-lint for ansible-yaml files
4 call ale#Set('ansible_ansible_lint_executable', 'ansible-lint')
6 function! ale_linters#ansible#ansible_lint#GetExecutable(buffer) abort
7 return ale#Var(a:buffer, 'ansible_ansible_lint_executable')
10 function! ale_linters#ansible#ansible_lint#Handle(buffer, version, lines) abort
11 for l:line in a:lines[:10]
12 if match(l:line, '^Traceback') >= 0
15 \ 'text': 'An exception was thrown. See :ALEDetail',
16 \ 'detail': join(a:lines, "\n"),
21 let l:version_group = ale#semver#GTE(a:version, [6, 0, 0]) ? '>=6.0.0' :
22 \ ale#semver#GTE(a:version, [5, 0, 0]) ? '>=5.0.0' :
26 if '>=6.0.0' is# l:version_group
27 let l:error_codes = { 'blocker': 'E', 'critical': 'E', 'major': 'W', 'minor': 'W', 'info': 'I' }
28 let l:linter_issues = ale#util#FuzzyJSONDecode(a:lines, [])
30 for l:issue in l:linter_issues
31 if ale#path#IsBufferPath(a:buffer, l:issue.location.path)
32 if exists('l:issue.location.positions')
33 let l:coord_keyname = 'positions'
35 let l:coord_keyname = 'lines'
38 let l:column_member = printf(
39 \ 'l:issue.location.%s.begin.column', l:coord_keyname
43 \ 'lnum': exists(l:column_member) ? l:issue.location[l:coord_keyname].begin.line :
44 \ l:issue.location[l:coord_keyname].begin,
45 \ 'col': exists(l:column_member) ? l:issue.location[l:coord_keyname].begin.column : 0,
46 \ 'text': l:issue.check_name,
47 \ 'detail': l:issue.description,
48 \ 'code': l:issue.severity,
49 \ 'type': l:error_codes[l:issue.severity],
55 if '>=5.0.0' is# l:version_group
56 " Matches patterns line the following:
57 " test.yml:3:148: syntax-check 'var' is not a valid attribute for a Play
58 " roles/test/tasks/test.yml:8: [package-latest] [VERY_LOW] Package installs should not use latest
59 " D:\test\tasks\test.yml:8: [package-latest] [VERY_LOW] package installs should not use latest
60 let l:pattern = '\v^(%([a-zA-Z]:)?[^:]+):(\d+):%((\d+):)? %(\[([-[:alnum:]]+)\]) %(\[([_[:alnum:]]+)\]) (.*)$'
61 let l:error_codes = { 'VERY_HIGH': 'E', 'HIGH': 'E', 'MEDIUM': 'W', 'LOW': 'W', 'VERY_LOW': 'W', 'INFO': 'I' }
63 for l:match in ale#util#GetMatches(a:lines, l:pattern)
64 if ale#path#IsBufferPath(a:buffer, l:match[1])
66 \ 'lnum': l:match[2] + 0,
67 \ 'col': l:match[3] + 0,
70 \ 'type': l:error_codes[l:match[5]],
76 if '<5.0.0' is# l:version_group
77 " Matches patterns line the following:
78 " test.yml:35: [EANSIBLE0002] Trailing whitespace
79 let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: \[?([[:alnum:]]+)\]? (.*)$'
81 for l:match in ale#util#GetMatches(a:lines, l:pattern)
82 let l:code = l:match[4]
84 if l:code is# 'EANSIBLE0002'
85 \&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
86 " Skip warnings for trailing whitespace if the option is off.
90 if ale#path#IsBufferPath(a:buffer, l:match[1])
92 \ 'lnum': l:match[2] + 0,
93 \ 'col': l:match[3] + 0,
96 \ 'type': l:code[:0] is# 'E' ? 'E' : 'W',
105 function! ale_linters#ansible#ansible_lint#GetCommand(buffer, version) abort
107 \ '>=6.0.0': '%e --nocolor -f json -x yaml %s',
108 \ '>=5.0.0': '%e --nocolor --parseable-severity -x yaml %s',
109 \ '<5.0.0': '%e --nocolor -p %t'
111 let l:command = ale#semver#GTE(a:version, [6, 0]) ? l:commands['>=6.0.0'] :
112 \ ale#semver#GTE(a:version, [5, 0]) ? l:commands['>=5.0.0'] :
113 \ l:commands['<5.0.0']
118 call ale#linter#Define('ansible', {
119 \ 'name': 'ansible_lint',
120 \ 'aliases': ['ansible', 'ansible-lint'],
121 \ 'executable': function('ale_linters#ansible#ansible_lint#GetExecutable'),
122 \ 'command': {buffer -> ale#semver#RunWithVersionCheck(
124 \ ale_linters#ansible#ansible_lint#GetExecutable(buffer),
126 \ function('ale_linters#ansible#ansible_lint#GetCommand'),
129 \ 'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
131 \ ale_linters#ansible#ansible_lint#GetExecutable(buffer),
133 \ {buffer, version -> ale_linters#ansible#ansible_lint#Handle(