Skip to content

Commit

Permalink
Edited regular expression for normal case to fix issues rubocop#3514
Browse files Browse the repository at this point in the history
…and rubocop#3516 (rubocop#3524)

[Fix rubocop#3514][Fix rubocop#3516] Edit regular expression which defines normal case by allowing numbers after the first underscore.

This also fixes the normal case analog to issue rubocop#3519 - changed and simplified normal case definition to allow multi-digit numbers.
  • Loading branch information
Brendan Good authored and Neodelf committed Oct 15, 2016
1 parent 80bdfdd commit 675d9f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
* [#3485](https://github.com/bbatsov/rubocop/issues/3485): Make OneLineConditional cop not register offense for empty else. ([@tejasbubane][])
* [#3508](https://github.com/bbatsov/rubocop/pull/3508): Fix false negatives in `Rails/NotNullColumn`. ([@pocke][])
* [#3462](https://github.com/bbatsov/rubocop/issues/3462): Don't create MultilineMethodCallBraceLayout offenses for single-line method calls when receiver spans multiple lines. ([@maxjacobson][])

* [#3514](https://github.com/bbatsov/rubocop/issues/3514): Make Style/VariableNumber cop not register an offense when valid normal case variable names have an integer after the first "_". ([@b-t-g][])
* [#3516](https://github.com/bbatsov/rubocop/issues/3516): Make Style/VariableNumber cop not register an offense when valid normal case variable names have an integer in the middle. ([@b-t-g][])
### Changes

* [#3341](https://github.com/bbatsov/rubocop/issues/3341): Exclude RSpec tests from inspection by `Style/NumericPredicate` cop. ([@drenmi][])
Expand Down Expand Up @@ -2377,3 +2378,4 @@
[@scottohara]: https://github.com/scottohara
[@koic]: https://github.com/koic
[@groddeck]: https://github.com/groddeck
[@b-t-g]: https://github.com/b-t-g
3 changes: 2 additions & 1 deletion lib/rubocop/cop/mixin/configurable_numbering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module ConfigurableNumbering
include ConfigurableEnforcedStyle

SNAKE_CASE = /^@{0,2}[-_a-z]+[_\D]*(_\d)*[!?=]?$/
NORMAL_CASE = /^@{0,2}-{0,1}_{0,1}[a-zA-Z\d]*[_\D]*[!?=]?$/
NORMAL_CASE = /^@{0,2}-{0,1}_{0,1}[a-zA-Z\d]*(_[a-zA-Z]+\d*)*
[_\D]*[!?=]?$/x
NON_INTEGER = /^@{0,2}[-_a-z]+[_\D]*[!?=]?$/

def check_name(node, name, name_range)
Expand Down
44 changes: 44 additions & 0 deletions spec/rubocop/cop/style/variable_number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,50 @@
expect(cop.offenses.size).to eq(0)
end

it 'does not register an offense for normal case number in local
variable' do
inspect_source(cop, 'user1_id = 1')
expect(cop.offenses.size).to eq(0)
end

it 'does not register an offense for normal case number in local
variable' do
inspect_source(cop, 'sha256 = 3')
expect(cop.offenses.size).to eq(0)
end

it 'does not register on offense for normal case multi digit number in
local variable' do
inspect_source(cop, 'foo10_bar = 4')
expect(cop.offenses.size).to eq(0)
end

it 'does not register on offense for normal case multi digit number in
local variable' do
inspect_source(cop, 'foo_bar10 = 4')
expect(cop.offenses.size).to eq(0)
end

it 'does not register an offense for normal case number in the middle of
local variable' do
inspect_source(cop, 'target_u2f_device = nil')
expect(cop.offenses.size).to eq(0)
end

it 'registers an offense for only integers in the middle' do
inspect_source(cop, 'user_1_id = 3')
expect(cop.offenses.size).to eq(1)
expect(cop.highlights).to eq(['user_1_id'])
expect(cop.messages).to eq(['Use normalcase for variable numbers.'])
end

it 'registers an offense for only integers at the end of name' do
inspect_source(cop, 'sha_256 = 1')
expect(cop.offenses.size).to eq(1)
expect(cop.highlights).to eq(['sha_256'])
expect(cop.messages).to eq(['Use normalcase for variable numbers.'])
end

it 'registers an offense for snake case numbering in instance variable' do
inspect_source(cop, '@local_1 = 3')
expect(cop.offenses.size).to eq(1)
Expand Down

0 comments on commit 675d9f2

Please sign in to comment.