Add Style/ParenthesizedAssignments rule

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-14 16:30:32 +01:00
parent 496e8930e2
commit 94a271b2a1

View 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