mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add Style/ParenthesizedAssignments
rule
This commit is contained in:
parent
496e8930e2
commit
94a271b2a1
1 changed files with 43 additions and 0 deletions
43
src/ameba/rule/style/parenthesized_assignments.cr
Normal file
43
src/ameba/rule/style/parenthesized_assignments.cr
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
module Ameba::Rule::Style
|
||||||
|
# A rule that disallows assignments without parens in control expressions.
|
||||||
|
#
|
||||||
|
# For example, this is considered invalid:
|
||||||
|
#
|
||||||
|
# ```
|
||||||
|
# if foo = @foo
|
||||||
|
# do_something
|
||||||
|
# end
|
||||||
|
# ```
|
||||||
|
#
|
||||||
|
# And should be replaced by the following:
|
||||||
|
#
|
||||||
|
# ```
|
||||||
|
# if (foo = @foo)
|
||||||
|
# do_something
|
||||||
|
# end
|
||||||
|
# ```
|
||||||
|
#
|
||||||
|
# YAML configuration example:
|
||||||
|
#
|
||||||
|
# ```
|
||||||
|
# Style/ParenthesizedAssignments:
|
||||||
|
# Enabled: true
|
||||||
|
# ```
|
||||||
|
class ParenthesizedAssignments < Base
|
||||||
|
properties do
|
||||||
|
enabled false
|
||||||
|
description "Disallows assignments without parens in control expressions"
|
||||||
|
end
|
||||||
|
|
||||||
|
MSG = "Missing parentheses around assignment"
|
||||||
|
|
||||||
|
def test(source, node : Crystal::If | Crystal::Unless | Crystal::Case | Crystal::While | Crystal::Until)
|
||||||
|
return unless (cond = node.cond).is_a?(Crystal::Assign)
|
||||||
|
|
||||||
|
issue_for cond, MSG do |corrector|
|
||||||
|
corrector.insert_before(cond, '(')
|
||||||
|
corrector.insert_after(cond, ')')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue