-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
VarNode
class for lvar
, ivar
, cvar
and gvar
node types.
- Loading branch information
1 parent
742450b
commit 0b334f8
Showing
6 changed files
with
84 additions
and
4 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 @@ | ||
* [#204](https://github.com/rubocop-hq/rubocop-ast/pull/204): Add `VarNode` class for `lvar`, `ivar`, `cvar` and `gvar` node types. ([@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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `lvar`, `ivar`, `cvar` and `gvar` 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 VarNode < Node | ||
# @return [Symbol] The name of the variable. | ||
def name | ||
node_parts[0] | ||
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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::VarNode do | ||
let(:node) { parse_source(source).node } | ||
|
||
describe '.new' do | ||
context 'with a `lvar` node' do | ||
let(:source) { 'x = 1; >>x<<' } | ||
|
||
it { expect(node).to be_a(described_class) } | ||
end | ||
|
||
context 'with an `ivar` node' do | ||
let(:source) { '@x' } | ||
|
||
it { expect(node).to be_a(described_class) } | ||
end | ||
|
||
context 'with an `cvar` node' do | ||
let(:source) { '@@x' } | ||
|
||
it { expect(node).to be_a(described_class) } | ||
end | ||
|
||
context 'with an `gvar` node' do | ||
let(:source) { '$x' } | ||
|
||
it { expect(node).to be_a(described_class) } | ||
end | ||
end | ||
|
||
describe '#name' do | ||
subject { node.name } | ||
|
||
context 'with a `lvar` node' do | ||
let(:source) { 'x = 1; >>x<<' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with an `ivar` node' do | ||
let(:source) { '@x' } | ||
|
||
it { is_expected.to eq(:@x) } | ||
end | ||
|
||
context 'with an `cvar` node' do | ||
let(:source) { '@@x' } | ||
|
||
it { is_expected.to eq(:@@x) } | ||
end | ||
|
||
context 'with an `gvar` node' do | ||
let(:source) { '$x' } | ||
|
||
it { is_expected.to eq(:$x) } | ||
end | ||
end | ||
end |