Add Rake/ConstantDefinitionInTask cop - #65
Open
corsonknowles wants to merge 1 commit into
Open
Conversation
corsonknowles
added a commit
to corsonknowles/rubocop-rake
that referenced
this pull request
Jul 1, 2026
corsonknowles
marked this pull request as ready for review
July 2, 2026 04:49
corsonknowles
force-pushed
the
add-constant-definition-in-task-cop
branch
from
July 10, 2026 20:51
f2716e7 to
913f2d5
Compare
corsonknowles
force-pushed
the
add-constant-definition-in-task-cop
branch
from
July 30, 2026 20:26
913f2d5 to
5702177
Compare
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
force-pushed
the
add-constant-definition-in-task-cop
branch
from
August 10, 2026 11:43
5702177 to
6900858
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new cop,
Rake/ConstantDefinitionInTask, that registers an offensefor a constant assignment inside a task or namespace.
Why
A constant assigned inside a
taskornamespaceblock is defined to thetop level, not scoped to the block. That is the same surprise the existing
cops already cover for methods and classes:
Rake/MethodDefinitionInTaskfor methodsRake/ClassDefinitionInTaskfor classes and modulesRake/ConstantDefinitionInTask(new) for plain constant assignmentsRake 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 likeRake/ClassDefinitionInTaskit skips nodes inside a
class,module, orClass.newbody viaHelper::ClassDefinition.in_class_definition?, because those constants arenot defined to the top level.
Details
Adds the cop and its spec, a
config/default.ymlentry (Enabled: pending,VersionAdded: '<<next>>'), therake_cops.rbrequire, and a CHANGELOGentry.
bundle exec rakeis green.