mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Remove redundant parentheses in if
expressions
+ `elsif` -> `case`
This commit is contained in:
parent
d1bfdaabeb
commit
a6ebb48f14
10 changed files with 20 additions and 18 deletions
|
@ -162,11 +162,11 @@ module Ameba::AST::Util
|
||||||
# Returns the exp code of a control expression.
|
# Returns the exp code of a control expression.
|
||||||
# Wraps implicit tuple literal with curly brackets (e.g. multi-return).
|
# Wraps implicit tuple literal with curly brackets (e.g. multi-return).
|
||||||
def control_exp_code(node : Crystal::ControlExpression, code_lines)
|
def control_exp_code(node : Crystal::ControlExpression, code_lines)
|
||||||
return unless (exp = node.exp)
|
return unless exp = node.exp
|
||||||
return unless (exp_code = node_source(exp, code_lines))
|
return unless exp_code = node_source(exp, code_lines)
|
||||||
return exp_code unless exp.is_a?(Crystal::TupleLiteral) && exp_code[0] != '{'
|
return exp_code unless exp.is_a?(Crystal::TupleLiteral) && exp_code[0] != '{'
|
||||||
return unless (exp_start = exp.elements.first.location)
|
return unless exp_start = exp.elements.first.location
|
||||||
return unless (exp_end = exp.end_location)
|
return unless exp_end = exp.end_location
|
||||||
|
|
||||||
"{#{source_between(exp_start, exp_end, code_lines)}}"
|
"{#{source_between(exp_start, exp_end, code_lines)}}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -114,11 +114,12 @@ module Ameba::AST
|
||||||
def visit(node : Crystal::Var)
|
def visit(node : Crystal::Var)
|
||||||
variable = @current_scope.find_variable node.name
|
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)
|
@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)
|
@current_scope.add_variable(node)
|
||||||
elsif variable # node is a reference
|
when variable # node is a reference
|
||||||
reference = variable.reference node, @current_scope
|
reference = variable.reference node, @current_scope
|
||||||
if @current_assign.is_a?(Crystal::OpAssign) || !reference.target_of?(@current_assign)
|
if @current_assign.is_a?(Crystal::OpAssign) || !reference.target_of?(@current_assign)
|
||||||
variable.reference_assignments!
|
variable.reference_assignments!
|
||||||
|
|
|
@ -38,7 +38,7 @@ module Ameba::Formatter
|
||||||
private def explain(source, issue)
|
private def explain(source, issue)
|
||||||
rule = issue.rule
|
rule = issue.rule
|
||||||
|
|
||||||
return unless (location = issue.location)
|
return unless location = issue.location
|
||||||
|
|
||||||
output_title "ISSUE INFO"
|
output_title "ISSUE INFO"
|
||||||
output_paragraph [
|
output_paragraph [
|
||||||
|
|
|
@ -41,7 +41,7 @@ module Ameba::Formatter
|
||||||
end
|
end
|
||||||
|
|
||||||
def affected_code(issue : Issue, context_lines = 0, max_length = 120, ellipsis = " ...", prompt = "> ")
|
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)
|
affected_code(issue.code, location, issue.end_location, context_lines, max_length, ellipsis, prompt)
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,8 +59,8 @@ module Ameba::Rule::Style
|
||||||
|
|
||||||
def test(source, node : Crystal::Call)
|
def test(source, node : Crystal::Call)
|
||||||
return unless node.name.in?(filter_names)
|
return unless node.name.in?(filter_names)
|
||||||
return unless (filter_location = node.name_location)
|
return unless filter_location = node.name_location
|
||||||
return unless (block = node.block)
|
return unless block = node.block
|
||||||
return unless (body = block.body).is_a?(Crystal::IsA)
|
return unless (body = block.body).is_a?(Crystal::IsA)
|
||||||
return unless (path = body.const).is_a?(Crystal::Path)
|
return unless (path = body.const).is_a?(Crystal::Path)
|
||||||
return unless body.obj.is_a?(Crystal::Var)
|
return unless body.obj.is_a?(Crystal::Var)
|
||||||
|
|
|
@ -116,7 +116,7 @@ module Ameba::Rule::Style
|
||||||
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral)
|
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral)
|
||||||
return if allow_empty_next && (node.exp.nil? || node.exp.try(&.nop?))
|
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|
|
issue_for node, MSG do |corrector|
|
||||||
corrector.replace(node, exp_code)
|
corrector.replace(node, exp_code)
|
||||||
end
|
end
|
||||||
|
|
|
@ -113,7 +113,7 @@ module Ameba::Rule::Style
|
||||||
return if allow_multi_return && node.exp.is_a?(Crystal::TupleLiteral)
|
return if allow_multi_return && node.exp.is_a?(Crystal::TupleLiteral)
|
||||||
return if allow_empty_return && (node.exp.nil? || node.exp.try(&.nop?))
|
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|
|
issue_for node, MSG do |corrector|
|
||||||
corrector.replace(node, exp_code)
|
corrector.replace(node, exp_code)
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,7 +59,7 @@ module Ameba::Rule::Style
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit(node : Crystal::InstanceVar | Crystal::ClassVar)
|
def visit(node : Crystal::InstanceVar | Crystal::ClassVar)
|
||||||
if (location = node.location)
|
if location = node.location
|
||||||
var_locations << location
|
var_locations << location
|
||||||
end
|
end
|
||||||
super
|
super
|
||||||
|
|
|
@ -34,8 +34,8 @@ module Ameba::Rule::Style
|
||||||
|
|
||||||
def test(source, node : Crystal::While)
|
def test(source, node : Crystal::While)
|
||||||
return unless node.cond.true_literal?
|
return unless node.cond.true_literal?
|
||||||
return unless (location = node.location)
|
return unless location = node.location
|
||||||
return unless (end_location = node.cond.end_location)
|
return unless end_location = node.cond.end_location
|
||||||
|
|
||||||
issue_for node, MSG do |corrector|
|
issue_for node, MSG do |corrector|
|
||||||
corrector.replace(location, end_location, "loop do")
|
corrector.replace(location, end_location, "loop do")
|
||||||
|
|
|
@ -17,12 +17,13 @@ class Ameba::Spec::AnnotatedSource
|
||||||
annotations = [] of {Int32, String, String}
|
annotations = [] of {Int32, String, String}
|
||||||
code_lines = annotated_code.split('\n') # must preserve trailing newline
|
code_lines = annotated_code.split('\n') # must preserve trailing newline
|
||||||
code_lines.each do |code_line|
|
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
|
message_index = annotation_match.end
|
||||||
prefix = code_line[0...message_index]
|
prefix = code_line[0...message_index]
|
||||||
message = code_line[message_index...]
|
message = code_line[message_index...]
|
||||||
annotations << {lines.size, prefix, message}
|
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]
|
lines << code_line[...annotation_index]
|
||||||
message_index = annotation_index + ANNOTATION_PATTERN_2.size
|
message_index = annotation_index + ANNOTATION_PATTERN_2.size
|
||||||
message = code_line[message_index...]
|
message = code_line[message_index...]
|
||||||
|
|
Loading…
Reference in a new issue