Skip to content

Commit

Permalink
Merge pull request #6851 from marcandre/type_pattern
Browse files Browse the repository at this point in the history
Fix NodePattern of node type on a literal
  • Loading branch information
Drenmi authored Mar 21, 2019
2 parents 698ddeb + abffc1f commit 3173f34
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rubocop/node_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ def compile_funcall(tokens, cur_node, method, seq_head)
end

def compile_nodetype(cur_node, type)
"(#{cur_node} && #{cur_node}.#{type.tr('-', '_')}_type?)"
"(#{cur_node}.is_a?(RuboCop::AST::Node) && " \
"#{cur_node}.#{type.tr('-', '_')}_type?)"
end

def compile_param(cur_node, number, seq_head)
Expand Down
41 changes: 41 additions & 0 deletions spec/rubocop/node_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,47 @@
end
end

describe 'node type' do
describe 'in seq head' do
let(:pattern) { '(send ...)' }

context 'on a node with the same type' do
let(:ruby) { '@ivar + 2' }

it_behaves_like 'matching'
end

context 'on a child with a different type' do
let(:ruby) { '@ivar += 2' }

it_behaves_like 'nonmatching'
end
end

describe 'for a child' do
let(:pattern) { '(_ send ...)' }

context 'on a child with the same type' do
let(:ruby) { 'foo.bar' }

it_behaves_like 'matching'
end

context 'on a child with a different type' do
let(:ruby) { '@ivar.bar' }

it_behaves_like 'nonmatching'
end

context 'on a child litteral' do
let(:pattern) { '(_ _ send)' }
let(:ruby) { '42.bar' }

it_behaves_like 'nonmatching'
end
end
end

describe 'literals' do
context 'negative integer literals' do
let(:pattern) { '(int -100)' }
Expand Down

0 comments on commit 3173f34

Please sign in to comment.