From 5ee4074c1bd542935c3147d46bde85445839149a Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Fri, 18 Nov 2022 21:00:54 +0100 Subject: [PATCH] Rename `RedundantParentheses` -> `ParenthesesAroundCondition` Also rename the option: `parenthesized_assignments` -> `allow_safe_assignment` --- ...ec.cr => parentheses_around_condition_spec.cr} | 12 ++++++------ ...ntheses.cr => parentheses_around_condition.cr} | 15 ++++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) rename spec/ameba/rule/style/{redundant_parentheses_spec.cr => parentheses_around_condition_spec.cr} (91%) rename src/ameba/rule/style/{redundant_parentheses.cr => parentheses_around_condition.cr} (82%) diff --git a/spec/ameba/rule/style/redundant_parentheses_spec.cr b/spec/ameba/rule/style/parentheses_around_condition_spec.cr similarity index 91% rename from spec/ameba/rule/style/redundant_parentheses_spec.cr rename to spec/ameba/rule/style/parentheses_around_condition_spec.cr index 92aa9cea..6312fe0e 100644 --- a/spec/ameba/rule/style/redundant_parentheses_spec.cr +++ b/spec/ameba/rule/style/parentheses_around_condition_spec.cr @@ -1,9 +1,9 @@ require "../../../spec_helper" module Ameba::Rule::Style - subject = RedundantParentheses.new + subject = ParenthesesAroundCondition.new - describe RedundantParentheses do + describe ParenthesesAroundCondition do {% for keyword in %w(if unless while until) %} context "{{ keyword.id }}" do it "reports if redundant parentheses are found" do @@ -51,7 +51,7 @@ module Ameba::Rule::Style end it "allows to configure assignments" do - rule = Rule::Style::RedundantParentheses.new + rule = Rule::Style::ParenthesesAroundCondition.new rule.exclude_ternary = false expect_issue rule, <<-CRYSTAL @@ -75,7 +75,7 @@ module Ameba::Rule::Style end end - context "#parenthesized_assignments=" do + context "#allow_safe_assignment=" do it "reports assignments by default" do expect_issue subject, <<-CRYSTAL if (foo = @foo) @@ -98,8 +98,8 @@ module Ameba::Rule::Style end it "allows to configure assignments" do - rule = Rule::Style::RedundantParentheses.new - rule.parenthesized_assignments = true + rule = Rule::Style::ParenthesesAroundCondition.new + rule.allow_safe_assignment = true expect_issue rule, <<-CRYSTAL if foo = @foo diff --git a/src/ameba/rule/style/redundant_parentheses.cr b/src/ameba/rule/style/parentheses_around_condition.cr similarity index 82% rename from src/ameba/rule/style/redundant_parentheses.cr rename to src/ameba/rule/style/parentheses_around_condition.cr index 07e97532..1b347e2a 100644 --- a/src/ameba/rule/style/redundant_parentheses.cr +++ b/src/ameba/rule/style/parentheses_around_condition.cr @@ -1,5 +1,6 @@ module Ameba::Rule::Style - # A rule that disallows redundant parentheses around control expressions. + # A rule that checks for the presence of superfluous parentheses + # around the condition of `if`, `unless`, `case, `while` and `until`. # # For example, this is considered invalid: # @@ -20,17 +21,17 @@ module Ameba::Rule::Style # YAML configuration example: # # ``` - # Style/RedundantParentheses: + # Style/ParenthesesAroundCondition: # Enabled: true # ExcludeTernary: false - # ParenthesizedAssignments: false + # AllowSafeAssignment: false # ``` - class RedundantParentheses < Base + class ParenthesesAroundCondition < Base properties do description "Disallows redundant parentheses around control expressions" exclude_ternary false - parenthesized_assignments false + allow_safe_assignment false end MSG_REDUNDANT = "Redundant parentheses" @@ -45,7 +46,7 @@ module Ameba::Rule::Style when Crystal::Yield !in_ternary || node.has_parentheses? || node.exps.empty? when Crystal::Assign, Crystal::OpAssign, Crystal::MultiAssign - !in_ternary && !parenthesized_assignments + !in_ternary && !allow_safe_assignment else true end @@ -54,7 +55,7 @@ module Ameba::Rule::Style def test(source, node : Crystal::If | Crystal::Unless | Crystal::Case | Crystal::While | Crystal::Until) cond = node.cond - if cond.is_a?(Crystal::Assign) && parenthesized_assignments + if cond.is_a?(Crystal::Assign) && allow_safe_assignment issue_for cond, MSG_MISSING do |corrector| corrector.insert_before(cond, '(') corrector.insert_after(cond, ')')