Skip to content

Commit 3ef8af6

Browse files
PsickOSSHNicolas SCHMAUCH
authored andcommitted
Add j2lint linter for Jinja2 templates (dense-analysis#5048)
Co-authored-by: Nicolas SCHMAUCH <nic.schmauch@i-0330135t.ac-bordeaux.fr>
1 parent 3817d0f commit 3ef8af6

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

ale_linters/jinja/j2lint.vim

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
" Description: linter for jinja using j2lint
2+
3+
call ale#Set('jinja_j2lint_executable', 'j2lint')
4+
call ale#Set('jinja_j2lint_options', '')
5+
call ale#Set('jinja_j2lint_use_global', get(g:, 'ale_use_global_executables', 0))
6+
call ale#Set('jinja_j2lint_auto_pipenv', 0)
7+
call ale#Set('jinja_j2lint_auto_poetry', 0)
8+
call ale#Set('jinja_j2lint_auto_uv', 0)
9+
10+
function! ale_linters#jinja#j2lint#GetExecutable(buffer) abort
11+
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'jinja_j2lint_auto_pipenv'))
12+
\ && ale#python#PipenvPresent(a:buffer)
13+
return 'pipenv'
14+
endif
15+
16+
if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'jinja_j2lint_auto_poetry'))
17+
\ && ale#python#PoetryPresent(a:buffer)
18+
return 'poetry'
19+
endif
20+
21+
if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'jinja_j2lint_auto_uv'))
22+
\ && ale#python#UvPresent(a:buffer)
23+
return 'uv'
24+
endif
25+
26+
return ale#python#FindExecutable(a:buffer, 'jinja_j2lint', ['j2lint'])
27+
endfunction
28+
29+
function! ale_linters#jinja#j2lint#GetCommand(buffer) abort
30+
let l:executable = ale_linters#jinja#j2lint#GetExecutable(a:buffer)
31+
32+
let l:exec_args = l:executable =~? 'pipenv\|poetry\|uv$'
33+
\ ? ' run j2lint'
34+
\ : ''
35+
36+
return ale#Escape(l:executable) . l:exec_args
37+
\ . ale#Pad(ale#Var(a:buffer, 'jinja_j2lint_options'))
38+
\ . ' %t'
39+
endfunction
40+
41+
call ale#linter#Define('jinja', {
42+
\ 'name': 'j2lint',
43+
\ 'executable': function('ale_linters#jinja#j2lint#GetExecutable'),
44+
\ 'command': function('ale_linters#jinja#j2lint#GetCommand'),
45+
\ 'callback': 'ale#handlers#unix#HandleAsError',
46+
\})

doc/ale-jinja.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ djlint *ale-jinja-djlint*
88

99
See |ale-html-djlint|
1010

11+
===============================================================================
12+
j2lint *ale-jinja-j2lint*
13+
14+
1115
===============================================================================
1216
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

doc/ale-supported-languages-and-tools.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Notes:
336336
* `xo`
337337
* Jinja
338338
* djlint
339+
* j2lint
339340
* JSON
340341
* `VSCode JSON language server`
341342
* `biome`

doc/ale.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,7 @@ documented in additional help files.
36343634
xo....................................|ale-javascript-xo|
36353635
jinja...................................|ale-jinja-options|
36363636
djlint................................|ale-jinja-djlint|
3637+
j2lint................................|ale-jinja-j2lint|
36373638
json....................................|ale-json-options|
36383639
biome.................................|ale-json-biome|
36393640
clang-format..........................|ale-json-clangformat|

supported-tools.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ formatting.
346346
* [xo](https://github.com/sindresorhus/xo)
347347
* Jinja
348348
* [djlint](https://djlint.com/)
349+
* [j2lint](https://github.com/aristanetworks/j2lint/)
349350
* JSON
350351
* [VSCode JSON language server](https://github.com/hrsh7th/vscode-langservers-extracted)
351352
* [biome](https://biomejs.dev/)

test/linter/test_j2lint.vader

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Before:
2+
call ale#assert#SetUpLinterTest('jinja', 'j2lint')
3+
4+
After:
5+
call ale#assert#TearDownLinterTest()
6+
7+
Execute(The j2lint executable should be configurable):
8+
let g:ale_jinja_j2lint_executable = '~/.local/bin/j2lint'
9+
10+
AssertLinter '~/.local/bin/j2lint',
11+
\ ale#Escape('~/.local/bin/j2lint'). ' %t'
12+
13+
Execute(Setting executable to 'pipenv' appends 'run j2lint'):
14+
let g:ale_jinja_j2lint_executable = 'path/to/pipenv'
15+
16+
AssertLinter 'path/to/pipenv',
17+
\ ale#Escape('path/to/pipenv') . ' run j2lint %t'
18+
19+
Execute(Pipenv is detected when jinja_j2lint_auto_pipenv is set):
20+
let g:ale_jinja_j2lint_auto_pipenv = 1
21+
call ale#test#SetFilename('../test-files/python/pipenv/whatever.py')
22+
23+
AssertLinter 'pipenv',
24+
\ ale#Escape('pipenv') . ' run j2lint %t'
25+
26+
Execute(Setting executable to 'poetry' appends 'run j2lint'):
27+
let g:ale_jinja_j2lint_executable = 'path/to/poetry'
28+
29+
AssertLinter 'path/to/poetry',
30+
\ ale#Escape('path/to/poetry') . ' run j2lint %t'
31+
32+
Execute(Poetry is detected when jinja_j2lint_auto_poetry is set):
33+
let g:ale_jinja_j2lint_auto_poetry = 1
34+
call ale#test#SetFilename('../test-files/python/poetry/whatever.py')
35+
36+
AssertLinter 'poetry',
37+
\ ale#Escape('poetry') . ' run j2lint %t'
38+
39+
Execute(uv is detected when jinja_j2lint_auto_uv is set):
40+
let g:ale_jinja_j2lint_auto_uv = 1
41+
call ale#test#SetFilename('../test-files/python/uv/whatever.py')
42+
43+
AssertLinter 'uv',
44+
\ ale#Escape('uv') . ' run j2lint %t'

0 commit comments

Comments
 (0)