Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add discrete node classes for assignments.
- Loading branch information
1 parent
e75733e
commit fac4004
Showing
14 changed files
with
454 additions
and
8 deletions.
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 @@ | ||
* [#201](https://github.com/rubocop-hq/rubocop-ast/pull/201): Add discrete node classes for assignments. ([@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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `op_asgn` 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 AndAsgnNode < OpAsgnNode | ||
# The operator being used for assignment as a symbol. | ||
# | ||
# @return [Symbol] the assignment operator | ||
def operator | ||
:'&&' | ||
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `lvasgn`, `ivasgn`, `cvasgn`, and `gvasgn` 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 AsgnNode < Node | ||
# The name of the variable being assigned as a symbol. | ||
# | ||
# @return [Symbol] the name of the variable being assigned | ||
def name | ||
node_parts[0] | ||
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `casgn` 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 CasgnNode < Node | ||
# The namespace of the constant being assigned. | ||
# | ||
# @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...) | ||
def namespace | ||
node_parts[0] | ||
end | ||
|
||
# The name of the variable being assigned as a symbol. | ||
# | ||
# @return [Symbol] the name of the variable being assigned | ||
def name | ||
node_parts[1] | ||
end | ||
|
||
# The expression being assigned to the variable. | ||
# | ||
# @return [Node] the expression being assigned. | ||
def expression | ||
node_parts[2] | ||
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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `op_asgn` 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 OpAsgnNode < Node | ||
# @return [AsgnNode] the assignment node | ||
def assignment_node | ||
node_parts[0] | ||
end | ||
|
||
# The name of the variable being assigned as a symbol. | ||
# | ||
# @return [Symbol] the name of the variable being assigned | ||
def name | ||
assignment_node.name | ||
end | ||
|
||
# The operator being used for assignment as a symbol. | ||
# | ||
# @return [Symbol] the assignment operator | ||
def operator | ||
node_parts[1] | ||
end | ||
|
||
# The expression being assigned to the variable. | ||
# | ||
# @return [Node] the expression being assigned. | ||
def expression | ||
node_parts.last | ||
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `op_asgn` 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 OrAsgnNode < OpAsgnNode | ||
# The operator being used for assignment as a symbol. | ||
# | ||
# @return [Symbol] the assignment operator | ||
def operator | ||
:'||' | ||
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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::AndAsgnNode do | ||
let(:or_asgn_node) { parse_source(source).ast } | ||
let(:source) { 'var &&= value' } | ||
|
||
describe '.new' do | ||
it { expect(or_asgn_node).to be_a(described_class) } | ||
end | ||
|
||
describe '#assignment_node' do | ||
subject { or_asgn_node.assignment_node } | ||
|
||
it { is_expected.to be_a(RuboCop::AST::AsgnNode) } | ||
end | ||
|
||
describe '#name' do | ||
subject { or_asgn_node.name } | ||
|
||
it { is_expected.to eq(:var) } | ||
end | ||
|
||
describe '#operator' do | ||
subject { or_asgn_node.operator } | ||
|
||
it { is_expected.to eq(:'&&') } | ||
end | ||
|
||
describe '#expression' do | ||
include AST::Sexp | ||
|
||
subject { or_asgn_node.expression } | ||
|
||
it { is_expected.to eq(s(:send, nil, :value)) } | ||
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,89 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::AsgnNode do | ||
let(:asgn_node) { parse_source(source).ast } | ||
|
||
describe '.new' do | ||
context 'with a `lvasgn` node' do | ||
let(:source) { 'var = value' } | ||
|
||
it { expect(asgn_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a `ivasgn` node' do | ||
let(:source) { '@var = value' } | ||
|
||
it { expect(asgn_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a `cvasgn` node' do | ||
let(:source) { '@@var = value' } | ||
|
||
it { expect(asgn_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a `gvasgn` node' do | ||
let(:source) { '$var = value' } | ||
|
||
it { expect(asgn_node).to be_a(described_class) } | ||
end | ||
end | ||
|
||
describe '#name' do | ||
subject { asgn_node.name } | ||
|
||
context 'with a `lvasgn` node' do | ||
let(:source) { 'var = value' } | ||
|
||
it { is_expected.to eq(:var) } | ||
end | ||
|
||
context 'with a `ivasgn` node' do | ||
let(:source) { '@var = value' } | ||
|
||
it { is_expected.to eq(:@var) } | ||
end | ||
|
||
context 'with a `cvasgn` node' do | ||
let(:source) { '@@var = value' } | ||
|
||
it { is_expected.to eq(:@@var) } | ||
end | ||
|
||
context 'with a `gvasgn` node' do | ||
let(:source) { '$var = value' } | ||
|
||
it { is_expected.to eq(:$var) } | ||
end | ||
end | ||
|
||
describe '#expression' do | ||
include AST::Sexp | ||
|
||
subject { asgn_node.expression } | ||
|
||
context 'with a `lvasgn` node' do | ||
let(:source) { 'var = value' } | ||
|
||
it { is_expected.to eq(s(:send, nil, :value)) } | ||
end | ||
|
||
context 'with a `ivasgn` node' do | ||
let(:source) { '@var = value' } | ||
|
||
it { is_expected.to eq(s(:send, nil, :value)) } | ||
end | ||
|
||
context 'with a `cvasgn` node' do | ||
let(:source) { '@@var = value' } | ||
|
||
it { is_expected.to eq(s(:send, nil, :value)) } | ||
end | ||
|
||
context 'with a `gvasgn` node' do | ||
let(:source) { '$var = value' } | ||
|
||
it { is_expected.to eq(s(:send, nil, :value)) } | ||
end | ||
end | ||
end |
Oops, something went wrong.