Skip to content

Commit 59c996c

Browse files
authored
Support for Zlint as Zig linter (#4923)
* feat: Add Zig zlint linter and handler for ALE * docs: Add zlint documentation to ALE Zig integration guide * docs: Updating docs for zlint support * tests: Adding tests for checking zlint executable and command * refactor: Move zlint configuration test to separate test file
1 parent c4cedee commit 59c996c

File tree

7 files changed

+114
-1
lines changed

7 files changed

+114
-1
lines changed

ale_linters/zig/zlint.vim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
" Author: Don Isaac
2+
" Description: A linter for the Zig programming language
3+
4+
call ale#Set('zig_zlint_executable', 'zlint')
5+
6+
function! ale_linters#zig#zlint#Handle(buffer, lines) abort
7+
" GitHub Actions format: ::severity file=file,line=line,col=col,title=code::message
8+
let l:pattern = '::\([a-z]\+\) file=\([^,]\+\),line=\(\d\+\),col=\(\d\+\),title=\([^:]\+\)::\(.*\)'
9+
let l:output = []
10+
11+
for l:match in ale#util#GetMatches(a:lines, l:pattern)
12+
call add(l:output, {
13+
\ 'filename': l:match[2],
14+
\ 'lnum': str2nr(l:match[3]),
15+
\ 'col': str2nr(l:match[4]),
16+
\ 'text': l:match[6],
17+
\ 'type': l:match[1] =~? 'error\|fail' ? 'E' : 'W',
18+
\ 'code': l:match[5],
19+
\})
20+
endfor
21+
22+
return l:output
23+
endfunction
24+
25+
function! ale_linters#zig#zlint#GetCommand(buffer) abort
26+
return ale#Escape(ale#Var(a:buffer, 'zig_zlint_executable')) . ' %s -f gh'
27+
endfunction
28+
29+
call ale#linter#Define('zig', {
30+
\ 'name': 'zlint',
31+
\ 'executable': {b -> ale#Var(b, "zig_zlint_executable")},
32+
\ 'command': function('ale_linters#zig#zlint#GetCommand'),
33+
\ 'callback': 'ale_linters#zig#zlint#Handle',
34+
\})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ Notes:
758758
* `zeek`!!
759759
* Zig
760760
* `zigfmt`
761+
* `zlint`
761762
* `zls`
762763

763764
===============================================================================

doc/ale-zig.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ ALE Zig Integration *ale-zig-options*
55
===============================================================================
66
Integration Information
77

8-
Currently, the only supported linter for zig is zls.
8+
The following linters are supported for Zig:
9+
10+
* zlint (https://github.com/DonIsaac/zlint)
11+
* zls (https://github.com/zigtools/zls)
912

1013

1114
===============================================================================
@@ -19,6 +22,16 @@ g:ale_zig_zigfmt_executable *g:ale_zig_zigfmt_executable*
1922
The executable that will be run for the `zig fmt` fixer.
2023

2124

25+
===============================================================================
26+
zlint *ale-zig-zlint*
27+
28+
g:ale_zig_zlint_executable *g:ale_zig_zlint_executable*
29+
*b:ale_zig_zlint_executable*
30+
Type: |String|
31+
Default: `'zlint'`
32+
33+
This variable can be modified to change the executable path for `zlint`.
34+
2235
===============================================================================
2336
zls *ale-zig-zls*
2437

doc/ale.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3546,6 +3546,7 @@ documented in additional help files.
35463546
zeek..................................|ale-zeek-zeek|
35473547
zig.....................................|ale-zig-options|
35483548
zigfmt................................|ale-zig-zigfmt|
3549+
zlint.................................|ale-zig-zlint|
35493550
zls...................................|ale-zig-zls|
35503551

35513552

supported-tools.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,4 +767,5 @@ formatting.
767767
* [zeek](http://zeek.org) :floppy_disk:
768768
* Zig
769769
* [zigfmt](https://github.com/ziglang/zig)
770+
* [zlint](https://github.com/DonIsaac/zlint)
770771
* [zls](https://github.com/zigtools/zls)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Before:
2+
runtime ale_linters/zig/zlint.vim
3+
4+
After:
5+
call ale#linter#Reset()
6+
7+
Execute(The zlint handler should parse GitHub Actions format correctly):
8+
" Create a temporary buffer
9+
let buffer = bufnr('')
10+
11+
" Define input lines
12+
let input_lines = [
13+
\ '::warning file=test.zig,line=61,col=47,title=unsafe-undefined::`undefined` is missing a safety comment',
14+
\ '',
15+
\ '::error file=test2.zig,line=4,col=33,title=no-unresolved::Unresolved import to ''test3.zig''',
16+
\ '',
17+
\ ]
18+
19+
" Define expected output
20+
let expected_output = [
21+
\ {
22+
\ 'filename': 'test.zig',
23+
\ 'lnum': 61,
24+
\ 'col': 47,
25+
\ 'text': '`undefined` is missing a safety comment',
26+
\ 'type': 'W',
27+
\ 'code': 'unsafe-undefined'
28+
\ },
29+
\ {
30+
\ 'filename': 'test2.zig',
31+
\ 'lnum': 4,
32+
\ 'col': 33,
33+
\ 'text': 'Unresolved import to ''test3.zig''',
34+
\ 'type': 'E',
35+
\ 'code': 'no-unresolved'
36+
\ },
37+
\ ]
38+
39+
" Get actual output
40+
let actual_output = ale_linters#zig#zlint#Handle(buffer, input_lines)
41+
42+
" Assert equality
43+
AssertEqual expected_output, actual_output
44+

test/linter/test_zig_zlint.vader

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Before:
2+
call ale#assert#SetUpLinterTest('zig', 'zlint')
3+
4+
After:
5+
call ale#assert#TearDownLinterTest()
6+
7+
Execute(The zlint executable and command should be configured correctly):
8+
" Set a custom executable path
9+
let g:ale_zig_zlint_executable = '/custom/path/to/zlint'
10+
11+
" Create a buffer with Zig filetype
12+
call ale#test#SetFilename('test.zig')
13+
14+
" Check the executable
15+
AssertEqual '/custom/path/to/zlint', ale#Var(bufnr(''), 'zig_zlint_executable')
16+
17+
" Check the command
18+
let cmd = ale_linters#zig#zlint#GetCommand(bufnr(''))
19+
AssertEqual ale#Escape('/custom/path/to/zlint') . ' %s -f gh', cmd

0 commit comments

Comments
 (0)