Skip to content
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

Rubocop brakes on 2.7 pattern matching AST nodes while tree walking, as they children may be nil #7624

Closed
maksar opened this issue Jan 3, 2020 · 4 comments · Fixed by #7786
Labels

Comments

@maksar
Copy link

maksar commented Jan 3, 2020

Following code

  case payload
  in key:
    method
  end

produces such AST (with Parser::CurrentRuby.parse, using Parser::Ruby27 parser gem version: 2.7.0.1):

s(:case_match,
  s(:send, nil, :payload),
  s(:in_pattern,
    s(:hash_pattern,
      s(:match_var, :key)), nil, # <- here
    s(:send, nil, :method)), nil)

Second children of the in_pattern s-expression is nil, which leads to

`undefined method `type' for nil:NilClass

error on line https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/ast/traversal.rb#L57

Simplest fix (maksar@db9f1cf) for me was adding .compact, but I'm not sure it is the right thing to do.

Issue is reproducible with current master (019fe5e)

@bbatsov bbatsov added the bug label Jan 5, 2020
@buehmann
Copy link
Contributor

buehmann commented Jan 6, 2020

Thanks! This is related to my remark here: #7605 (comment)

@iliabylich
Copy link
Contributor

Why does rubocop have its own visitor implementation? parser ships with https://github.com/whitequark/parser/blob/master/lib/parser/ast/processor.rb

@buehmann
Copy link
Contributor

buehmann commented Jan 9, 2020

According to Git history:

Add RuboCop::Node::Traversal (to traverse AST without mutating it)

When AST::Processor calls a hook, it takes the return value as an updated
node and tries building a modified AST. We don't want to do this.

aead344

@koic
Copy link
Member

koic commented Mar 11, 2020

This issue will be solved by #7786.

koic added a commit to koic/rubocop that referenced this issue Mar 19, 2020
Closes rubocop#7624 and rubocop#7774.

This PR supports pattern match for `Layout/ElseAlignment` cop.
Also this resolves the crash reported in rubocop#7624 and rubocop#7774 for
using Ruby 2.7's pattern match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants