Skip to content

Commit a21201f

Browse files
committed
astgrep: add a fixer
1 parent e6face6 commit a21201f

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

autoload/ale/fix/registry.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ let s:default_registry = {
2222
\ 'suggested_filetypes': ['apkbuild'],
2323
\ 'description': 'Fix policy violations found by apkbuild-lint in APKBUILDs',
2424
\ },
25+
\ 'ast-grep': {
26+
\ 'function': 'ale#fixers#astgrep#Fix',
27+
\ 'suggested_filetypes': [],
28+
\ 'description': 'Apply ast-grep rules.',
29+
\ },
2530
\ 'autoimport': {
2631
\ 'function': 'ale#fixers#autoimport#Fix',
2732
\ 'suggested_filetypes': ['python'],

autoload/ale/fixers/astgrep.vim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
" Author: Ben Boeckel <[email protected]>
2+
" Description: Fix files with `ast-grep`.
3+
4+
call ale#Set('astgrep_executable', 'ast-grep')
5+
call ale#Set('astgrep_scan_options', '--update-all')
6+
7+
function! ale#fixers#astgrep#Fix(buffer) abort
8+
let l:executable = ale#Var(a:buffer, 'astgrep_executable')
9+
let l:options = ale#Var(a:buffer, 'astgrep_scan_options')
10+
11+
return {
12+
\ 'command': ale#Escape(l:executable)
13+
\ . ' scan'
14+
\ . (empty(l:options) ? '' : ' ' . l:options)
15+
\ . ' %t',
16+
\ 'read_temporary_file': 1,
17+
\}
18+
endfunction

doc/ale-generic.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,17 @@ g:ale_astgrep_linter_languages
3838
`ast-grep` linter.
3939

4040

41+
*ale-options.astgrep_scan_options*
42+
*g:ale_astgrep_scan_options*
43+
*b:ale_astgrep_scan_options*
44+
astgrep_scan_options
45+
g:ale_astgrep_scan_options
46+
Type: |String|
47+
Default: `'--update-all'`
48+
49+
This variable can be modified to change the options when applying rules with
50+
the `ast-grep scan` command.
51+
52+
4153
===============================================================================
4254
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Before:
2+
Save g:ale_astgrep_executable
3+
Save g:ale_astgrep_scan_options
4+
5+
After:
6+
Restore
7+
8+
Execute(The ast-grep callback should return the correct default values):
9+
AssertEqual
10+
\ {
11+
\ 'command': ale#Escape('ast-grep') . ' scan --update-all %t',
12+
\ 'read_temporary_file': 1,
13+
\ },
14+
\ ale#fixers#astgrep#Fix(bufnr(''))
15+
16+
Execute(The astgrep executable and options should be configurable):
17+
let g:ale_astgrep_executable = '/path/to/ast-grep'
18+
let g:ale_astgrep_scan_options = '--interactive'
19+
20+
AssertEqual
21+
\ {
22+
\ 'command': ale#Escape('/path/to/ast-grep')
23+
\ . ' scan'
24+
\ . ' --interactive %t',
25+
\ 'read_temporary_file': 1,
26+
\ },
27+
\ ale#fixers#astgrep#Fix(bufnr(''))

0 commit comments

Comments
 (0)