Skip to content

Commit

Permalink
[Fix rubocop#5053] Naming/ConstantName allows non-offensive casgn ass…
Browse files Browse the repository at this point in the history
…ignment
  • Loading branch information
garettarrowood committed Nov 15, 2017
1 parent 044173e commit 39da4fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* [#5025](https://github.com/bbatsov/rubocop/issues/5025): Fix error with Layout/MultilineMethodCallIndentation cop and lambda.(...). ([@dpostorivo][])
* [#4781](https://github.com/bbatsov/rubocop/issues/4781): Prevent `Style/UnneededPercentQ` from breaking on strings that are concated with backslash. ([@pocke][])
* [#4363](https://github.com/bbatsov/rubocop/issues/4363): Fix `Style/PercentLiteralDelimiters` incorrectly automatically modifies escaped percent literal delimiter. ([@koic][])
* [#5053](https://github.com/bbatsov/rubocop/issues/5053): Fix `Naming/ConstantName` false offense on assigning to a nonoffensive assignment. ([@garettarrowood][])
* [#5019](https://github.com/bbatsov/rubocop/pull/5019): Fix auto-correct for `Style/HashSyntax` cop when hash is used as unspaced argument. ([@drenmi][])

### Changes
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/naming/constant_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def on_casgn(node)
# NewClass = something_that_returns_a_class
# It's also ok to assign a class constant another class constant
# SomeClass = SomeOtherClass
return if value && %i[send block const].include?(value.type)
return if value && %i[send block const casgn].include?(value.type)

add_offense(node, location: :name) if const_name !~ SNAKE_CASE
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/naming/constant_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
RUBY
end

it 'registers 1 offense if rhs is offending const assignment' do
expect_offense(<<-RUBY.strip_indent)
Bar = Foo = 4
^^^ Use SCREAMING_SNAKE_CASE for constants.
RUBY
end

it 'allows screaming snake case in const name' do
expect_no_offenses('TOP_TEST = 5')
end
Expand All @@ -49,6 +56,12 @@
expect_no_offenses('Parser::CurrentRuby = Parser::Ruby21')
end

it 'does not check if rhs is a non-offensive const assignment' do
expect_no_offenses(<<-RUBY.strip_indent)
Bar = Foo = Qux
RUBY
end

it 'checks qualified const names' do
expect_offense(<<-RUBY.strip_indent)
::AnythingGoes = 30
Expand Down

0 comments on commit 39da4fc

Please sign in to comment.