From 4894c760c25e9f436a071ceb80a538fb0b23b306 Mon Sep 17 00:00:00 2001 From: jD91mZM2 Date: Sun, 31 Jan 2021 13:01:03 +0100 Subject: [PATCH 1/3] Add jq linter for JSON diagnostics --- ale_linters/json/jq.vim | 34 ++++++++++++++++++++++++++++++++++ test/test_jq_linter.vader | 15 +++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 ale_linters/json/jq.vim create mode 100644 test/test_jq_linter.vader diff --git a/ale_linters/json/jq.vim b/ale_linters/json/jq.vim new file mode 100644 index 00000000..4cce0e98 --- /dev/null +++ b/ale_linters/json/jq.vim @@ -0,0 +1,34 @@ +" Author: jD91mZM2 + +call ale#Set('json_jq_executable', 'jq') + +function! ale_linters#json#jq#GetCommand(buffer) abort + let l:executable = ale#Var(a:buffer, 'json_jq_executable') + + return ale#Var(a:buffer, 'json_jq_executable') +endfunction + +function! ale_linters#json#jq#Handle(buffer, lines) abort + " Matches patterns like the following: + " parse error: Expected another key-value pair at line 4, column 3 + let l:pattern = '^parse error: \(.\+\) at line \(\d\+\), column \(\d\+\)$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + call add(l:output, { + \ 'text': l:match[1], + \ 'lnum': l:match[2] + 0, + \ 'col': l:match[3] + 0, + \}) + endfor + + return l:output +endfunction + +call ale#linter#Define('json', { +\ 'name': 'jq', +\ 'executable': { b -> ale#Var(b, 'json_jq_executable') }, +\ 'output_stream': 'stderr', +\ 'command': function('ale_linters#json#jq#GetCommand'), +\ 'callback': 'ale_linters#json#jq#Handle', +\}) diff --git a/test/test_jq_linter.vader b/test/test_jq_linter.vader new file mode 100644 index 00000000..e702f5a3 --- /dev/null +++ b/test/test_jq_linter.vader @@ -0,0 +1,15 @@ +Before: + runtime ale_linters/json/jq.vim + +Execute (Should parse error correctly): + AssertEqual + \ [ + \ { + \ 'lnum': 1, + \ 'col': 9, + \ 'text': 'Expected another array element', + \ } + \ ], + \ ale_linters#json#jq#Handle(0, [ + \ 'parse error: Expected another array element at line 1, column 9' + \ ]) From 98caa19cc76ee1d98121b3dbe9158374cf00f2d7 Mon Sep 17 00:00:00 2001 From: jD91mZM2 Date: Tue, 2 Feb 2021 13:01:03 +0100 Subject: [PATCH 2/3] Add command callback test --- ale_linters/json/jq.vim | 2 +- test/command_callback/test_jq_command_callback.vader | 8 ++++++++ test/test_jq_linter.vader | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test/command_callback/test_jq_command_callback.vader diff --git a/ale_linters/json/jq.vim b/ale_linters/json/jq.vim index 4cce0e98..777b65c6 100644 --- a/ale_linters/json/jq.vim +++ b/ale_linters/json/jq.vim @@ -5,7 +5,7 @@ call ale#Set('json_jq_executable', 'jq') function! ale_linters#json#jq#GetCommand(buffer) abort let l:executable = ale#Var(a:buffer, 'json_jq_executable') - return ale#Var(a:buffer, 'json_jq_executable') + return ale#Escape(l:executable) endfunction function! ale_linters#json#jq#Handle(buffer, lines) abort diff --git a/test/command_callback/test_jq_command_callback.vader b/test/command_callback/test_jq_command_callback.vader new file mode 100644 index 00000000..20c3db5b --- /dev/null +++ b/test/command_callback/test_jq_command_callback.vader @@ -0,0 +1,8 @@ +Before: + call ale#assert#SetUpLinterTest('json', 'jq') + +After: + call ale#assert#TearDownLinterTest() + +Execute(The default command should be correct): + AssertLinter 'jq', ale#Escape('jq') diff --git a/test/test_jq_linter.vader b/test/test_jq_linter.vader index e702f5a3..cbe23b96 100644 --- a/test/test_jq_linter.vader +++ b/test/test_jq_linter.vader @@ -1,6 +1,9 @@ Before: runtime ale_linters/json/jq.vim +After: + call ale#linter#Reset() + Execute (Should parse error correctly): AssertEqual \ [ From 27130efc655dcba17c84cd4f8d30b6985c574441 Mon Sep 17 00:00:00 2001 From: jD91mZM2 Date: Sat, 6 Feb 2021 11:06:08 +0100 Subject: [PATCH 3/3] Reuse ale#fixers#jq#GetExecutable for jq --- ale_linters/json/jq.vim | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ale_linters/json/jq.vim b/ale_linters/json/jq.vim index 777b65c6..dd6ed0a6 100644 --- a/ale_linters/json/jq.vim +++ b/ale_linters/json/jq.vim @@ -1,9 +1,7 @@ " Author: jD91mZM2 -call ale#Set('json_jq_executable', 'jq') - function! ale_linters#json#jq#GetCommand(buffer) abort - let l:executable = ale#Var(a:buffer, 'json_jq_executable') + let l:executable = ale#fixers#jq#GetExecutable(a:buffer) return ale#Escape(l:executable) endfunction @@ -27,7 +25,7 @@ endfunction call ale#linter#Define('json', { \ 'name': 'jq', -\ 'executable': { b -> ale#Var(b, 'json_jq_executable') }, +\ 'executable': function('ale#fixers#jq#GetExecutable'), \ 'output_stream': 'stderr', \ 'command': function('ale_linters#json#jq#GetCommand'), \ 'callback': 'ale_linters#json#jq#Handle',