mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Support named arguments in VerboseBlock#node_to_s
This commit is contained in:
parent
16743a756c
commit
a9d1b17deb
2 changed files with 75 additions and 14 deletions
|
@ -154,6 +154,44 @@ module Ameba::Rule::Style
|
|||
end
|
||||
end
|
||||
|
||||
it "reports call args and named_args" do
|
||||
short_block_variants = {
|
||||
%|map(&.to_s.[start: 0.to_i64, count: 3]?)|,
|
||||
%|map(&.to_s.[0.to_i64, count: 3]?)|,
|
||||
%|map(&.to_s.[0.to_i64, 3]?)|,
|
||||
%|map(&.to_s.[start: 0.to_i64, count: 3]=("foo"))|,
|
||||
%|map(&.to_s.[0.to_i64, count: 3]=("foo"))|,
|
||||
%|map(&.to_s.[0.to_i64, 3]=("foo"))|,
|
||||
%|map(&.to_s.camelcase(lower: true))|,
|
||||
%|map(&.to_s.camelcase)|,
|
||||
%|map(&.to_s.gsub('_', '-'))|,
|
||||
%|map(&.in?(*{1, 2, 3}, **{foo: :bar}))|,
|
||||
%|map(&.in?(1, *foo, 3, **bar))|,
|
||||
%|join(separator: '.', &.to_s)|,
|
||||
}
|
||||
|
||||
source = Source.new path: "source.cr", code: %(
|
||||
(1..3).map { |i| i.to_s[start: 0.to_i64, count: 3]? }
|
||||
(1..3).map { |i| i.to_s[0.to_i64, count: 3]? }
|
||||
(1..3).map { |i| i.to_s[0.to_i64, 3]? }
|
||||
(1..3).map { |i| i.to_s[start: 0.to_i64, count: 3] = "foo" }
|
||||
(1..3).map { |i| i.to_s[0.to_i64, count: 3] = "foo" }
|
||||
(1..3).map { |i| i.to_s[0.to_i64, 3] = "foo" }
|
||||
(1..3).map { |i| i.to_s.camelcase(lower: true) }
|
||||
(1..3).map { |i| i.to_s.camelcase }
|
||||
(1..3).map { |i| i.to_s.gsub('_', '-') }
|
||||
(1..3).map { |i| i.in?(*{1, 2, 3}, **{foo: :bar}) }
|
||||
(1..3).map { |i| i.in?(1, *foo, 3, **bar) }
|
||||
(1..3).join(separator: '.') { |i| i.to_s }
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
source.issues.size.should eq(short_block_variants.size)
|
||||
|
||||
source.issues.each_with_index do |issue, i|
|
||||
issue.message.should eq(VerboseBlock::MSG % short_block_variants[i])
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new path: "source.cr", code: %(
|
||||
(1..3).any? { |i| i.odd? }
|
||||
|
|
|
@ -108,25 +108,48 @@ module Ameba::Rule::Style
|
|||
i
|
||||
end
|
||||
|
||||
protected def node_to_s(node : Crystal::Call)
|
||||
case name = node.name
|
||||
when "[]"
|
||||
name = "[#{node.args.join ", "}]"
|
||||
when "[]?"
|
||||
name = "[#{node.args.join ", "}]?"
|
||||
when "[]="
|
||||
unless node.args.empty?
|
||||
name = "[#{node.args[..-2].join ", "}]=(#{node.args.last})"
|
||||
protected def args_to_s(io : IO, node : Crystal::Call, skip_last_arg = false)
|
||||
node.args.dup.tap do |args|
|
||||
args.pop? if skip_last_arg
|
||||
args.join io, ", "
|
||||
node.named_args.try do |named_args|
|
||||
io << ", " unless args.empty? || named_args.empty?
|
||||
named_args.join io, ", " do |arg, inner_io|
|
||||
inner_io << arg.name << ": " << arg.value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
protected def node_to_s(node : Crystal::Call)
|
||||
String.build do |str|
|
||||
case name = node.name
|
||||
when "[]"
|
||||
str << '['
|
||||
args_to_s(str, node)
|
||||
str << ']'
|
||||
when "[]?"
|
||||
str << '['
|
||||
args_to_s(str, node)
|
||||
str << "]?"
|
||||
when "[]="
|
||||
str << '['
|
||||
args_to_s(str, node, skip_last_arg: true)
|
||||
str << "]=(" << node.args.last? << ')'
|
||||
else
|
||||
str << name
|
||||
if !node.args.empty? || (node.named_args && !node.named_args.try(&.empty?))
|
||||
str << '('
|
||||
args_to_s(str, node)
|
||||
str << ')'
|
||||
end
|
||||
str << " {...}" if node.block
|
||||
end
|
||||
else
|
||||
name += "(#{node.args.join ", "})" unless node.args.empty?
|
||||
name += " {...}" if node.block
|
||||
end
|
||||
name
|
||||
end
|
||||
|
||||
protected def call_code(call, body)
|
||||
args = call.args.join ", " unless call.args.empty?
|
||||
args = String.build { |io| args_to_s(io, call) }.presence
|
||||
args += ", " if args
|
||||
|
||||
call_chain = %w[].tap do |arr|
|
||||
|
|
Loading…
Reference in a new issue