-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
MasgnNode
class for masgn
nodes.
- Loading branch information
1 parent
3aef982
commit ff4472f
Showing
6 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#203](https://github.com/rubocop-hq/rubocop-ast/pull/203): Add `MasgnNode` class for `masgn` nodes. ([@dvandersluis][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `masgn` nodes. | ||
# This will be used in place of a plain node when the builder constructs | ||
# the AST, making its methods available to all assignment nodes within RuboCop. | ||
class MasgnNode < Node | ||
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn].to_set.freeze | ||
|
||
# Calls the given block for each assignment node in the `masgn` LHS. | ||
# If no block is given, an `Enumerator` is returned. | ||
# | ||
# @return [self] if a block is given | ||
# @return [Enumerator] if no block is given | ||
def each_assignment | ||
# The first child is a `mlhs` node | ||
assignments = node_parts[0].each_descendant(*ASSIGNMENT_TYPES) | ||
return assignments.to_enum unless block_given? | ||
|
||
assignments do |assignment| | ||
yield(*assignment) | ||
end | ||
|
||
self | ||
end | ||
|
||
# @return Array<Node> the assignment nodes of the multiple assignment | ||
def assignments | ||
each_assignment.to_a | ||
end | ||
|
||
# @return Array<Symbol> names of all the variables being assigned | ||
def names | ||
each_assignment.map do |assignment| | ||
unless assignment.is_a?(AsgnNode) | ||
assignment = assignment.each_descendant(*ASSIGNMENT_TYPES).first | ||
end | ||
|
||
next if assignment.nil? | ||
|
||
assignment.name | ||
end | ||
end | ||
|
||
# The expression being assigned to the variable. | ||
# | ||
# @return [Node] the expression being assigned. | ||
def expression | ||
node_parts[1] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::MasgnNode do | ||
let(:masgn_node) { parse_source(source).ast } | ||
|
||
describe '.new' do | ||
context 'with a `masgn` node' do | ||
let(:source) { 'x, y = z' } | ||
|
||
it { expect(masgn_node).to be_a(described_class) } | ||
end | ||
end | ||
|
||
describe '#assignments' do | ||
include AST::Sexp | ||
|
||
subject { masgn_node.assignments } | ||
|
||
context 'with variables' do | ||
let(:source) { 'x, y = z' } | ||
|
||
it { is_expected.to eq([s(:lvasgn, :x), s(:lvasgn, :y)]) } | ||
end | ||
|
||
context 'with a splat' do | ||
let(:source) { 'x, *y = z' } | ||
|
||
it { is_expected.to eq([s(:lvasgn, :x), s(:lvasgn, :y)]) } | ||
end | ||
|
||
context 'with nested `mlhs` nodes' do | ||
let(:source) { 'a, (b, c) = z' } | ||
|
||
it { is_expected.to eq([s(:lvasgn, :a), s(:lvasgn, :b), s(:lvasgn, :c)]) } | ||
end | ||
end | ||
|
||
describe '#names' do | ||
subject { masgn_node.names } | ||
|
||
let(:source) { 'a, @b, @@c, $d, *e = z' } | ||
|
||
it { is_expected.to eq(%i[a @b @@c $d e]) } | ||
|
||
context 'with nested `mlhs` nodes' do | ||
let(:source) { 'a, (b, c) = z' } | ||
|
||
it { is_expected.to eq(%i[a b c]) } | ||
end | ||
end | ||
|
||
describe '#expression' do | ||
include AST::Sexp | ||
|
||
subject { masgn_node.expression } | ||
|
||
context 'with variables' do | ||
let(:source) { 'x, y = z' } | ||
|
||
it { is_expected.to eq(s(:send, nil, :z)) } | ||
end | ||
|
||
context 'with a LHS splat' do | ||
let(:source) { 'x, *y = z' } | ||
|
||
it { is_expected.to eq(s(:send, nil, :z)) } | ||
end | ||
|
||
context 'with multiple RHS values' do | ||
let(:source) { 'x, y = 1, 2' } | ||
|
||
it { is_expected.to eq(s(:array, s(:int, 1), s(:int, 2))) } | ||
end | ||
|
||
context 'with an RHS splat' do | ||
let(:source) { 'x, y = *z' } | ||
|
||
it { is_expected.to eq(s(:array, s(:splat, s(:send, nil, :z)))) } | ||
end | ||
end | ||
end |