Skip to content

Commit 8bd0f3f

Browse files
committed
Add matcher for an arbitrary block argument
1 parent 4eb5319 commit 8bd0f3f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/analyzers/acronym/representation.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def uses_method_chain?
3030
end
3131

3232
def uses_method_chain_with_block?
33+
arg = ArbitraryArg.new
3334
matchers = [
3435
{
3536
method_name: :upcase,
@@ -42,7 +43,7 @@ def uses_method_chain_with_block?
4243
type: :block,
4344
method_name: :map,
4445
chained?: true,
45-
arguments: s(:args, s(:arg, :word))
46+
arguments: arg,
4647
},
4748
{
4849
method_name: :map,
@@ -59,7 +60,7 @@ def uses_method_chain_with_block?
5960
},
6061
{
6162
method_name: :chr,
62-
receiver: s(:lvar, :word)
63+
receiver: arg.lvar
6364
}
6465
]
6566

@@ -152,3 +153,25 @@ def ==(other)
152153
other.type == :lvar
153154
end
154155
end
156+
157+
class NamedLvar
158+
attr_accessor :name
159+
160+
def ==(other)
161+
other == Parser::AST::Node.new(:lvar, name)
162+
end
163+
end
164+
165+
class ArbitraryArg
166+
def lvar
167+
@lvar ||= NamedLvar.new
168+
end
169+
170+
def ==(other)
171+
if other.type == :args
172+
lvar.name = other.first.node_parts
173+
end
174+
175+
other.type == :args
176+
end
177+
end

test/exercises/acronym_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def self.abbreviate(words)
4141
assert_equal ["ruby.acronym.block_syntax.shorthand"], results[:comments]
4242
end
4343

44+
def test_method_chaining_with_block_syntax_with_arbitrary_arg_passes
45+
source = %q{
46+
class Acronym
47+
def self.abbreviate(words)
48+
words.tr('-', ' ').split.map { |term| term.chr }.join.upcase
49+
end
50+
end
51+
}
52+
results = Acronym::Analyze.(source)
53+
assert_equal :approve, results[:status]
54+
assert_equal ["ruby.acronym.block_syntax.shorthand"], results[:comments]
55+
end
56+
4457
def test_module_method_passes
4558
source = %q{
4659
module Acronym

0 commit comments

Comments
 (0)