Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow WordArray to support a minimum array size #603

Merged
merged 1 commit into from
Nov 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* `FinalNewline` cop does auto-correction. ([@jonas054][])
* New cop `CyclomaticComplexity` checks the cyclomatic complexity of methods against a configurable max value. ([@jonas054][])
* [#594](https://github.com/bbatsov/rubocop/pull/594): New parameter `EnforcedStyleForEmptyBraces` with values `space` and `no_space` (default) added to `SpaceAroundBlockBraces`. ([@jonas054][])
* [#603](https://github.com/bbatsov/rubocop/pull/603): New parameter `MinSize` added to `WordArray` to allow small string arrays, retaining the default (0). ([@claco][])

### Changes

Expand Down
9 changes: 6 additions & 3 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ AssignmentInCondition:
BlockNesting:
Max: 3

BracesAroundHashParameters:
# Valid values are: braces, no_braces
EnforcedStyle: no_braces

ClassLength:
CountComments: false # count full line comments?
Max: 100
Expand Down Expand Up @@ -205,6 +209,5 @@ VariableName:
# Valid values are: snake_case, camelCase
EnforcedStyle: snake_case

BracesAroundHashParameters:
# Valid values are: braces, no_braces
EnforcedStyle: no_braces
WordArray:
MinSize: 0
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/word_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def on_array(node)

string_array = array_elems.all? { |e| e.type == :str }

if string_array && !complex_content?(array_elems)
if string_array && !complex_content?(array_elems) &&
array_elems.size > min_size
convention(node, :expression)
end
end
Expand All @@ -36,6 +37,10 @@ def complex_content?(arr_sexp)

false
end

def min_size
cop_config['MinSize']
end
end
end
end
Expand Down
13 changes: 11 additions & 2 deletions spec/rubocop/cop/style/word_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

require 'spec_helper'

describe Rubocop::Cop::Style::WordArray do
subject(:cop) { described_class.new }
describe Rubocop::Cop::Style::WordArray, :config do
subject(:cop) { described_class.new(config) }
let(:cop_config) { { 'MinSize' => 0 } }

it 'registers an offence for arrays of single quoted strings' do
inspect_source(cop,
Expand Down Expand Up @@ -52,4 +53,12 @@
['["", "two", "three"]'])
expect(cop.offences).to be_empty
end

it 'does not register an offence for array with allowed number of strings' do
cop_config['MinSize'] = 3

inspect_source(cop,
['["one", "two", "three"]'])
expect(cop.offences).to be_empty
end
end