summaryrefslogtreecommitdiff
path: root/ale_linters/ansible/ansible_lint.vim
diff options
context:
space:
mode:
authorserapas <vytautas.macionis@manomail.de>2021-02-28 13:43:23 +0100
committerGitHub <noreply@github.com>2021-02-28 21:43:23 +0900
commit038e4a8c31feb03beea02c09d953affc8c460515 (patch)
tree4470f633a25e7fda68d9dca34c10d7ba8c1bf6cf /ale_linters/ansible/ansible_lint.vim
parent76965615558e9398ef4cc79991632a7b68a6c7bc (diff)
downloadale-038e4a8c31feb03beea02c09d953affc8c460515.zip
Fix ansible-lint linter definition. (#3601)
* Fix ansible-lint linter definition. Use ansible-lint's feature auto-detection instead of temporary file. For auto-detection to work, ansible project has to be also a git repository. Don't use yaml rules. These are checked by yamllint. Refactor pattern to work with ansible-lint >=5.0 version. Clean-up obsolete test cases. * Pull Request changes
Diffstat (limited to 'ale_linters/ansible/ansible_lint.vim')
-rw-r--r--ale_linters/ansible/ansible_lint.vim97
1 files changed, 72 insertions, 25 deletions
diff --git a/ale_linters/ansible/ansible_lint.vim b/ale_linters/ansible/ansible_lint.vim
index c4affa31..f83de01a 100644
--- a/ale_linters/ansible/ansible_lint.vim
+++ b/ale_linters/ansible/ansible_lint.vim
@@ -1,4 +1,4 @@
-" Author: Bjorn Neergaard <bjorn@neersighted.com>
+" Authors: Bjorn Neergaard <bjorn@neersighted.com>, Vytautas Macionis <vytautas.macionis@manomail.de>
" Description: ansible-lint for ansible-yaml files
call ale#Set('ansible_ansible_lint_executable', 'ansible-lint')
@@ -7,7 +7,7 @@ function! ale_linters#ansible#ansible_lint#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'ansible_ansible_lint_executable')
endfunction
-function! ale_linters#ansible#ansible_lint#Handle(buffer, lines) abort
+function! ale_linters#ansible#ansible_lint#Handle(buffer, version, lines) abort
for l:line in a:lines[:10]
if match(l:line, '^Traceback') >= 0
return [{
@@ -18,39 +18,86 @@ function! ale_linters#ansible#ansible_lint#Handle(buffer, lines) abort
endif
endfor
- " Matches patterns line the following:
- "
- " test.yml:35: [EANSIBLE0002] Trailing whitespace
- let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: \[?([[:alnum:]]+)\]? (.*)$'
+ let l:version_group = ale#semver#GTE(a:version, [5, 0, 0]) ? '>=5.0.0' : '<5.0.0'
let l:output = []
- for l:match in ale#util#GetMatches(a:lines, l:pattern)
- let l:code = l:match[4]
+ if '>=5.0.0' is# l:version_group
+ " Matches patterns line the following:
+ " test.yml:3:148: syntax-check 'var' is not a valid attribute for a Play
+ " roles/test/tasks/test.yml:8: [package-latest] [VERY_LOW] Package installs should not use latest
+ " D:\test\tasks\test.yml:8: [package-latest] [VERY_LOW] package installs should not use latest
+ let l:pattern = '\v^(%([a-zA-Z]:)?[^:]+):(\d+):%((\d+):)? %(\[([-[:alnum:]]+)\]) %(\[([_[:alnum:]]+)\]) (.*)$'
+ let l:error_codes = { 'VERY_HIGH': 'E', 'HIGH': 'E', 'MEDIUM': 'W', 'LOW': 'W', 'VERY_LOW': 'W', 'INFO': 'I' }
- if l:code is# 'EANSIBLE0002'
- \&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
- " Skip warnings for trailing whitespace if the option is off.
- continue
- endif
+ for l:match in ale#util#GetMatches(a:lines, l:pattern)
+ if ale#path#IsBufferPath(a:buffer, l:match[1])
+ call add(l:output, {
+ \ 'lnum': l:match[2] + 0,
+ \ 'col': l:match[3] + 0,
+ \ 'text': l:match[6],
+ \ 'code': l:match[4],
+ \ 'type': l:error_codes[l:match[5]],
+ \})
+ endif
+ endfor
+ endif
- if ale#path#IsBufferPath(a:buffer, l:match[1])
- call add(l:output, {
- \ 'lnum': l:match[2] + 0,
- \ 'col': l:match[3] + 0,
- \ 'text': l:match[5],
- \ 'code': l:code,
- \ 'type': l:code[:0] is# 'E' ? 'E' : 'W',
- \})
- endif
- endfor
+ if '<5.0.0' is# l:version_group
+ " Matches patterns line the following:
+ " test.yml:35: [EANSIBLE0002] Trailing whitespace
+ let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: \[?([[:alnum:]]+)\]? (.*)$'
+
+ for l:match in ale#util#GetMatches(a:lines, l:pattern)
+ let l:code = l:match[4]
+
+ if l:code is# 'EANSIBLE0002'
+ \&& !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
+ " Skip warnings for trailing whitespace if the option is off.
+ continue
+ endif
+
+ if ale#path#IsBufferPath(a:buffer, l:match[1])
+ call add(l:output, {
+ \ 'lnum': l:match[2] + 0,
+ \ 'col': l:match[3] + 0,
+ \ 'text': l:match[5],
+ \ 'code': l:code,
+ \ 'type': l:code[:0] is# 'E' ? 'E' : 'W',
+ \})
+ endif
+ endfor
+ endif
return l:output
endfunction
+function! ale_linters#ansible#ansible_lint#GetCommand(buffer, version) abort
+ let l:commands = {
+ \ '>=5.0.0': '%e --parseable-severity -x yaml',
+ \ '<5.0.0': '%e -p %t'
+ \}
+ let l:command = ale#semver#GTE(a:version, [5, 0]) ? l:commands['>=5.0.0'] : l:commands['<5.0.0']
+
+ return l:command
+endfunction
+
call ale#linter#Define('ansible', {
\ 'name': 'ansible_lint',
\ 'aliases': ['ansible', 'ansible-lint'],
\ 'executable': function('ale_linters#ansible#ansible_lint#GetExecutable'),
-\ 'command': '%e -p %t',
-\ 'callback': 'ale_linters#ansible#ansible_lint#Handle',
+\ 'command': {buffer -> ale#semver#RunWithVersionCheck(
+\ buffer,
+\ ale_linters#ansible#ansible_lint#GetExecutable(buffer),
+\ '%e --version',
+\ function('ale_linters#ansible#ansible_lint#GetCommand'),
+\ )},
+\ 'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
+\ buffer,
+\ ale_linters#ansible#ansible_lint#GetExecutable(buffer),
+\ '%e --version',
+\ {buffer, version -> ale_linters#ansible#ansible_lint#Handle(
+\ buffer,
+\ l:version,
+\ lines)},
+\ )},
\})