Skip to content

Commit

Permalink
def_node_pattern and def_node_search now return the method name.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre authored and mergify[bot] committed Nov 7, 2020
1 parent 11cd4c0 commit a3aaf86
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#147](https://github.com/rubocop-hq/rubocop-ast/pull/147): `def_node_pattern` and `def_node_search` now return the method name. ([@marcandre][])
2 changes: 2 additions & 0 deletions lib/rubocop/ast/node_pattern/method_definer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def def_helper(base, method_name, **defaults)
end
src = yield method_name
base.class_eval(src, location.path, location.lineno)

method_name
end

def emit_node_search(method_name)
Expand Down
13 changes: 11 additions & 2 deletions spec/rubocop/ast/node_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2054,9 +2054,10 @@ def withargs(foo, bar, qux)

let(:keyword_defaults) { {} }
let(:method_name) { :my_matcher }
let(:line_no) { __LINE__ + 2 }
let(:line_no) { __LINE__ + 1 }
let(:call_helper) { MyClass.public_send helper_name, method_name, pattern, **keyword_defaults }
let(:defined_class) do
MyClass.public_send helper_name, method_name, pattern, **keyword_defaults
call_helper
MyClass
end
let(:ruby) { ':hello' }
Expand All @@ -2076,6 +2077,10 @@ def withargs(foo, bar, qux)
context 'def_node_matcher' do
let(:helper_name) { :def_node_matcher }

it 'returns the method name' do
expect(call_helper).to eq method_name
end

context 'when called on matching code' do
it { expect(instance).to match_code(node) }
end
Expand All @@ -2099,6 +2104,10 @@ def withargs(foo, bar, qux)
let(:helper_name) { :def_node_search }
let(:ruby) { 'foo(:hello, :world)' }

it 'returns the method name' do
expect(call_helper).to eq method_name
end

context('without a predicate name') do
context 'when called on matching code' do
it 'returns an enumerator yielding the matches' do
Expand Down

0 comments on commit a3aaf86

Please sign in to comment.