Implement support for SQL linter sqlfluff (#4361)
This commit is contained in:
parent
ca355f4cb4
commit
6c4be47437
|
@ -0,0 +1,66 @@
|
|||
" Author: Carl Smedstad <carl.smedstad at protonmail dot com>
|
||||
" Description: sqlfluff for SQL files
|
||||
|
||||
let g:ale_sql_sqlfluff_executable =
|
||||
\ get(g:, 'ale_sql_sqlfluff_executable', 'sqlfluff')
|
||||
|
||||
let g:ale_sql_sqlfluff_options =
|
||||
\ get(g:, 'ale_sql_sqlfluff_options', '')
|
||||
|
||||
function! ale_linters#sql#sqlfluff#Executable(buffer) abort
|
||||
return ale#Var(a:buffer, 'sql_sqlfluff_executable')
|
||||
endfunction
|
||||
|
||||
function! ale_linters#sql#sqlfluff#Command(buffer) abort
|
||||
let l:executable = ale_linters#sql#sqlfluff#Executable(a:buffer)
|
||||
let l:options = ale#Var(a:buffer, 'sql_sqlfluff_options')
|
||||
|
||||
let l:cmd =
|
||||
\ ale#Escape(l:executable)
|
||||
\ . ' lint'
|
||||
|
||||
let l:config_file = ale#path#FindNearestFile(a:buffer, '.sqlfluff')
|
||||
|
||||
if !empty(l:config_file)
|
||||
let l:cmd .= ' --config ' . ale#Escape(l:config_file)
|
||||
else
|
||||
let l:cmd .= ' --dialect ansi'
|
||||
endif
|
||||
|
||||
let l:cmd .=
|
||||
\ ' --format json '
|
||||
\ . l:options
|
||||
\ . ' %t'
|
||||
|
||||
return l:cmd
|
||||
endfunction
|
||||
|
||||
function! ale_linters#sql#sqlfluff#Handle(buffer, lines) abort
|
||||
let l:output = []
|
||||
let l:json = ale#util#FuzzyJSONDecode(a:lines, {})[0]
|
||||
|
||||
" if there's no warning, 'result' is `null`.
|
||||
if empty(get(l:json, 'violations'))
|
||||
return l:output
|
||||
endif
|
||||
|
||||
for l:violation in get(l:json, 'violations', [])
|
||||
call add(l:output, {
|
||||
\ 'filename': l:json.filepath,
|
||||
\ 'lnum': l:violation.line_no,
|
||||
\ 'col': l:violation.line_pos,
|
||||
\ 'text': l:violation.description,
|
||||
\ 'code': l:violation.code,
|
||||
\ 'type': 'W',
|
||||
\})
|
||||
endfor
|
||||
|
||||
return l:output
|
||||
endfunction
|
||||
|
||||
call ale#linter#Define('sql', {
|
||||
\ 'name': 'sqlfluff',
|
||||
\ 'executable': function('ale_linters#sql#sqlfluff#Executable'),
|
||||
\ 'command': function('ale_linters#sql#sqlfluff#Command'),
|
||||
\ 'callback': 'ale_linters#sql#sqlfluff#Handle',
|
||||
\})
|
|
@ -27,6 +27,27 @@ g:ale_sql_pgformatter_options *g:ale_sql_pgformatter_options*
|
|||
This variable can be set to pass additional options to the pgformatter fixer.
|
||||
|
||||
|
||||
===============================================================================
|
||||
sqlfluff *ale-sql-sqlfluff*
|
||||
|
||||
g:ale_sql_sqlfluff_executable *g:ale_sql_sqlfluff_executable*
|
||||
*b:ale_sql_sqlfluff_executable*
|
||||
Type: |String|
|
||||
Default: `'sqlfluff'`
|
||||
|
||||
This variable sets executable used for sqlfluff.
|
||||
|
||||
g:ale_sql_sqlfluff_options *g:ale_sql_sqlfluff_options*
|
||||
*b:ale_sql_sqlfluff_options*
|
||||
Type: |String|
|
||||
Default: `''`
|
||||
|
||||
This variable can be set to pass additional options to the sqlfluff linter.
|
||||
|
||||
|
||||
===============================================================================
|
||||
|
||||
|
||||
===============================================================================
|
||||
sqlfmt *ale-sql-sqlfmt*
|
||||
|
||||
|
|
|
@ -573,6 +573,7 @@ Notes:
|
|||
* `dprint`
|
||||
* `pgformatter`
|
||||
* `sql-lint`
|
||||
* `sqlfluff`
|
||||
* `sqlfmt`
|
||||
* `sqlformat`
|
||||
* `sqlint`
|
||||
|
|
|
@ -3241,6 +3241,7 @@ documented in additional help files.
|
|||
sql.....................................|ale-sql-options|
|
||||
dprint................................|ale-sql-dprint|
|
||||
pgformatter...........................|ale-sql-pgformatter|
|
||||
sqlfluff..............................|ale-sql-sqlfluff|
|
||||
sqlfmt................................|ale-sql-sqlfmt|
|
||||
sqlformat.............................|ale-sql-sqlformat|
|
||||
stylus..................................|ale-stylus-options|
|
||||
|
|
|
@ -582,6 +582,7 @@ formatting.
|
|||
* [dprint](https://dprint.dev)
|
||||
* [pgformatter](https://github.com/darold/pgFormatter)
|
||||
* [sql-lint](https://github.com/joereynolds/sql-lint)
|
||||
* [sqlfluff](https://github.com/sqlfluff/sqlfluff)
|
||||
* [sqlfmt](https://github.com/jackc/sqlfmt)
|
||||
* [sqlformat](https://github.com/andialbrecht/sqlparse)
|
||||
* [sqlint](https://github.com/purcell/sqlint)
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
Before:
|
||||
runtime ale_linters/sql/sqlfluff.vim
|
||||
|
||||
After:
|
||||
Restore
|
||||
|
||||
call ale#linter#Reset()
|
||||
|
||||
Execute(The sqlfluff handler should handle basic warnings):
|
||||
AssertEqual
|
||||
\ [
|
||||
\ {
|
||||
\ 'filename': 'schema.sql',
|
||||
\ 'lnum': 1,
|
||||
\ 'col': 8,
|
||||
\ 'type': 'W',
|
||||
\ 'code': 'L010',
|
||||
\ 'text': 'Keywords must be consistently upper case.',
|
||||
\ },
|
||||
\ {
|
||||
\ 'filename': 'schema.sql',
|
||||
\ 'lnum': 13,
|
||||
\ 'col': 2,
|
||||
\ 'type': 'W',
|
||||
\ 'code': 'L003',
|
||||
\ 'text': 'Expected 1 indentation, found 0 [compared to line 12]',
|
||||
\ },
|
||||
\ {
|
||||
\ 'filename': 'schema.sql',
|
||||
\ 'lnum': 16,
|
||||
\ 'col': 1,
|
||||
\ 'type': 'W',
|
||||
\ 'code': 'L009',
|
||||
\ 'text': 'Files must end with a single trailing newline.',
|
||||
\ },
|
||||
\ ],
|
||||
\ ale_linters#sql#sqlfluff#Handle(1, [
|
||||
\ '[{"filepath": "schema.sql", "violations": [{"line_no": 1, "line_pos": 8, "code": "L010", "description": "Keywords must be consistently upper case."}, {"line_no": 13, "line_pos": 2, "code": "L003", "description": "Expected 1 indentation, found 0 [compared to line 12]"}, {"line_no": 16, "line_pos": 1, "code": "L009", "description": "Files must end with a single trailing newline."}]}]',
|
||||
\ ])
|
|
@ -0,0 +1,25 @@
|
|||
Before:
|
||||
call ale#assert#SetUpLinterTest('sql', 'sqlfluff')
|
||||
|
||||
After:
|
||||
call ale#assert#TearDownLinterTest()
|
||||
|
||||
Execute(The default command should be correct):
|
||||
AssertLinter 'sqlfluff',
|
||||
\ ale#Escape('sqlfluff')
|
||||
\ . ' lint --dialect ansi --format json %t'
|
||||
|
||||
Execute(The executable should be configurable):
|
||||
let g:ale_sql_sqlfluff_executable = 'foobar'
|
||||
|
||||
AssertLinter 'foobar',
|
||||
\ ale#Escape('foobar')
|
||||
\ . ' lint --dialect ansi --format json %t'
|
||||
|
||||
Execute(Overriding options should work):
|
||||
let g:ale_sql_sqlfluff_executable = 'foobar'
|
||||
let g:ale_sql_sqlfluff_options = '--whatever'
|
||||
|
||||
AssertLinter 'foobar',
|
||||
\ ale#Escape('foobar')
|
||||
\ . ' lint --dialect ansi --format json --whatever %t'
|
Loading…
Reference in New Issue