-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove +Parser+ dependencies as class loading time
We deliberately only load `parser` when there a console session starts, so we need to remove references from class declarations.
- Loading branch information
1 parent
96373f9
commit 2addc58
Showing
4 changed files
with
82 additions
and
72 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,69 @@ | ||
# Naming class with dot so that it doesn't get loaded eagerly by Zeitwork. We want to load | ||
# only when a console session is started, when +parser+ is loaded. | ||
# | ||
# See +Console1984::Supervisor#require_dependencies+ | ||
class Console1984::CommandValidator::CommandParser < ::Parser::AST::Processor | ||
include AST::Processor::Mixin | ||
include Console1984::Freezeable | ||
|
||
def initialize | ||
@constants = [] | ||
@declared_classes_or_modules = [] | ||
@constant_assignments = [] | ||
end | ||
|
||
# We define accessors to define lists without duplicates. We are not using a +SortedSet+ because we want | ||
# to mutate strings in the list while the processing is happening. And we don't use metapgroamming to define the | ||
# accessors to prevent having problems with freezable and its instance_variable* protection. | ||
|
||
def constants | ||
@constants.uniq | ||
end | ||
|
||
def declared_classes_or_modules | ||
@declared_classes_or_modules.uniq | ||
end | ||
|
||
def constant_assignments | ||
@constant_assignments.uniq | ||
end | ||
|
||
def on_class(node) | ||
super | ||
const_declaration, _, _ = *node | ||
constant = extract_constants(const_declaration).first | ||
@declared_classes_or_modules << constant if constant.present? | ||
end | ||
|
||
alias_method :on_module, :on_class | ||
|
||
def on_const(node) | ||
super | ||
name, const_name = *node | ||
const_name = const_name.to_s | ||
last_constant = @constants.last | ||
|
||
if name.nil? || (name && name.type == :cbase) # cbase = leading :: | ||
if last_constant&.end_with?("::") | ||
last_constant << const_name | ||
else | ||
@constants << const_name | ||
end | ||
elsif last_constant | ||
last_constant << "::#{const_name}" | ||
end | ||
end | ||
|
||
def on_casgn(node) | ||
super | ||
scope_node, name, value_node = *node | ||
@constant_assignments.push(*extract_constants(value_node)) | ||
end | ||
|
||
private | ||
def extract_constants(node) | ||
self.class.new.tap do |processor| | ||
processor.process(node) | ||
end.constants | ||
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
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