forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Gemspec/AddRuntimeDependency
cop
Follow up rubygems/rubygems#7799 (comment). Prefer `add_dependency` over `add_runtime_dependency` because `add_runtime_dependency` is considered soft-deprecated. ```ruby # bad Gem::Specification.new do |spec| spec.add_runtime_dependency('rubocop') end # good Gem::Specification.new do |spec| spec.add_dependency('rubocop') end ``` rubocop/ruby-style-guide#943 has been opened for the Style Guide.
- Loading branch information
Showing
6 changed files
with
104 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#13030](https://github.com/rubocop/rubocop/pull/13030): Add new `Gemspec/AddRuntimeDependency` cop. ([@koic][]) |
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Gemspec | ||
# Prefer `add_dependency` over `add_runtime_dependency` because | ||
# `add_dependency` is considered soft-deprecated. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# Gem::Specification.new do |spec| | ||
# spec.add_runtime_dependency('rubocop') | ||
# end | ||
# | ||
# # good | ||
# Gem::Specification.new do |spec| | ||
# spec.add_dependency('rubocop') | ||
# end | ||
# | ||
class AddRuntimeDependency < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `add_dependency` instead of `add_runtime_dependency`.' | ||
|
||
RESTRICT_ON_SEND = %i[add_runtime_dependency].freeze | ||
|
||
def on_send(node) | ||
return if !node.receiver || node.arguments.empty? | ||
|
||
add_offense(node.loc.selector) do |corrector| | ||
corrector.replace(node.loc.selector, 'add_dependency') | ||
end | ||
end | ||
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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Gemspec::AddRuntimeDependency, :config do | ||
it 'registers an offense when using `add_runtime_dependency`' do | ||
expect_offense(<<~RUBY) | ||
Gem::Specification.new do |spec| | ||
spec.add_runtime_dependency('rubocop') | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `add_dependency` instead of `add_runtime_dependency`. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Gem::Specification.new do |spec| | ||
spec.add_dependency('rubocop') | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `add_dependency`' do | ||
expect_no_offenses(<<~RUBY) | ||
Gem::Specification.new do |spec| | ||
spec.add_dependency('rubocop') | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `add_development_dependency`' do | ||
expect_no_offenses(<<~RUBY) | ||
Gem::Specification.new do |spec| | ||
spec.add_development_dependency('rubocop') | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `add_runtime_dependency` without receiver' do | ||
expect_no_offenses(<<~RUBY) | ||
add_runtime_dependency('rubocop') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `add_runtime_dependency` without arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
spec.add_runtime_dependency | ||
RUBY | ||
end | ||
end |