Class: RuboCop::Cop::Gusto::ToplevelConstants

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/toplevel_constants.rb

Overview

Checks that no top-level constants (excluding classes and modules) are defined. This rule exists to prevent accidental pollution of the global namespace as well as cases where application code has accidentally depended on test code.

By default, this check is limited to files in ‘app/`, `lib/`, and `spec/` directories, except in the root of `lib/` and in support files in `spec/support/`.

Examples:

when in a checked directory

# bad
FOO = 'bar' # lib/foo/bar.rb

# bad
FOO = 'bar' # app/models/foo.rb

# bad
FOO = 'bar' # spec/foo.rb

# good
FOO = 'bar' # spec/spec_helper.rb

# good
class MyClass # lib/foo/bar.rb
  FOO = 'bar'
end

when in a ‘spec/support/` file

# good
FOO = 'bar' # spec/support/foo/bar.rb

when in a ‘config/` file

# good
FOO = 'bar' # config/initializers/foo.rb

Constant Summary collapse

MSG =
"Top-level constants should be defined in an initializer. See https://github.com/Gusto/rubocop-gusto/blob/main/lib/rubocop/cop/gusto/toplevel_constants.rb"

Instance Method Summary collapse

Instance Method Details

#on_casgn(node) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/gusto/toplevel_constants.rb', line 44

def on_casgn(node)
  # Allow nested constants
  return unless node.parent.nil? || node.ancestors.all?(&:begin_type?)
  # Allow one-liners like `MyClass::MY_CONSTANT = 10`
  return unless node.children.first.nil?

  add_offense(node)
end