-
-
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.
Refactor
CasgnNode
and ConstNode
to extract common functionality …
…to `ConstantNode` mixin.
- Loading branch information
1 parent
b91da40
commit 742450b
Showing
5 changed files
with
137 additions
and
64 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
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# Common functionality for nodes that deal with constants: | ||
# `const`, `casgn`. | ||
module ConstantNode | ||
# @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 | ||
|
||
# @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