From dd1d0f4cd4f276b2dd6e7f37d04f43770fe24d73 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 26 Jun 2020 02:59:39 +0900 Subject: [PATCH] Support right hand assignment This PR supports right hand assignment for Ruby 2.8.0-dev (Ruby 3.0). https://github.com/whitequark/parser/pull/682 ## `mrasgn` vs `masgn` `mrasgn` has the same structure as `masgn` except that the child nodes are reversed. So this PR adds `mrasgn` to the same constant as `masgn`. ### `mrasign` ```console % ruby-parse -e '13.divmod(5) => a, b' warning: parser/current is loading parser/ruby28, which recognizes warning: 2.8.0-dev-compliant syntax, but you are running 2.8.0. warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. (mrasgn (send (int 13) :divmod (int 5)) (mlhs (lvasgn :a) (lvasgn :b))) ``` ### `masign` ```console % ruby-parse -e 'a, b = 13.divmod(5)' warning: parser/current is loading parser/ruby28, which recognizes warning: 2.8.0-dev-compliant syntax, but you are running 2.8.0. warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. (masgn (mlhs (lvasgn :a) (lvasgn :b)) (send (int 13) :divmod (int 5))) ``` ## `rasgn` `rasgn` has the same structure as `mrasgn` without `mlhs`. So this PR adds `rasgn` to the same constant as `mrasgn`. ```console % ruby-parse -e '13.divmod(5) => a' warning: parser/current is loading parser/ruby28, which recognizes warning: 2.8.0-dev-compliant syntax, but you are running 2.8.0. warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri. (rasgn (send (int 13) :divmod (int 5)) (lvasgn :a)) ``` --- CHANGELOG.md | 1 + lib/rubocop/ast/node.rb | 2 +- lib/rubocop/ast/traversal.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50ebca5e8..2c6216522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * [#83](https://github.com/rubocop-hq/rubocop-ast/pull/83): Add `ProcessedSource#each_comment_in_lines` ([@marcandre][]) * [#84](https://github.com/rubocop-hq/rubocop-ast/pull/84): Add `Source::Range#line_span` ([@marcandre][]) * [#87](https://github.com/rubocop-hq/rubocop-ast/pull/87): Add `CaseNode#branches` ([@marcandre][]) +* [#89](https://github.com/rubocop-hq/rubocop-ast/pull/89): Support `mrasgn` has the same structure as `masgn` except that the child nodes are reversed for Ruby 2.8 (3.0) parser. ([@koic][]) ### Bug fixes diff --git a/lib/rubocop/ast/node.rb b/lib/rubocop/ast/node.rb index ffe3305e7..9a1497aac 100644 --- a/lib/rubocop/ast/node.rb +++ b/lib/rubocop/ast/node.rb @@ -38,7 +38,7 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength IMMUTABLE_LITERALS = (LITERALS - MUTABLE_LITERALS).freeze EQUALS_ASSIGNMENTS = %i[lvasgn ivasgn cvasgn gvasgn - casgn masgn].freeze + casgn masgn rasgn mrasgn].freeze SHORTHAND_ASSIGNMENTS = %i[op_asgn or_asgn and_asgn].freeze ASSIGNMENTS = (EQUALS_ASSIGNMENTS + SHORTHAND_ASSIGNMENTS).freeze diff --git a/lib/rubocop/ast/traversal.rb b/lib/rubocop/ast/traversal.rb index 63fe59e92..30a37ecbc 100644 --- a/lib/rubocop/ast/traversal.rb +++ b/lib/rubocop/ast/traversal.rb @@ -28,7 +28,7 @@ def walk(node) arg_expr pin match_rest if_guard unless_guard match_with_trailing_comma].freeze MANY_CHILD_NODES = %i[dstr dsym xstr regexp array hash pair - mlhs masgn or_asgn and_asgn + mlhs masgn or_asgn and_asgn rasgn mrasgn undef alias args super yield or and while_post until_post iflipflop eflipflop match_with_lvasgn begin kwbegin return