mirror of
https://github.com/dense-analysis/ale.git
synced 2025-12-06 12:44:23 +08:00
Implement support for SQL linter sqlfluff (#4361)
This commit is contained in:
66
ale_linters/sql/sqlfluff.vim
Normal file
66
ale_linters/sql/sqlfluff.vim
Normal file
@@ -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.
|
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*
|
sqlfmt *ale-sql-sqlfmt*
|
||||||
|
|
||||||
|
|||||||
@@ -573,6 +573,7 @@ Notes:
|
|||||||
* `dprint`
|
* `dprint`
|
||||||
* `pgformatter`
|
* `pgformatter`
|
||||||
* `sql-lint`
|
* `sql-lint`
|
||||||
|
* `sqlfluff`
|
||||||
* `sqlfmt`
|
* `sqlfmt`
|
||||||
* `sqlformat`
|
* `sqlformat`
|
||||||
* `sqlint`
|
* `sqlint`
|
||||||
|
|||||||
@@ -3241,6 +3241,7 @@ documented in additional help files.
|
|||||||
sql.....................................|ale-sql-options|
|
sql.....................................|ale-sql-options|
|
||||||
dprint................................|ale-sql-dprint|
|
dprint................................|ale-sql-dprint|
|
||||||
pgformatter...........................|ale-sql-pgformatter|
|
pgformatter...........................|ale-sql-pgformatter|
|
||||||
|
sqlfluff..............................|ale-sql-sqlfluff|
|
||||||
sqlfmt................................|ale-sql-sqlfmt|
|
sqlfmt................................|ale-sql-sqlfmt|
|
||||||
sqlformat.............................|ale-sql-sqlformat|
|
sqlformat.............................|ale-sql-sqlformat|
|
||||||
stylus..................................|ale-stylus-options|
|
stylus..................................|ale-stylus-options|
|
||||||
|
|||||||
@@ -582,6 +582,7 @@ formatting.
|
|||||||
* [dprint](https://dprint.dev)
|
* [dprint](https://dprint.dev)
|
||||||
* [pgformatter](https://github.com/darold/pgFormatter)
|
* [pgformatter](https://github.com/darold/pgFormatter)
|
||||||
* [sql-lint](https://github.com/joereynolds/sql-lint)
|
* [sql-lint](https://github.com/joereynolds/sql-lint)
|
||||||
|
* [sqlfluff](https://github.com/sqlfluff/sqlfluff)
|
||||||
* [sqlfmt](https://github.com/jackc/sqlfmt)
|
* [sqlfmt](https://github.com/jackc/sqlfmt)
|
||||||
* [sqlformat](https://github.com/andialbrecht/sqlparse)
|
* [sqlformat](https://github.com/andialbrecht/sqlparse)
|
||||||
* [sqlint](https://github.com/purcell/sqlint)
|
* [sqlint](https://github.com/purcell/sqlint)
|
||||||
|
|||||||
39
test/handler/test_sql_sqlfluff_handler.vader
Normal file
39
test/handler/test_sql_sqlfluff_handler.vader
Normal file
@@ -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."}]}]',
|
||||||
|
\ ])
|
||||||
25
test/linter/test_sql_sqlfluff.vader
Normal file
25
test/linter/test_sql_sqlfluff.vader
Normal file
@@ -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'
|
||||||
Reference in New Issue
Block a user