Skip to content

Commit

Permalink
[Fix #8207] Don't consider underscores and dashes when sorting gems (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Jun 30, 2020
1 parent bad9b4c commit 0ccb16a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,11 @@
* [#8223](https://github.com/rubocop-hq/rubocop/pull/8223): Support auto-correction for `Style/IfUnlessModifierOfIfUnless`. ([@koic][])
* [#8172](https://github.com/rubocop-hq/rubocop/pull/8172): Support auto-correction for `Lint/SafeNavigationWithEmpty`. ([@koic][])

### Changes

* [#7868](https://github.com/rubocop-hq/rubocop/pull/7868): **(Breaking)** Extensive refactoring of internal classes `Team`, `Commissioner`, `Corrector`. `Cop::Cop#corrections` not completely compatible. See Upgrade Notes. ([@marcandre][])
* [#8207](https://github.com/rubocop-hq/rubocop/pull/8207): **(Breaking)** Order for gems names now disregards underscores and dashes unless `ConsiderPunctuation` setting is set to `true`. ([@marcandre][])

### Bug fixes

* [#8196](https://github.com/rubocop-hq/rubocop/issues/8196): Fix a false positive for `Style/RedundantFetchBlock` when using with `Rails.cache`. ([@fatkodima][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Expand Up @@ -180,6 +180,9 @@ Bundler/OrderedGems:
VersionAdded: '0.46'
VersionChanged: '0.47'
TreatCommentsAsGroupSeparators: true
# By default, "-" and "_" are ignored for order purposes.
# This can be overriden by setting this parameter to true.
ConsiderPunctuation: false
Include:
- '**/*.gemfile'
- '**/Gemfile'
Expand All @@ -200,6 +203,9 @@ Gemspec/OrderedDependencies:
Enabled: true
VersionAdded: '0.51'
TreatCommentsAsGroupSeparators: true
# By default, "-" and "_" are ignored for order purposes.
# This can be overriden by setting this parameter to true.
ConsiderPunctuation: false
Include:
- '**/*.gemspec'

Expand Down
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/cops_bundler.adoc
Expand Up @@ -245,6 +245,10 @@ gem 'rspec'
| `true`
| Boolean

| ConsiderPunctuation
| `false`
| Boolean

| Include
| `+**/*.gemfile+`, `+**/Gemfile+`, `+**/gems.rb+`
| Array
Expand Down
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/cops_gemspec.adoc
Expand Up @@ -131,6 +131,10 @@ spec.add_dependency 'rspec'
| `true`
| Boolean

| ConsiderPunctuation
| `false`
| Boolean

| Include
| `+**/*.gemspec+`
| Array
Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/mixin/ordered_gem_node.rb
Expand Up @@ -15,8 +15,13 @@ def get_source_range(node, comments_as_separators)
node.source_range
end

def gem_canonical_name(name)
name = name.tr('-_', '') unless cop_config['ConsiderPunctuation']
name.downcase
end

def case_insensitive_out_of_order?(string_a, string_b)
string_a.downcase < string_b.downcase
gem_canonical_name(string_a) < gem_canonical_name(string_b)
end

def consecutive_lines(previous, current)
Expand Down
36 changes: 34 additions & 2 deletions spec/rubocop/cop/bundler/ordered_gems_spec.rb
Expand Up @@ -175,11 +175,11 @@
end
end

context 'When gems are asciibetically sorted' do
context 'When gems are asciibetically sorted irrespective of _' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
gem 'paper_trail'
gem 'paperclip'
gem 'paper_trail'
RUBY
end
end
Expand Down Expand Up @@ -208,6 +208,38 @@
end
end

context 'When a gem is sorted but not so when disregarding _-' do
context 'by default' do
it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'active-admin-some_plugin'
gem 'active_admin_other_plugin'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `active_admin_other_plugin` should appear before `active-admin-some_plugin`.
gem 'activeadmin'
^^^^^^^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `activeadmin` should appear before `active_admin_other_plugin`.
RUBY

expect_correction(<<~RUBY)
gem 'activeadmin'
gem 'active_admin_other_plugin'
gem 'active-admin-some_plugin'
RUBY
end
end

context 'when ConsiderPunctuation is true' do
let(:cop_config) { super().merge({ 'ConsiderPunctuation' => true }) }

it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
gem 'active-admin-some_plugin'
gem 'active_admin_other_plugin'
gem 'activeadmin'
RUBY
end
end
end

context 'When there are duplicated gems in group' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 0ccb16a

Please sign in to comment.