Skip to content

Commit

Permalink
[Fix rubocop#3699] Fix false positive in VariableNumber
Browse files Browse the repository at this point in the history
Style/VariableNumber would report false positives on variable names ending with
an underscore.
  • Loading branch information
bquorning committed Nov 4, 2016
1 parent 83798ac commit 2058f03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug fixes

* [#3662](https://github.com/bbatsov/rubocop/issues/3662): Fix the auto-correction of `Lint/UnneededSplatExpansion` when the splat expansion is inside of another array. ([@rrosenblum][])
* [#3699](https://github.com/bbatsov/rubocop/issues/3699): Fix false positive in `Style/VariableNumber` on variable names ending with an underscore. ([@bquorning][])

## 0.45.0 (2016-10-31)

Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/mixin/configurable_numbering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Cop
module ConfigurableNumbering
include ConfigurableEnforcedStyle

SNAKE_CASE = /(?:^_|[a-z]|_\d+)$/
NORMAL_CASE = /(?:^_|[A-Za-z]\d*)$/
NON_INTEGER = /(?:^_|[A-Za-z])$/
SNAKE_CASE = /(?:[a-z_]|_\d+)$/
NORMAL_CASE = /(?:_\D*|[A-Za-z]\d*)$/
NON_INTEGER = /[A-Za-z_]$/

def check_name(node, name, name_range)
return if operator?(name)
Expand Down
11 changes: 8 additions & 3 deletions spec/rubocop/cop/style/variable_number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
let(:cop_config) { { 'EnforcedStyle' => 'snake_case' } }

it_behaves_like :offense, 'snake_case', 'local1'
it_behaves_like :offense, 'snake_case', 'local_'
it_behaves_like :offense, 'snake_case', '@local1'
it_behaves_like :offense, 'snake_case', '@@local1'
it_behaves_like :offense, 'snake_case', 'camelCase1'
Expand All @@ -37,11 +36,14 @@
it_behaves_like :accepts, 'snake_case', 'local_1'
it_behaves_like :accepts, 'snake_case', 'local_12'
it_behaves_like :accepts, 'snake_case', 'local_123'
it_behaves_like :accepts, 'snake_case', 'local_'
it_behaves_like :accepts, 'snake_case', 'aB_1'
it_behaves_like :accepts, 'snake_case', 'a_1_b'
it_behaves_like :accepts, 'snake_case', 'a_1_b_1'
it_behaves_like :accepts, 'snake_case', '_'
it_behaves_like :accepts, 'snake_case', '_foo'
it_behaves_like :accepts, 'snake_case', '@foo'
it_behaves_like :accepts, 'snake_case', '@__foo__'

it 'registers an offense for normal case numbering in method parameter' do
inspect_source(cop, 'def method(arg1); end')
Expand All @@ -61,7 +63,6 @@
let(:cop_config) { { 'EnforcedStyle' => 'normalcase' } }

it_behaves_like :offense, 'normalcase', 'local_1'
it_behaves_like :offense, 'normalcase', 'local_'
it_behaves_like :offense, 'normalcase', 'sha_256'
it_behaves_like :offense, 'normalcase', '@local_1'
it_behaves_like :offense, 'normalcase', '@@local_1'
Expand All @@ -72,6 +73,7 @@
it_behaves_like :offense, 'normalcase', 'local_FOO_1'

it_behaves_like :accepts, 'normalcase', 'local1'
it_behaves_like :accepts, 'normalcase', 'local_'
it_behaves_like :accepts, 'normalcase', 'user1_id'
it_behaves_like :accepts, 'normalcase', 'sha256'
it_behaves_like :accepts, 'normalcase', 'foo10_bar'
Expand All @@ -81,6 +83,8 @@
it_behaves_like :accepts, 'normalcase', 'user_1_id'
it_behaves_like :accepts, 'normalcase', '_'
it_behaves_like :accepts, 'normalcase', '_foo'
it_behaves_like :accepts, 'normalcase', '@foo'
it_behaves_like :accepts, 'normalcase', '@__foo__'

it 'registers an offense for snake case numbering in method parameter' do
inspect_source(cop, 'def method(arg_1); end')
Expand All @@ -100,7 +104,6 @@
let(:cop_config) { { 'EnforcedStyle' => 'non_integer' } }

it_behaves_like :offense, 'non_integer', 'local_1'
it_behaves_like :offense, 'non_integer', 'local_'
it_behaves_like :offense, 'non_integer', 'local1'
it_behaves_like :offense, 'non_integer', '@local_1'
it_behaves_like :offense, 'non_integer', '@local1'
Expand All @@ -113,11 +116,13 @@

it_behaves_like :accepts, 'non_integer', 'localone'
it_behaves_like :accepts, 'non_integer', 'local_one'
it_behaves_like :accepts, 'non_integer', 'local_'
it_behaves_like :accepts, 'non_integer', '@foo'
it_behaves_like :accepts, 'non_integer', '@@foo'
it_behaves_like :accepts, 'non_integer', 'fooBar'
it_behaves_like :accepts, 'non_integer', '_'
it_behaves_like :accepts, 'non_integer', '_foo'
it_behaves_like :accepts, 'non_integer', '@__foo__'

it 'registers an offense for snake case numbering in method parameter' do
inspect_source(cop, 'def method(arg_1); end')
Expand Down

0 comments on commit 2058f03

Please sign in to comment.