mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add ExcludePrefixOperators option to Style/VerboseBlock rule
This commit is contained in:
parent
eed094b928
commit
16743a756c
2 changed files with 24 additions and 1 deletions
|
@ -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 }
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue