Skip to content

Commit ce3d448

Browse files
koicbbatsov
authored andcommitted
Add new Gemspec/AddRuntimeDependency cop
Follow up ruby/rubygems#7799 (comment). Prefer `add_dependency` over `add_runtime_dependency` as the latter 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.
1 parent d92c51f commit ce3d448

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13030](https://github.com/rubocop/rubocop/pull/13030): Add new `Gemspec/AddRuntimeDependency` cop. ([@koic][])

config/default.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ Bundler/OrderedGems:
262262

263263
#################### Gemspec ###############################
264264

265+
Gemspec/AddRuntimeDependency:
266+
Description: 'Prefer `add_dependency` over `add_runtime_dependency`.'
267+
StyleGuide: '#add_dependency_vs_add_runtime_dependency'
268+
Reference: https://github.com/rubygems/rubygems/issues/7799#issuecomment-2192720316
269+
Enabled: pending
270+
VersionAdded: '<<next>>'
271+
Include:
272+
- '**/*.gemspec'
273+
265274
Gemspec/DependencyVersion:
266275
Description: 'Requires or forbids specifying gem dependency versions.'
267276
Enabled: false

lib/rubocop.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
require_relative 'rubocop/cop/bundler/insecure_protocol_source'
170170
require_relative 'rubocop/cop/bundler/ordered_gems'
171171

172+
require_relative 'rubocop/cop/gemspec/add_runtime_dependency'
172173
require_relative 'rubocop/cop/gemspec/dependency_version'
173174
require_relative 'rubocop/cop/gemspec/deprecated_attribute_assignment'
174175
require_relative 'rubocop/cop/gemspec/development_dependencies'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Gemspec
6+
# Prefer `add_dependency` over `add_runtime_dependency` as the latter is
7+
# considered soft-deprecated.
8+
#
9+
# @example
10+
#
11+
# # bad
12+
# Gem::Specification.new do |spec|
13+
# spec.add_runtime_dependency('rubocop')
14+
# end
15+
#
16+
# # good
17+
# Gem::Specification.new do |spec|
18+
# spec.add_dependency('rubocop')
19+
# end
20+
#
21+
class AddRuntimeDependency < Base
22+
extend AutoCorrector
23+
24+
MSG = 'Use `add_dependency` instead of `add_runtime_dependency`.'
25+
26+
RESTRICT_ON_SEND = %i[add_runtime_dependency].freeze
27+
28+
def on_send(node)
29+
return if !node.receiver || node.arguments.empty?
30+
31+
add_offense(node.loc.selector) do |corrector|
32+
corrector.replace(node.loc.selector, 'add_dependency')
33+
end
34+
end
35+
end
36+
end
37+
end
38+
end

rubocop.gemspec

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ Gem::Specification.new do |s|
3131
'rubygems_mfa_required' => 'true'
3232
}
3333

34-
s.add_runtime_dependency('json', '~> 2.3')
35-
s.add_runtime_dependency('language_server-protocol', '>= 3.17.0')
36-
s.add_runtime_dependency('parallel', '~> 1.10')
37-
s.add_runtime_dependency('parser', '>= 3.3.0.2')
38-
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
39-
s.add_runtime_dependency('regexp_parser', '>= 2.4', '< 3.0')
40-
s.add_runtime_dependency('rexml', '>= 3.2.5', '< 4.0')
41-
s.add_runtime_dependency('rubocop-ast', '>= 1.31.1', '< 2.0')
42-
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
43-
s.add_runtime_dependency('unicode-display_width', '>= 2.4.0', '< 3.0')
34+
s.add_dependency('json', '~> 2.3')
35+
s.add_dependency('language_server-protocol', '>= 3.17.0')
36+
s.add_dependency('parallel', '~> 1.10')
37+
s.add_dependency('parser', '>= 3.3.0.2')
38+
s.add_dependency('rainbow', '>= 2.2.2', '< 4.0')
39+
s.add_dependency('regexp_parser', '>= 2.4', '< 3.0')
40+
s.add_dependency('rexml', '>= 3.2.5', '< 4.0')
41+
s.add_dependency('rubocop-ast', '>= 1.31.1', '< 2.0')
42+
s.add_dependency('ruby-progressbar', '~> 1.7')
43+
s.add_dependency('unicode-display_width', '>= 2.4.0', '< 3.0')
4444
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Gemspec::AddRuntimeDependency, :config do
4+
it 'registers an offense when using `add_runtime_dependency`' do
5+
expect_offense(<<~RUBY)
6+
Gem::Specification.new do |spec|
7+
spec.add_runtime_dependency('rubocop')
8+
^^^^^^^^^^^^^^^^^^^^^^ Use `add_dependency` instead of `add_runtime_dependency`.
9+
end
10+
RUBY
11+
12+
expect_correction(<<~RUBY)
13+
Gem::Specification.new do |spec|
14+
spec.add_dependency('rubocop')
15+
end
16+
RUBY
17+
end
18+
19+
it 'does not register an offense when using `add_dependency`' do
20+
expect_no_offenses(<<~RUBY)
21+
Gem::Specification.new do |spec|
22+
spec.add_dependency('rubocop')
23+
end
24+
RUBY
25+
end
26+
27+
it 'does not register an offense when using `add_development_dependency`' do
28+
expect_no_offenses(<<~RUBY)
29+
Gem::Specification.new do |spec|
30+
spec.add_development_dependency('rubocop')
31+
end
32+
RUBY
33+
end
34+
35+
it 'does not register an offense when using `add_runtime_dependency` without receiver' do
36+
expect_no_offenses(<<~RUBY)
37+
add_runtime_dependency('rubocop')
38+
RUBY
39+
end
40+
41+
it 'does not register an offense when using `add_runtime_dependency` without arguments' do
42+
expect_no_offenses(<<~RUBY)
43+
spec.add_runtime_dependency
44+
RUBY
45+
end
46+
end

0 commit comments

Comments
 (0)