-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
54 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
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `casgn` nodes. | ||
# Responds to all methods of `const` | ||
class ConstAssignNode < Node | ||
include ConstAccessNode | ||
|
||
# @return [Node, nil] the node associated with the assignment. | ||
# Returns `nil` if is lhs of multiple assignement. | ||
# Should probably be extracted for other assignment nodes | ||
def assignment | ||
children[2] || (parent.mlhs_type? ? nil : parent.children[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
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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# Common functionality for nodes that access constants: | ||
# `const`, `casgn` | ||
module ConstAccessNode | ||
# @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...) | ||
def namespace | ||
children[0] | ||
end | ||
|
||
# @return [Symbol] the demodulized name of the constant: "::Foo::Bar" => :Bar | ||
def short_name | ||
children[1] | ||
end | ||
|
||
# The body of this block. | ||
# | ||
# @return [Boolean] if the constant is a Module / Class, according to the standard convention. | ||
# Note: some classes might have uppercase in which case this method | ||
# returns false | ||
def module_name? | ||
short_name.match?(/[[:lower:]]/) | ||
end | ||
alias class_name? module_name? | ||
|
||
# @return [Boolean] if the constant starts with `::` (aka s(:cbase)) | ||
def absolute? | ||
return false unless namespace | ||
|
||
each_path.first.cbase_type? | ||
end | ||
|
||
# @return [Boolean] if the constant does not start with `::` (aka s(:cbase)) | ||
def relative? | ||
!absolute? | ||
end | ||
|
||
# Yield nodes for the namespace | ||
# | ||
# For `::Foo::Bar::BAZ` => yields: | ||
# s(:cbase), then | ||
# s(:const, :Foo), then | ||
# s(:const, s(:const, :Foo), :Bar) | ||
def each_path(&block) | ||
return to_enum(__method__) unless block | ||
|
||
descendants = [] | ||
last = self | ||
loop do | ||
last = last.children.first | ||
break if last.nil? | ||
|
||
descendants << last | ||
break unless last.const_type? | ||
end | ||
descendants.reverse_each(&block) | ||
|
||
self | ||
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::ConstNode do | ||
subject(:ast) { parse_source(source).ast } | ||
|
||
let(:casgn_node) { ast.each_node.find { |node| node.casgn_type? } } | ||
# Relying on `casgn_node_test` for common methods | ||
# Testing only additional behavior | ||
describe '#assignment' do | ||
context 'for a simple assignement' do | ||
let(:source) { '::Foo::Bar::BAZ = 42' } | ||
it { expect(casgn_node.assignment.source).to eq '42' } | ||
end | ||
|
||
context 'for a complex assignement' do | ||
let(:source) { '::Foo::Bar::BAZ ||= 42' } | ||
it { expect(casgn_node.assignment.source).to eq '42' } | ||
end | ||
|
||
context 'for a multiple assignement' do | ||
let(:source) { '::Foo::Bar::BAZ, = 42' } | ||
it { expect(casgn_node.assignment).to eq nil } | ||
end | ||
end | ||
end |