Rename RedundantParentheses -> ParenthesesAroundCondition

Also rename the option: `parenthesized_assignments` -> `allow_safe_assignment`
This commit is contained in:
Sijawusz Pur Rahnama 2022-11-18 21:00:54 +01:00
parent 0b0a815c31
commit 5ee4074c1b
2 changed files with 14 additions and 13 deletions

View file

@ -1,9 +1,9 @@
require "../../../spec_helper" require "../../../spec_helper"
module Ameba::Rule::Style module Ameba::Rule::Style
subject = RedundantParentheses.new subject = ParenthesesAroundCondition.new
describe RedundantParentheses do describe ParenthesesAroundCondition do
{% for keyword in %w(if unless while until) %} {% for keyword in %w(if unless while until) %}
context "{{ keyword.id }}" do context "{{ keyword.id }}" do
it "reports if redundant parentheses are found" do it "reports if redundant parentheses are found" do
@ -51,7 +51,7 @@ module Ameba::Rule::Style
end end
it "allows to configure assignments" do it "allows to configure assignments" do
rule = Rule::Style::RedundantParentheses.new rule = Rule::Style::ParenthesesAroundCondition.new
rule.exclude_ternary = false rule.exclude_ternary = false
expect_issue rule, <<-CRYSTAL expect_issue rule, <<-CRYSTAL
@ -75,7 +75,7 @@ module Ameba::Rule::Style
end end
end end
context "#parenthesized_assignments=" do context "#allow_safe_assignment=" do
it "reports assignments by default" do it "reports assignments by default" do
expect_issue subject, <<-CRYSTAL expect_issue subject, <<-CRYSTAL
if (foo = @foo) if (foo = @foo)
@ -98,8 +98,8 @@ module Ameba::Rule::Style
end end
it "allows to configure assignments" do it "allows to configure assignments" do
rule = Rule::Style::RedundantParentheses.new rule = Rule::Style::ParenthesesAroundCondition.new
rule.parenthesized_assignments = true rule.allow_safe_assignment = true
expect_issue rule, <<-CRYSTAL expect_issue rule, <<-CRYSTAL
if foo = @foo if foo = @foo

View file

@ -1,5 +1,6 @@
module Ameba::Rule::Style 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: # For example, this is considered invalid:
# #
@ -20,17 +21,17 @@ module Ameba::Rule::Style
# YAML configuration example: # YAML configuration example:
# #
# ``` # ```
# Style/RedundantParentheses: # Style/ParenthesesAroundCondition:
# Enabled: true # Enabled: true
# ExcludeTernary: false # ExcludeTernary: false
# ParenthesizedAssignments: false # AllowSafeAssignment: false
# ``` # ```
class RedundantParentheses < Base class ParenthesesAroundCondition < Base
properties do properties do
description "Disallows redundant parentheses around control expressions" description "Disallows redundant parentheses around control expressions"
exclude_ternary false exclude_ternary false
parenthesized_assignments false allow_safe_assignment false
end end
MSG_REDUNDANT = "Redundant parentheses" MSG_REDUNDANT = "Redundant parentheses"
@ -45,7 +46,7 @@ module Ameba::Rule::Style
when Crystal::Yield when Crystal::Yield
!in_ternary || node.has_parentheses? || node.exps.empty? !in_ternary || node.has_parentheses? || node.exps.empty?
when Crystal::Assign, Crystal::OpAssign, Crystal::MultiAssign when Crystal::Assign, Crystal::OpAssign, Crystal::MultiAssign
!in_ternary && !parenthesized_assignments !in_ternary && !allow_safe_assignment
else else
true true
end end
@ -54,7 +55,7 @@ module Ameba::Rule::Style
def test(source, node : Crystal::If | Crystal::Unless | Crystal::Case | Crystal::While | Crystal::Until) def test(source, node : Crystal::If | Crystal::Unless | Crystal::Case | Crystal::While | Crystal::Until)
cond = node.cond 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| issue_for cond, MSG_MISSING do |corrector|
corrector.insert_before(cond, '(') corrector.insert_before(cond, '(')
corrector.insert_after(cond, ')') corrector.insert_after(cond, ')')