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"
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

View file

@ -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, ')')