Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
module RuboCop | ||
module Cop | ||
module Bundler | ||
# Gems in consecutive lines should be alphabetically sorted | ||
# @example | ||
# # bad | ||
# gem 'rubocop' | ||
# gem 'rspec' | ||
# | ||
# # good | ||
# gem 'rspec' | ||
# gem 'rubocop' | ||
# | ||
# # good | ||
# gem 'rubocop' | ||
# | ||
# gem 'rspec' | ||
class OrderedGems < Cop | ||
MSG = 'Gem `%s` should appear before `%s` in their gem group.'.freeze | ||
def investigate(processed_source) | ||
return if processed_source.ast.nil? | ||
gem_declarations(processed_source.ast) | ||
.each_cons(2) do |previous, current| | ||
next unless consecutive_lines(previous, current) | ||
next unless current.children[2].children.first.to_s < | ||
previous.children[2].children.first.to_s | ||
register_offense(previous, current) | ||
end | ||
end | ||
|
||
def consecutive_lines(previous, current) | ||
previous.source_range.last_line == current.source_range.first_line - 1 | ||
end | ||
|
||
def register_offense(previous, current) | ||
add_offense( | ||
current, | ||
current.source_range, | ||
format( | ||
MSG, | ||
current.children[2].children.first, | ||
previous.children[2].children.first | ||
) | ||
) | ||
end | ||
|
||
def_node_search :gem_declarations, <<-PATTERN | ||
(:send, nil, :gem, ...) | ||
PATTERN | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Bundler::OrderedGems, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'When gems are alphabetically sorted' do | ||
let(:source) { <<-END } | ||
gem 'rspec' | ||
gem 'rubocop' | ||
END | ||
|
||
it 'does not register any offenses' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses).to be_empty | ||
end | ||
end | ||
|
||
context 'When gems are not alphabetically sorted' do | ||
let(:source) { <<-END } | ||
gem 'rubocop' | ||
gem 'rspec' | ||
END | ||
|
||
it 'registers an offense' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'mentions both gem names in message' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses.first.message).to include('rspec') | ||
expect(cop.offenses.first.message).to include('rubocop') | ||
end | ||
|
||
it 'highlights the second gem' do | ||
inspect_source(cop, source) | ||
expect(cop.highlights).to eq(["gem 'rspec'"]) | ||
end | ||
end | ||
|
||
context 'When each individual group of line is sorted' do | ||
let(:source) { <<-END } | ||
gem 'rspec' | ||
gem 'rubocop' | ||
gem 'hello' | ||
gem 'world' | ||
END | ||
|
||
it 'does not register any offenses' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses).to be_empty | ||
end | ||
end | ||
|
||
context 'When a gem declaration takes several lines' do | ||
let(:source) { <<-END } | ||
gem 'rubocop', | ||
'0.1.1' | ||
gem 'rspec' | ||
END | ||
|
||
it 'registers an offense' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
end | ||
|
||
context 'When the gemfile is empty' do | ||
let(:source) { <<-END } | ||
# Gemfile | ||
END | ||
|
||
it 'does not register any offenses' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses.size).to eq(0) | ||
end | ||
end | ||
end |