Skip to content

Commit

Permalink
🔀 Squash merge APPEALS-44512 - Zeitwerk Autoloader Transition (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcroteau authored Oct 31, 2024
1 parent c337074 commit e28b507
Show file tree
Hide file tree
Showing 28 changed files with 802 additions and 152 deletions.
17 changes: 13 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
inherit_from: .rubocop_todo.yml

require:
- .rubocop/custom_cop/top_level_constants_per_file.rb

AllCops:
TargetRubyVersion: 2.5
TargetRailsVersion: 5.1
Include:
- '**/config.ru'
- '**/Rakefile'
- '**/*.rake'
Exclude:
- 'bin/**/*'
- 'db/**/*'
Expand Down Expand Up @@ -62,3 +63,11 @@ Style/TrailingCommaInArrayLiteral:

Style/TrailingCommaInHashLiteral:
Enabled: false

CustomCop/TopLevelConstantsPerFile:
Enabled: true
Include:
- 'app/**/*'
- 'lib/**/*'
Exclude:
- '**/*[^.rb]' # exclude non-.rb files (ex: .rake files)
28 changes: 28 additions & 0 deletions .rubocop/custom_cop/top_level_constants_per_file.rb
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
Loading

0 comments on commit e28b507

Please sign in to comment.