Skip to content

Commit

Permalink
[Fix #7576] Fix an error for Gemspec/OrderedDependencies
Browse files Browse the repository at this point in the history
Fixes #7576.

This PR fixes the following error for `Gemspec/OrderedDependencies`
when using a local variable in an argument of dependent gem.

```console
% cat example.gemspec
Gem::Specification.new do |s|
  %w[foo bar].each { |dep| s.add_dependency dep }
  s.add_dependency 'baz'
end

% rubocop --only Gemspec/OrderedDependencies example.gemspec -d
(nsip)

An error occurred while Gemspec/OrderedDependencies cop was inspecting
/Users/koic/src/github.com/koic/rubocop-issues/7576/example.gemspec.
undefined method `str_type?' for nil:NilClass
```

Since it is difficult to trace variables and list gem names, this PR
will skip them.

And `add_dependency`, `add_runtime_dependency`, and
`add_development_dependency` accept a symbol.

e.g. `add_dependency(:foo)`

The original implementation raises the same error if a symbol is passed
as an argument. This PR also solves that problem.

The issue for `add_dependency` and` abc` to accept symbols will
open as separate a PR.
  • Loading branch information
koic authored and bbatsov committed Dec 28, 2019
1 parent 73ac8bf commit af2d567
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* [#7193](https://github.com/rubocop-hq/rubocop/issues/7193): Prevent `Style/PercentLiteralDelimiters` from changing `%i` literals that contain escaped delimiters. ([@buehmann][])
* [#7590](https://github.com/rubocop-hq/rubocop/issues/7590): Fix an error for `Layout/SpaceBeforeBlockBraces` when using with `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop. ([@koic][])
* [#7569](https://github.com/rubocop-hq/rubocop/issues/7569): Make `Style/YodaCondition` accept `__FILE__ == $0`. ([@koic][])
* [#7576](https://github.com/rubocop-hq/rubocop/issues/7576): Fix an error for `Gemspec/OrderedDependencies` when using a local variable in an argument of dependent gem. ([@koic][])

## 0.78.0 (2019-12-18)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/gemspec/ordered_dependencies.rb
Expand Up @@ -98,7 +98,7 @@ def get_dependency_name(node)
end

def_node_search :dependency_declarations, <<~PATTERN
(send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} ...)
(send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} (str _) ...)
PATTERN
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb
Expand Up @@ -198,4 +198,15 @@
RUBY
end
end

context 'When using a local variable in an argument of dependent gem' do
it 'does not register any offenses' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
%w(rubocop-performance rubocop-rails).each { |dep| spec.add_dependency dep }
spec.add_dependency 'parser'
end
RUBY
end
end
end

0 comments on commit af2d567

Please sign in to comment.