Skip to content

Commit

Permalink
Fix Bundler/OrderedGems autocorrect behavior
Browse files Browse the repository at this point in the history
  ## Summary

  This PR improves `Bundler/OrderedGems` autocorrect  behavior on the
  specific  situation.
  When group includes duplicate gems, and one of duplicate gems is
  in the first line of group, and one of them has
  offense `Bundler/OrderedGems`.
  The cop replace the gem out of group and the gem in group.

  ### Before

  For example, the target Gemfile is like below.

  ```ruby
  gem 'a'

  group :development do
    gem 'b'
    gem 'c'
    gem 'b'
  end
  ```

  ### Expected

  Expected result of `rubocop -a --only Bundler/OrderedGems`
  is like below.

  ```ruby
  gem 'a'

  group :development do
    gem 'b'
    gem 'b'
    gem 'c'
  end
  ```

  ### Actual

  But actual result is like below.

  ```ruby
  gem 'b'

  group :development do
    gem 'a'
    gem 'b'
    gem 'c'
  end
  ```
  • Loading branch information
colorbox authored and bbatsov committed Mar 8, 2018
1 parent dff44de commit 8ec6cf6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#5623](https://github.com/bbatsov/rubocop/pull/5623): Fix `Bundler/OrderedGems` when a group includes duplicate gems. ([@colorbox][])

## 0.53.0 (2018-03-05)

### New features
Expand Down Expand Up @@ -3225,3 +3229,4 @@
[@satyap]: https://github.com/satyap
[@unkmas]: https://github.com/unkmas
[@elebow]: https://github.com/elebow
[@colorbox]: https://github.com/colorbox
2 changes: 1 addition & 1 deletion lib/rubocop/cop/bundler/ordered_gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def autocorrect(node)

def previous_declaration(node)
declarations = gem_declarations(processed_source.ast)
node_index = declarations.find_index(node)
node_index = declarations.map(&:location).find_index(node.location)
declarations.to_a[node_index - 1]
end

Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/bundler/ordered_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,42 @@
RUBY
end
end

context 'When there are duplicated gems in group' do
let(:source) { <<-RUBY.strip_indent }
gem 'a'
group :development do
gem 'b'
gem 'c'
gem 'b'
end
RUBY

it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
gem 'a'
group :development do
gem 'b'
gem 'c'
gem 'b'
^^^^^^^ #{format(message, 'b', 'c')}
end
RUBY
end

it 'autocorrects' do
new_source = autocorrect_source_with_loop(source)
expect(new_source).to eq(<<-RUBY.strip_indent)
gem 'a'
group :development do
gem 'b'
gem 'b'
gem 'c'
end
RUBY
end
end
end

0 comments on commit 8ec6cf6

Please sign in to comment.