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 957d7549b..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 = { @@ -52,6 +53,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 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