Skip to content

Add Rake/ConstantDefinitionInTask cop - #65

Open
corsonknowles wants to merge 1 commit into
rubocop:masterfrom
corsonknowles:add-constant-definition-in-task-cop
Open

Add Rake/ConstantDefinitionInTask cop#65
corsonknowles wants to merge 1 commit into
rubocop:masterfrom
corsonknowles:add-constant-definition-in-task-cop

Conversation

@corsonknowles

@corsonknowles corsonknowles commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

What

Adds a new cop, Rake/ConstantDefinitionInTask, that registers an offense
for a constant assignment inside a task or namespace.

# bad
task :foo do
  CONST = 1
end

# bad
namespace :foo do
  CONST = 1
end

# good - It is also defined to the top level, but it looks expected behavior.
CONST = 1
task :foo do
end

Why

A constant assigned inside a task or namespace block is defined to the
top level, not scoped to the block. That is the same surprise the existing
cops already cover for methods and classes:

  • Rake/MethodDefinitionInTask for methods
  • Rake/ClassDefinitionInTask for classes and modules
  • Rake/ConstantDefinitionInTask (new) for plain constant assignments

Rake files are also sometimes loaded rather than required, so a constant
that leaks to the top level can produce "already initialized constant"
warnings on reload.

The cop reuses Helper::TaskDefinition, and like Rake/ClassDefinitionInTask
it skips nodes inside a class, module, or Class.new body via
Helper::ClassDefinition.in_class_definition?, because those constants are
not defined to the top level.

Details

Adds the cop and its spec, a config/default.yml entry (Enabled: pending,
VersionAdded: '<<next>>'), the rake_cops.rb require, and a CHANGELOG
entry. bundle exec rake is green.

corsonknowles added a commit to corsonknowles/rubocop-rake that referenced this pull request Jul 1, 2026
@corsonknowles
corsonknowles marked this pull request as ready for review July 2, 2026 04:49
@corsonknowles
corsonknowles force-pushed the add-constant-definition-in-task-cop branch from f2716e7 to 913f2d5 Compare July 10, 2026 20:51
@corsonknowles
corsonknowles force-pushed the add-constant-definition-in-task-cop branch from 913f2d5 to 5702177 Compare July 30, 2026 20:26
Adds a new cop, `Rake/ConstantDefinitionInTask`, that registers an offense
for a constant assignment inside a task or namespace.

```ruby
task :foo do
  CONST = 1
end

namespace :foo do
  CONST = 1
end

CONST = 1
task :foo do
end
```

A constant assigned inside a `task` or `namespace` block is defined to the
top level, not scoped to the block. That is the same surprise the existing
cops already cover for methods and classes:

* `Rake/MethodDefinitionInTask` for methods
* `Rake/ClassDefinitionInTask` for classes and modules
* `Rake/ConstantDefinitionInTask` (new) for plain constant assignments

Rake files are also sometimes loaded rather than required, so a constant
that leaks to the top level can produce "already initialized constant"
warnings on reload.

The cop reuses `Helper::TaskDefinition`, and like `Rake/ClassDefinitionInTask`
it skips nodes inside a `class`, `module`, or `Class.new` body via
`Helper::ClassDefinition.in_class_definition?`, because those constants are
not defined to the top level.

Adds the cop and its spec, a `config/default.yml` entry (`Enabled: pending`,
`VersionAdded: '<<next>>'`), a `register_cop` directive in
`lib/rubocop/cop/rake.rb` so the cop is registered for lazy loading, the
cop count in `spec/lazy_loading_spec.rb`, and a CHANGELOG entry.
`bundle exec rake` is green.
@corsonknowles
corsonknowles force-pushed the add-constant-definition-in-task-cop branch from 5702177 to 6900858 Compare August 10, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant