diff --git a/changelog/change_def_node_pattern_and_def_node_search_now.md b/changelog/change_def_node_pattern_and_def_node_search_now.md new file mode 100644 index 000000000..edfeaf997 --- /dev/null +++ b/changelog/change_def_node_pattern_and_def_node_search_now.md @@ -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][]) diff --git a/lib/rubocop/ast/node_pattern/method_definer.rb b/lib/rubocop/ast/node_pattern/method_definer.rb index 6caac18f8..890cb37e0 100644 --- a/lib/rubocop/ast/node_pattern/method_definer.rb +++ b/lib/rubocop/ast/node_pattern/method_definer.rb @@ -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) diff --git a/spec/rubocop/ast/node_pattern_spec.rb b/spec/rubocop/ast/node_pattern_spec.rb index dda7febb2..15ee7ec51 100644 --- a/spec/rubocop/ast/node_pattern_spec.rb +++ b/spec/rubocop/ast/node_pattern_spec.rb @@ -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' } @@ -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 @@ -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