From 597ebaad89b2fffa50c76cf6cf3b24e648f4ab01 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sun, 27 Dec 2020 23:42:07 -0500 Subject: [PATCH 1/2] Update node types --- lib/rubocop/ast/builder.rb | 1 + lib/rubocop/ast/traversal.rb | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/ast/builder.rb b/lib/rubocop/ast/builder.rb index 957d7549b..fec878395 100644 --- a/lib/rubocop/ast/builder.rb +++ b/lib/rubocop/ast/builder.rb @@ -52,6 +52,7 @@ class Builder < Parser::Builders::Default indexasgn: IndexasgnNode, irange: RangeNode, erange: RangeNode, + kwargs: HashNode, kwsplat: KeywordSplatNode, lambda: LambdaNode, module: ModuleNode, diff --git a/lib/rubocop/ast/traversal.rb b/lib/rubocop/ast/traversal.rb index 3fdb0148d..071928313 100644 --- a/lib/rubocop/ast/traversal.rb +++ b/lib/rubocop/ast/traversal.rb @@ -114,9 +114,10 @@ def arity_check(range) in_match match_alt break next match_as array_pattern array_pattern_with_tail hash_pattern const_pattern find_pattern - index indexasgn procarg0] + index indexasgn procarg0 kwargs] many_opt_node_children = %i[case rescue resbody ensure for when - case_match in_pattern irange erange] + case_match in_pattern irange erange + match_pattern match_pattern_p] ### Callbacks for above def_callback no_children From 5b1991a1af797d69d3f0a7daf329aac7dc411f77 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sun, 27 Dec 2020 23:33:39 -0500 Subject: [PATCH 2/2] Turn on 'emit_match_pattern' --- README.md | 4 +++- lib/rubocop/ast/builder.rb | 1 + spec/spec_helper.rb | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5eda6ce2..b532cafa5 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,9 @@ See the [docs site](https://docs.rubocop.org/rubocop-ast) for more details. ### Parser compatibility switches -This gem, by default, uses most [legacy AST output from parser](https://github.com/whitequark/parser/#usage), except for `emit_forward_arg` which is set to `true`. +This gem, by default, uses most [legacy AST output from parser](https://github.com/whitequark/parser/#usage), except for the following which are set to `true`: +* `emit_forward_arg` +* `emit_match_pattern` The main `RuboCop` gem uses these defaults (and is currently only compatible with these), but this gem can be used separately from `RuboCop` and is meant to be compatible with all settings. For example, to have `-> { ... }` emitted as `LambdaNode` instead of `SendNode`: diff --git a/lib/rubocop/ast/builder.rb b/lib/rubocop/ast/builder.rb index fec878395..5fa16c757 100644 --- a/lib/rubocop/ast/builder.rb +++ b/lib/rubocop/ast/builder.rb @@ -15,6 +15,7 @@ module AST # root_node = parser.parse(buffer) class Builder < Parser::Builders::Default self.emit_forward_arg = true + self.emit_match_pattern = true if respond_to?(:emit_match_pattern=) # @api private NODE_MAP = { diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a4363e4ad..e2a06b840 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,6 +14,9 @@ if ENV['MODERNIZE'] RuboCop::AST::Builder.modernize RuboCop::AST::Builder.emit_forward_arg = false # inverse of default + if RuboCop::AST::Builder.respond_to?(:emit_match_pattern=) + RuboCop::AST::Builder.emit_match_pattern = false # inverse of default + end end RSpec.shared_context 'ruby 2.3', :ruby23 do