diff --git a/README.md b/README.md index cd7c150..cb2d93a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ - [x] [stylua](https://github.com/JohnnyMorganz/StyLua) - [ ] [swiftformat](https://github.com/nicklockwood/SwiftFormat) - [ ] [swift-format](https://github.com/apple/swift-format) +- [x] [sqlfluff](https://github.com/sqlfluff/sqlfluff) as `sqlfluff format` +- [x] [sqlfluff_fix](https://github.com/sqlfluff/sqlfluff) as `sqlfluff fix` - [x] [sql-formatter](https://github.com/sql-formatter-org/sql-formatter) - [x] [yapf](https://github.com/google/yapf) - [x] [ruff](https://github.com/astral-sh/ruff) as `ruff format` @@ -48,6 +50,7 @@ - [ ] [hadolint](https://github.com/hadolint/hadolint) - [x] [ktlint](https://github.com/pinterest/ktlint) - [x] [luacheck](https://github.com/lunarmodules/luacheck) +- [x] [sqlfluff](https://github.com/sqlfluff/sqlfluff) - [x] [pylint](https://github.com/PyCQA/pylint) - [ ] [rubocop](https://github.com/rubocop/rubocop) - [x] [selene](https://github.com/Kampfkarren/selene) diff --git a/lua/guard-collection/formatter.lua b/lua/guard-collection/formatter.lua index c66dd9f..7cd1a1c 100644 --- a/lua/guard-collection/formatter.lua +++ b/lua/guard-collection/formatter.lua @@ -153,6 +153,18 @@ M['swift-format'] = { stdin = true, } +M.sqlfluff = { + cmd = 'sqlfluff', + args = { 'format', '-' }, + stdin = true, +} + +M.sqlfluff_fix = { + cmd = 'sqlfluff', + args = { 'fix', '-' }, + stdin = true, +} + M['sql-formatter'] = { cmd = 'sql-formatter', stdin = true, diff --git a/lua/guard-collection/linter/init.lua b/lua/guard-collection/linter/init.lua index ffdaa8b..d97886f 100644 --- a/lua/guard-collection/linter/init.lua +++ b/lua/guard-collection/linter/init.lua @@ -7,6 +7,7 @@ return { hadolint = require('guard-collection.linter.hadolint'), ktlint = require('guard-collection.linter.ktlint'), luacheck = require('guard-collection.linter.luacheck'), + sqlfluff = require('guard-collection.linter.sqlfluff'), pylint = require('guard-collection.linter.pylint'), rubocop = require('guard-collection.linter.rubocop'), selene = require('guard-collection.linter.selene'), diff --git a/lua/guard-collection/linter/sqlfluff.lua b/lua/guard-collection/linter/sqlfluff.lua new file mode 100644 index 0000000..b0766cd --- /dev/null +++ b/lua/guard-collection/linter/sqlfluff.lua @@ -0,0 +1,22 @@ +local lint = require('guard.lint') + +return { + cmd = 'sqlfluff', + args = { 'lint', '-f', 'github-annotation' }, + stdin = true, + parse = lint.from_json({ + attributes = { + row = 'line', + col = 'start_column', + end_col = 'end_column', + severity = 'annotation_level', + message = 'message', + }, + severities = { + notice = lint.severities.info, + warning = lint.severities.warning, + error = lint.severities.error, + }, + source = 'sqlfluff', + }), +} diff --git a/test/formatter/sqlfluff_fix_spec.lua b/test/formatter/sqlfluff_fix_spec.lua new file mode 100644 index 0000000..cd1f729 --- /dev/null +++ b/test/formatter/sqlfluff_fix_spec.lua @@ -0,0 +1,21 @@ +describe('sqlfluff_fix', function() + it('can format', function() + local ft = require('guard.filetype') + local tool = ft('sql'):fmt('sqlfluff_fix') + tool.formatter[1].args = vim.list_extend(tool.formatter[1].args, { '--dialect', 'ansi' }) + require('guard').setup() + + local formatted = require('test.formatter.helper').test_with('sql', { + [[SELECT]], + [[ a + b AS foo,]], + [[ c AS bar]], + [[FROM my_table]], + }) + assert.are.same({ + [[SELECT]], + [[ c AS bar,]], + [[ a + b AS foo]], + [[FROM my_table]], + }, formatted) + end) +end) diff --git a/test/formatter/sqlfluff_spec.lua b/test/formatter/sqlfluff_spec.lua new file mode 100644 index 0000000..615f6fd --- /dev/null +++ b/test/formatter/sqlfluff_spec.lua @@ -0,0 +1,24 @@ +describe('sqlfluff', function() + it('can format', function() + local ft = require('guard.filetype') + local tool = ft('sql'):fmt('sqlfluff') + tool.formatter[1].args = vim.list_extend(tool.formatter[1].args,{ '--dialect', 'ansi' }) + require('guard').setup() + + local formatted = require('test.formatter.helper').test_with('sql', { + [[SELECT *]], + [[FROM]], + [[ World ]], + [[WHERE "Someone"]], + [[ LIKE '%YOU%']], + }) + assert.are.same({ + [[SELECT *]], + [[FROM]], + [[ World]], + [[WHERE]], + [[ "Someone"]], + [[ LIKE '%YOU%']], + }, formatted) + end) +end) diff --git a/test/linter/sqlfluff_spec.lua b/test/linter/sqlfluff_spec.lua new file mode 100644 index 0000000..e72591c --- /dev/null +++ b/test/linter/sqlfluff_spec.lua @@ -0,0 +1,115 @@ +describe('sqlfluff', function() + it('can lint', function() + local helper = require('test.linter.helper') + local ft = require('guard.filetype') + local tool = ft('sql'):lint('sqlfluff') + tool.linter[1].args = vim.list_extend(tool.linter[1].args, { '--dialect', 'ansi' }) + require('guard').setup() + + local diagnostics = helper.test_with('sql', { + [[SELECT a+b AS foo,]], + [[c AS bar from my_table]], + }) + assert.are.same({ + { + bufnr = 3, + col = 0, + end_col = 0, + end_lnum = 0, + lnum = 0, + message = 'LT09: Select targets should be on a new line unless there is only one select target. [nil]', + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 0, + end_col = 0, + end_lnum = 0, + lnum = 0, + message = 'ST06: Select wildcards then simple targets before calculations and aggregates. [nil]', + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 6, + end_col = 6, + end_lnum = 0, + lnum = 0, + message = "LT02: Expected line break and indent of 4 spaces before 'a'. [nil]", + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 8, + end_col = 8, + end_lnum = 0, + lnum = 0, + message = "LT01: Expected single whitespace between naked identifier and binary operator '+'. [nil]", + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 9, + end_col = 9, + end_lnum = 0, + lnum = 0, + message = "LT01: Expected single whitespace between binary operator '+' and naked identifier. [nil]", + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 10, + end_col = 10, + end_lnum = 0, + lnum = 0, + message = "LT01: Expected only single space before 'AS' keyword. Found ' '. [nil]", + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 0, + end_col = 0, + end_lnum = 1, + lnum = 1, + message = 'LT02: Expected indent of 4 spaces. [nil]', + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 8, + end_col = 8, + end_lnum = 1, + lnum = 1, + message = "LT02: Expected line break and no indent before 'from'. [nil]", + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + { + bufnr = 3, + col = 9, + end_col = 9, + end_lnum = 1, + lnum = 1, + message = 'CP01: Keywords must be consistently upper case. [nil]', + namespace = 1, + severity = 3, + source = 'sqlfluff', + }, + }, diagnostics) + end) +end) diff --git a/test/setup.sh b/test/setup.sh index 45e34b9..fd7f4c4 100644 --- a/test/setup.sh +++ b/test/setup.sh @@ -8,7 +8,7 @@ sudo apt-get install -qqq \ clang-format clang-tidy fish elixir & luarocks install luacheck & go install github.com/segmentio/golines@latest & -pip -qqq install autopep8 black djhtml flake8 isort pylint yapf codespell ruff & +pip -qqq install autopep8 black djhtml flake8 isort pylint yapf codespell ruff sqlfluff & npm install -g --silent \ prettier @fsouza/prettierd sql-formatter shellcheck shfmt & gem install -q rubocop &