Add ExcludePrefixOperators option to Style/VerboseBlock rule

This commit is contained in:
Sijawusz Pur Rahnama 2021-02-03 02:00:49 +01:00
parent eed094b928
commit 16743a756c
2 changed files with 24 additions and 1 deletions

View file

@ -76,6 +76,20 @@ module Ameba::Rule::Style
.catch(source).should_not be_valid
end
it "#exclude_prefix_operators" do
source = Source.new %(
(1..3).sum { |i| +i }
(1..3).sum { |i| -i }
)
rule = VerboseBlock.new
rule
.tap(&.exclude_prefix_operators = true)
.catch(source).should be_valid
rule
.tap(&.exclude_prefix_operators = false)
.catch(source).should_not be_valid
end
it "#exclude_operators" do
source = Source.new %(
(1..3).sum { |i| i * 2 }

View file

@ -21,6 +21,7 @@ module Ameba::Rule::Style
# Enabled: true
# ExcludeMultipleLineBlocks: true
# ExcludeCallsWithBlocks: false
# ExcludePrefixOperators: true
# ExcludeOperators: false
# ExcludeSetters: false
# MaxLineLength: ~
@ -32,6 +33,7 @@ module Ameba::Rule::Style
exclude_calls_with_block true
exclude_multiple_line_blocks false
exclude_prefix_operators true
exclude_operators false
exclude_setters false
@ -49,9 +51,14 @@ module Ameba::Rule::Style
a_location.line_number == b_location.line_number
end
private OPERATOR_CHARS =
private PREFIX_OPERATORS = {"+", "-"}
private OPERATOR_CHARS =
{'[', ']', '!', '=', '>', '<', '~', '+', '-', '*', '/', '%', '^', '|', '&'}
protected def prefix_operator?(node)
node.name.in?(PREFIX_OPERATORS) && node.args.empty?
end
protected def operator?(name)
name.each_char do |char|
return false unless char.in?(OPERATOR_CHARS)
@ -138,9 +145,11 @@ module Ameba::Rule::Style
CALL_PATTERN % {call.name, args, name}
end
# ameba:disable Metrics/CyclomaticComplexity
protected def issue_for_valid(source, call : Crystal::Call, body : Crystal::Call)
return if exclude_calls_with_block && body.block
return if exclude_multiple_line_blocks && !same_location_lines?(call, body)
return if exclude_prefix_operators && prefix_operator?(body)
return if exclude_operators && operator?(body.name)
return if exclude_setters && setter?(body.name)