Remove redundant parentheses in if expressions

+ `elsif` -> `case`
This commit is contained in:
Sijawusz Pur Rahnama 2022-11-14 01:14:39 +01:00
parent d1bfdaabeb
commit a6ebb48f14
10 changed files with 20 additions and 18 deletions

View file

@ -162,11 +162,11 @@ module Ameba::AST::Util
# Returns the exp code of a control expression.
# Wraps implicit tuple literal with curly brackets (e.g. multi-return).
def control_exp_code(node : Crystal::ControlExpression, code_lines)
return unless (exp = node.exp)
return unless (exp_code = node_source(exp, code_lines))
return unless exp = node.exp
return unless exp_code = node_source(exp, code_lines)
return exp_code unless exp.is_a?(Crystal::TupleLiteral) && exp_code[0] != '{'
return unless (exp_start = exp.elements.first.location)
return unless (exp_end = exp.end_location)
return unless exp_start = exp.elements.first.location
return unless exp_end = exp.end_location
"{#{source_between(exp_start, exp_end, code_lines)}}"
end

View file

@ -114,11 +114,12 @@ module Ameba::AST
def visit(node : Crystal::Var)
variable = @current_scope.find_variable node.name
if @current_scope.arg?(node) # node is an argument
case
when @current_scope.arg?(node) # node is an argument
@current_scope.add_argument(node)
elsif variable.nil? && @current_assign # node is a variable
when variable.nil? && @current_assign # node is a variable
@current_scope.add_variable(node)
elsif variable # node is a reference
when variable # node is a reference
reference = variable.reference node, @current_scope
if @current_assign.is_a?(Crystal::OpAssign) || !reference.target_of?(@current_assign)
variable.reference_assignments!

View file

@ -38,7 +38,7 @@ module Ameba::Formatter
private def explain(source, issue)
rule = issue.rule
return unless (location = issue.location)
return unless location = issue.location
output_title "ISSUE INFO"
output_paragraph [

View file

@ -41,7 +41,7 @@ module Ameba::Formatter
end
def affected_code(issue : Issue, context_lines = 0, max_length = 120, ellipsis = " ...", prompt = "> ")
return unless (location = issue.location)
return unless location = issue.location
affected_code(issue.code, location, issue.end_location, context_lines, max_length, ellipsis, prompt)
end

View file

@ -59,8 +59,8 @@ module Ameba::Rule::Style
def test(source, node : Crystal::Call)
return unless node.name.in?(filter_names)
return unless (filter_location = node.name_location)
return unless (block = node.block)
return unless filter_location = node.name_location
return unless block = node.block
return unless (body = block.body).is_a?(Crystal::IsA)
return unless (path = body.const).is_a?(Crystal::Path)
return unless body.obj.is_a?(Crystal::Var)

View file

@ -116,7 +116,7 @@ module Ameba::Rule::Style
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_next && (node.exp.nil? || node.exp.try(&.nop?))
if (exp_code = control_exp_code(node, source.lines))
if exp_code = control_exp_code(node, source.lines)
issue_for node, MSG do |corrector|
corrector.replace(node, exp_code)
end

View file

@ -113,7 +113,7 @@ module Ameba::Rule::Style
return if allow_multi_return && node.exp.is_a?(Crystal::TupleLiteral)
return if allow_empty_return && (node.exp.nil? || node.exp.try(&.nop?))
if (exp_code = control_exp_code(node, source.lines))
if exp_code = control_exp_code(node, source.lines)
issue_for node, MSG do |corrector|
corrector.replace(node, exp_code)
end

View file

@ -59,7 +59,7 @@ module Ameba::Rule::Style
end
def visit(node : Crystal::InstanceVar | Crystal::ClassVar)
if (location = node.location)
if location = node.location
var_locations << location
end
super

View file

@ -34,8 +34,8 @@ module Ameba::Rule::Style
def test(source, node : Crystal::While)
return unless node.cond.true_literal?
return unless (location = node.location)
return unless (end_location = node.cond.end_location)
return unless location = node.location
return unless end_location = node.cond.end_location
issue_for node, MSG do |corrector|
corrector.replace(location, end_location, "loop do")

View file

@ -17,12 +17,13 @@ class Ameba::Spec::AnnotatedSource
annotations = [] of {Int32, String, String}
code_lines = annotated_code.split('\n') # must preserve trailing newline
code_lines.each do |code_line|
if (annotation_match = ANNOTATION_PATTERN_1.match(code_line))
case
when annotation_match = ANNOTATION_PATTERN_1.match(code_line)
message_index = annotation_match.end
prefix = code_line[0...message_index]
message = code_line[message_index...]
annotations << {lines.size, prefix, message}
elsif (annotation_index = code_line.index(ANNOTATION_PATTERN_2))
when annotation_index = code_line.index(ANNOTATION_PATTERN_2)
lines << code_line[...annotation_index]
message_index = annotation_index + ANNOTATION_PATTERN_2.size
message = code_line[message_index...]