From 94a271b2a191a3da1ba90a25da861dc9eca031df Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 14 Nov 2022 16:30:32 +0100 Subject: [PATCH] Add `Style/ParenthesizedAssignments` rule --- .../rule/style/parenthesized_assignments.cr | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/ameba/rule/style/parenthesized_assignments.cr diff --git a/src/ameba/rule/style/parenthesized_assignments.cr b/src/ameba/rule/style/parenthesized_assignments.cr new file mode 100644 index 00000000..784d58b6 --- /dev/null +++ b/src/ameba/rule/style/parenthesized_assignments.cr @@ -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