Skip to content

Commit

Permalink
Support right hand assignment
Browse files Browse the repository at this point in the history
This PR supports right hand assignment for Ruby 2.8.0-dev (Ruby 3.0).
whitequark/parser#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))
```
  • Loading branch information
koic authored and marcandre committed Aug 3, 2020
1 parent 79c2586 commit dd1d0f4
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/traversal.rb
Expand Up @@ -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
Expand Down

0 comments on commit dd1d0f4

Please sign in to comment.