-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9363 from uplus/add-new-cop-or_assignment_to_cons…
…tant Add new cop `Lint/OrAssignmentToConstant`
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 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 @@ | ||
* [#9363](https://github.com/rubocop-hq/rubocop/pull/9363): Add new cop `Lint/OrAssignmentToConstant`. ([@uplus][]) |
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
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for unintended or-assignment to a constant. | ||
# | ||
# Constants should always be assigned in the same location. And its value | ||
# should always be the same. If constants are assigned in multiple | ||
# locations, the result may vary depending on the order of `require`. | ||
# | ||
# Also, if you already have such an implementation, auto-correction may | ||
# change the result. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# CONST ||= 1 | ||
# | ||
# # good | ||
# CONST = 1 | ||
# | ||
class OrAssignmentToConstant < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Avoid using or-assignment with constants.' | ||
|
||
def on_or_asgn(node) | ||
lhs, _rhs = *node | ||
return unless lhs&.casgn_type? | ||
|
||
add_offense(node.loc.operator) do |corrector| | ||
corrector.replace(node.loc.operator, '=') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::OrAssignmentToConstant, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense with or-assignment to a constant' do | ||
expect_offense(<<~RUBY) | ||
CONST ||= 1 | ||
^^^ Avoid using or-assignment with constants. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
CONST = 1 | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with plain assignment to a constant' do | ||
expect_no_offenses(<<~RUBY) | ||
CONST = 1 | ||
RUBY | ||
end | ||
|
||
[ | ||
['a local variable', 'var'], | ||
['an instance variable', '@var'], | ||
['a class variable', '@@var'], | ||
['a global variable', '$var'], | ||
['an attribute', 'self.var'] | ||
].each do |type, var| | ||
it "does not register an offense with or-assignment to #{type}" do | ||
expect_no_offenses(<<~RUBY) | ||
#{var} ||= 1 | ||
RUBY | ||
end | ||
end | ||
end |