New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
def_node_matcher
fails with NoMethodError
on a non-matching node
#5656
Comments
I keep running into errors with node matcher. The latest case is a snippet like this: module Example
msg = "Hello World"
msg += "Foo Bar"
end A matcher like this: def_node_search :sketchup_extension_new, <<-PATTERN
({:lvasgn :ivasgn :cvasgn :gvasgn :casgn} ...
(:send
(:const nil? :SketchupExtension) :new
_
_))
PATTERN Inspection like this: def investigate(processed_source)
source_node = processed_source.ast
extension_nodes = sketchup_extension_new(source_node).to_a
# ...
end Which leads to error like this:
My spec to reproduce: it 'does not throw an error when inspecting source' do
inspect_source(['module Example',
' msg = "Hello World"',
' msg += "Foo Bar"',
'end'],
'./src/hello.rb')
expect(cop.offenses.size).to eq(1)
end Any ideas? (I get lost when I try to debug into the matchers.) |
I inspected the code the
After trying add some line breaks for readability it looks like this: def bugout()
return nil unless
(temp1 = temp1 = self
(temp2 = temp2 = temp1
(temp2.type == :lvasgn) || (temp2.type == :ivasgn) || (temp2.type == :cvasgn) || (temp2.type == :gvasgn)) &&
(temp1.children.size > 0) && (temp3 = temp3 = temp1.children.last
(temp3.type == :send) && (temp4 = temp4 = temp3.children[0]
(temp4.type == :const) && (temp4.children[0].nil?) && (temp4.children[1] == :SketchupExtension) && (temp4.children.size == 2)) &&
(temp3.children[1] == :new) && true && true && (temp3.children.size == 4)))
block_given? ? yield() : (return true)
end From how I read this:
Since the (Btw, what's the reason behind |
I have been trying to set up a test in RuboCop for the failure, but to trigger it I need the logic of My current speculation is that it relates to
|
I've dug deeper and narrowed down what part of the Compiler generates the code that cause the error; This code generates something like this: (temp4.type == :const) The problem is that when using I experimented by injecting a type check and that avoided the errors I have been running into; I created a branch on my fork that replicate the error in a test and making a change that avoids it: The diff: def compile_literal(cur_node, literal, seq_head)
"(#{cur_node + '.is_a?(RuboCop::AST::Node) && ' if seq_head}#{cur_node}#{'.type' if seq_head} == #{literal})"
end All of RuboCop's tests continued to pass after my tweak. I didn't create a pull request because so far the test is a hack and I'm not sure how this project would ideally set up a test for I also wasn't sure if it was the correct place to add a type check, of whether it could be elsewhere while traversing nodes for Any feedback would be welcome, and I can try to tidy this up. |
I had incorrectly used |
FWIW, I believe #6834 fixed this. |
(I had originally added this as part of issue #5470; #5470 (comment). But I realized that I wasn't trying to match the same thing as described in that issue.)
I'm working on a RuboCop extension with a set of custom cops. I'm running into an unexpected error while trying to pattern match a
send
node.Here are the tests that pass (as expected);
However, this tests fail with an error;
(If its of any use, I have the failing test checked into a dev branch here: https://github.com/SketchUp/rubocop-sketchup/tree/dev-sketchup-extension-filetype)
Expected behavior
I expected the
:sketchup_extension_new
and:sketchup_extension_new?
patterns to returnnil
when inspectinginspect_source('require("filename")')
Actual behavior
Instead I'm getting an error:
Steps to reproduce the problem
I don't have a simple code snippet, I'm not sure how to exactly reproduce minimally the logic in
def_node_matcher
.I have a failing test checked in this repo: https://github.com/SketchUp/rubocop-sketchup/tree/dev-sketchup-extension-filetype
If checking out that dev branch the tests can be run;
bundle exec rspec ./spec/rubocop/sketchup/suggestions/sketchup_require_spec.rb:37:42
When I hooked this up to the debugger I got lost when the code entered
def_node_matcher
. Any suggestions on how to proceed would be appreciated.RuboCop version
Include the output of
rubocop -V
. Here's an example:The text was updated successfully, but these errors were encountered: