From 39da4fcd4c66cfedb95d7f01e380d2433f2ec368 Mon Sep 17 00:00:00 2001 From: Garett Arrowood Date: Tue, 14 Nov 2017 18:13:14 -0500 Subject: [PATCH] [Fix #5053] Naming/ConstantName allows non-offensive casgn assignment --- CHANGELOG.md | 1 + lib/rubocop/cop/naming/constant_name.rb | 2 +- spec/rubocop/cop/naming/constant_name_spec.rb | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac3c21c35f2b..e9615914b44a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/naming/constant_name.rb b/lib/rubocop/cop/naming/constant_name.rb index 451c4d315a39..2b5176b4952b 100644 --- a/lib/rubocop/cop/naming/constant_name.rb +++ b/lib/rubocop/cop/naming/constant_name.rb @@ -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 diff --git a/spec/rubocop/cop/naming/constant_name_spec.rb b/spec/rubocop/cop/naming/constant_name_spec.rb index df050f406b1e..287b028f328d 100644 --- a/spec/rubocop/cop/naming/constant_name_spec.rb +++ b/spec/rubocop/cop/naming/constant_name_spec.rb @@ -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 @@ -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