-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Squash merge APPEALS-44512 - Zeitwerk Autoloader Transition (#1733)
- Loading branch information
Showing
28 changed files
with
802 additions
and
152 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module RuboCop | ||
module CustomCop | ||
class TopLevelConstantsPerFile < RuboCop::Cop::Cop | ||
MSG = "Multiple top-level constants detected in one file. The autoloader expects one top-level constant per file." | ||
|
||
def investigate(processed_source) | ||
return unless processed_source | ||
|
||
# If more than one top-level constant in the file, add offense on the second one | ||
if top_level_constant_nodes.size > 1 | ||
add_offense(top_level_constant_nodes[1], message: MSG) | ||
end | ||
end | ||
|
||
private | ||
|
||
def top_level_constant_nodes | ||
@top_level_constant_nodes ||= | ||
processed_source.ast.each_node(:class, :module).select { |node| top_level_constant?(node) } | ||
end | ||
|
||
def top_level_constant?(node) | ||
# node is not nested within a class or module node? | ||
node.ancestors.none? { |ancestor| ancestor.class_type? || ancestor.module_type? } | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.