Skip to content

Commit

Permalink
add ruff linter,formatter (#21)
Browse files Browse the repository at this point in the history
* add ruff linter,formatter

* update readme

* fix ruff formatter test

* add ruff --fix

* Update README.md

* stylua
  • Loading branch information
JMarkin authored Oct 1, 2023
1 parent 3c94a3f commit 6b95e25
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
13 changes: 13 additions & 0 deletions lua/guard-collection/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lua/guard-collection/linter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
}
39 changes: 39 additions & 0 deletions lua/guard-collection/linter/ruff.lua
Original file line number Diff line number Diff line change
@@ -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',
}),
}
23 changes: 23 additions & 0 deletions test/formatter/ruff_fix_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions test/formatter/ruff_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 52 additions & 0 deletions test/linter/ruff_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion test/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down

0 comments on commit 6b95e25

Please sign in to comment.