From 6b95e25d3054a109dcd22736dab207c9d5646b5d Mon Sep 17 00:00:00 2001 From: Yura Markin Date: Sun, 1 Oct 2023 19:32:06 +0300 Subject: [PATCH] add ruff linter,formatter (#21) * add ruff linter,formatter * update readme * fix ruff formatter test * add ruff --fix * Update README.md * stylua --- README.md | 4 +++ lua/guard-collection/formatter.lua | 13 +++++++ lua/guard-collection/linter/init.lua | 1 + lua/guard-collection/linter/ruff.lua | 39 +++++++++++++++++++++ test/formatter/ruff_fix_spec.lua | 23 ++++++++++++ test/formatter/ruff_spec.lua | 25 +++++++++++++ test/linter/ruff_spec.lua | 52 ++++++++++++++++++++++++++++ test/setup.sh | 2 +- 8 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 lua/guard-collection/linter/ruff.lua create mode 100644 test/formatter/ruff_fix_spec.lua create mode 100644 test/formatter/ruff_spec.lua create mode 100644 test/linter/ruff_spec.lua diff --git a/README.md b/README.md index 9762b0d..cd7c150 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ - [ ] [swift-format](https://github.com/apple/swift-format) - [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` +- [x] [ruff_fix](https://github.com/astral-sh/ruff) as `ruff --fix` + ## Linters @@ -50,5 +53,6 @@ - [x] [selene](https://github.com/Kampfkarren/selene) - [ ] [shellcheck](https://github.com/koalaman/shellcheck) - [ ] [stylelint](https://stylelint.io/) +- [x] [ruff](https://github.com/astral-sh/ruff) ## License MIT diff --git a/lua/guard-collection/formatter.lua b/lua/guard-collection/formatter.lua index 4c866c0..c66dd9f 100644 --- a/lua/guard-collection/formatter.lua +++ b/lua/guard-collection/formatter.lua @@ -164,4 +164,17 @@ M.yapf = { stdin = true, } +M.ruff = { + cmd = 'ruff', + args = { 'format', '-' }, + stdin = true, +} + +M.ruff_fix = { + cmd = 'ruff', + args = { '--fix', '-', '--stdin-filename' }, + stdin = true, + fname = true, +} + return M diff --git a/lua/guard-collection/linter/init.lua b/lua/guard-collection/linter/init.lua index e78345d..ffdaa8b 100644 --- a/lua/guard-collection/linter/init.lua +++ b/lua/guard-collection/linter/init.lua @@ -12,4 +12,5 @@ return { selene = require('guard-collection.linter.selene'), shellcheck = require('guard-collection.linter.shellcheck'), stylelint = require('guard-collection.linter.stylelint'), + ruff = require('guard-collection.linter.ruff'), } diff --git a/lua/guard-collection/linter/ruff.lua b/lua/guard-collection/linter/ruff.lua new file mode 100644 index 0000000..29ddef6 --- /dev/null +++ b/lua/guard-collection/linter/ruff.lua @@ -0,0 +1,39 @@ +local lint = require('guard.lint') + +return { + cmd = 'ruff', + args = { + '-n', + '-e', + '--format', + 'json', + '-', + '--stdin-filename', + }, + stdin = true, + fname = true, + parse = lint.from_json({ + attributes = { + severity = 'type', + lnum = function(js) + return js['location']['row'] + end, + col = function(js) + return js['location']['column'] + end, + }, + severities = { + E = lint.severities.error, -- pycodestyle errors + W = lint.severities.warning, -- pycodestyle warnings + F = lint.severities.info, -- pyflakes + A = lint.severities.info, -- flake8-builtins + B = lint.severities.warning, -- flake8-bugbear + C = lint.severities.warning, -- flake8-comprehensions + T = lint.severities.info, -- flake8-print + U = lint.severities.info, -- pyupgrade + D = lint.severities.info, -- pydocstyle + M = lint.severities.into, -- Meta + }, + source = 'ruff', + }), +} diff --git a/test/formatter/ruff_fix_spec.lua b/test/formatter/ruff_fix_spec.lua new file mode 100644 index 0000000..ac2bd5d --- /dev/null +++ b/test/formatter/ruff_fix_spec.lua @@ -0,0 +1,23 @@ +describe('ruff', function() + it('can format', function() + local ft = require('guard.filetype') + ft('python'):fmt('ruff_fix') + require('guard').setup() + + local formatted = require('test.formatter.helper').test_with('python', { + [[import os]], + [[]], + [[def foo(n):]], + [[ if n in (1, 2, 3):]], + [[ return n + 1]], + [[ a, b = 1, 2]], + }) + assert.are.same({ + [[]], + [[def foo(n):]], + [[ if n in (1, 2, 3):]], + [[ return n + 1]], + [[ a, b = 1, 2]], + }, formatted) + end) +end) diff --git a/test/formatter/ruff_spec.lua b/test/formatter/ruff_spec.lua new file mode 100644 index 0000000..c791392 --- /dev/null +++ b/test/formatter/ruff_spec.lua @@ -0,0 +1,25 @@ +describe('ruff', function() + it('can format', function() + local ft = require('guard.filetype') + ft('python'):fmt('ruff') + require('guard').setup() + + local formatted = require('test.formatter.helper').test_with('python', { + [[def foo(n):]], + [[ if n in (1,2,3):]], + [[ return n+1]], + [[a, b = 1, 2]], + [[b, a = a, b]], + [[print( f"The factorial of {a} is: {foo(a)}")]], + }) + assert.are.same({ + [[def foo(n):]], + [[ if n in (1, 2, 3):]], + [[ return n + 1]], + [[]], + [[a, b = (1, 2)]], + [[b, a = a, b]], + [[print(f"The factorial of {a} is: {foo(a)}")]], + }, formatted) + end) +end) diff --git a/test/linter/ruff_spec.lua b/test/linter/ruff_spec.lua new file mode 100644 index 0000000..0028eca --- /dev/null +++ b/test/linter/ruff_spec.lua @@ -0,0 +1,52 @@ +describe('ruff', function() + it('can lint', function() + local helper = require('test.linter.helper') + local ns = helper.namespace + local ft = require('guard.filetype') + ft('python'):lint('ruff') + require('guard').setup() + + local diagnostics = helper.test_with('python', { + [[import os]], + [[def foo(n):]], + [[ if n in (1, 2, 3):]], + [[ return n + 1]], + [[ a, b = 1, 2]], + }) + assert.are.same({ + { + bufnr = 3, + col = 7, + end_col = 7, + end_lnum = 0, + lnum = 0, + message = '`os` imported but unused [F401]', + namespace = 1, + severity = 1, + source = 'ruff', + }, + { + bufnr = 3, + col = 2, + end_col = 2, + end_lnum = 4, + lnum = 4, + message = 'Local variable `a` is assigned to but never used [F841]', + namespace = 1, + severity = 1, + source = 'ruff', + }, + { + bufnr = 3, + col = 5, + end_col = 5, + end_lnum = 4, + lnum = 4, + message = 'Local variable `b` is assigned to but never used [F841]', + namespace = 1, + severity = 1, + source = 'ruff', + }, + }, diagnostics) + end) +end) diff --git a/test/setup.sh b/test/setup.sh index 5899012..45e34b9 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 & +pip -qqq install autopep8 black djhtml flake8 isort pylint yapf codespell ruff & npm install -g --silent \ prettier @fsouza/prettierd sql-formatter shellcheck shfmt & gem install -q rubocop &