Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option OnlyFor to the Bundler/GemComment cop. #7978

Merged
merged 21 commits into from May 31, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c3bfc90
Add tests for the new configuration.
ric2b May 15, 2020
91e6211
Update the cop to support 'OnlyIfVersionRestricted' configuration.
ric2b May 15, 2020
c1c90df
Update the cop's documentation.
ric2b May 15, 2020
acab071
Update docs.
ric2b May 15, 2020
e7ed684
Update CHANGELOG.md
ric2b May 15, 2020
c64c375
Fix documentation mistakes.
ric2b May 15, 2020
db46a0f
Generalize the configuration and support checking keyword arguments.
ric2b May 21, 2020
638178d
Update docs and changelog.
ric2b May 21, 2020
1cfcb73
Fix typo in changelog update.
ric2b May 24, 2020
3323708
Simplify `version_specified_gem?`
ric2b May 24, 2020
4153cd9
Update test context name to be more consistent
ric2b May 24, 2020
d52e515
Update test context name to be more consistent
ric2b May 24, 2020
7769628
Clarify `checked_options_present?` boolean expression
ric2b May 24, 2020
cd3e921
Use more rspec contexts to make test cases more readable.
ric2b May 24, 2020
dd9ec11
Merge branch 'master' into gem-comment-cop-add-pinned-only-option
ric2b May 26, 2020
c4bcc3a
Merge branch 'master' into gem-comment-cop-add-pinned-only-option
ric2b May 30, 2020
0f9be81
Merge remote-tracking branch 'origin/master' into gem-comment-cop-add…
ric2b May 30, 2020
4ffcb8b
update documentation
ric2b May 30, 2020
e5597a6
Simplify option names and update version changed to 0.85.
ric2b May 31, 2020
a308431
Add common options and link to bundler documentation.
ric2b May 31, 2020
965c6bd
Merge branch 'master' into gem-comment-cop-add-pinned-only-option
ric2b May 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### New features

* [#7978](https://github.com/rubocop-hq/rubocop/pull/7978): Add new option `OnlyIfVersionRestricted` to the `Bumdler/GemComment` cop. ([@ric2b][])
* [#7735](https://github.com/rubocop-hq/rubocop/issues/7735): `NodePattern` and `AST` classes have been moved to the [`rubocop-ast` gem](https://github.com/rubocop-hq/rubocop-ast). ([@marcandre][])
* [#7950](https://github.com/rubocop-hq/rubocop/pull/7950): Add new `Lint/DeprecatedOpenSSLConstant` cop. ([@bdewater][])

Expand Down Expand Up @@ -4524,3 +4525,4 @@
[@jeffcarbs]: https://github.com/jeffcarbs
[@laurmurclar]: https://github.com/laurmurclar
[@jethrodaniel]: https://github.com/jethrodaniel
[@ric2b]: https://github.com/ric2b
20 changes: 19 additions & 1 deletion lib/rubocop/cop/bundler/gem_comment.rb
Expand Up @@ -5,7 +5,7 @@ module Cop
module Bundler
# Add a comment describing each gem in your Gemfile.
#
# @example
# @example OnlyIfVersionRestricted: false (default)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also expand the cop summary.

# # bad
#
# gem 'foo'
Expand All @@ -15,6 +15,16 @@ module Bundler
# # Helpers for the foo things.
# gem 'foo'
#
# @example OnlyIfVersionRestricted: true
# # bad
#
# gem 'foo', '>= 2.1'
#
# # good
#
# # Version 2.1 introduces breaking change bar
# gem 'foo', '< 2.1'
#
class GemComment < Cop
include DefNode

Expand All @@ -26,6 +36,7 @@ def on_send(node)
return unless gem_declaration?(node)
return if ignored_gem?(node)
return if commented?(node)
return if cop_config['OnlyIfVersionRestricted'] && !version_restricted_gem?(node)

add_offense(node)
end
Expand Down Expand Up @@ -58,6 +69,13 @@ def ignored_gem?(node)
ignored_gems = Array(cop_config['IgnoredGems'])
ignored_gems.include?(node.first_argument.value)
end

# Besides the gem name, all other *positional* arguments to `gem` are version restrictions,
# as long as it has one we know there's a version restriction.
def version_restricted_gem?(send_node)
# arguments[0] is the gem name
send_node.arguments[1]&.str_type? == true
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions manual/cops_bundler.md
Expand Up @@ -49,6 +49,8 @@ Add a comment describing each gem in your Gemfile.

### Examples

#### OnlyIfVersionRestricted: false (default)

```ruby
# bad

Expand All @@ -59,6 +61,18 @@ gem 'foo'
# Helpers for the foo things.
gem 'foo'
```
#### OnlyIfVersionRestricted: true

```ruby
# bad

gem 'foo', '>= 2.1'

# good

# Version 2.1 introduces breaking change bar
gem 'foo', '< 2.1'
```

### Configurable attributes

Expand Down
56 changes: 56 additions & 0 deletions spec/rubocop/cop/bundler/gem_comment_spec.rb
Expand Up @@ -63,5 +63,61 @@
GEM
end
end

context 'and the OnlyIfVersionRestricted option is set to true' do
before { cop_config['OnlyIfVersionRestricted'] = true }

context 'and a gem is commented' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, 'Gemfile')
# Style-guide enforcer.
gem 'rubocop'
RUBY
end
end

context 'and a gem is uncommented but not version restricted' do
it 'does not register an offense in a simple example' do
expect_no_offenses(<<-GEM, 'Gemfile')
gem 'rubocop'
GEM
end

it 'does not register an offense in a more complex example' do
expect_no_offenses(<<-GEM, 'Gemfile')
gem 'rubocop', group: development
GEM
end
end

context 'and a gem is uncommented and version restricted' do
context 'and has no other paramaters' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', '~> 12.0'
^^^^^^^^^^^^^^^^^^^^^^^^ Missing gem description comment.
GEM
end
end

context 'and has multiple version restrictions' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', '~> 12.0', '>= 11.0'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Missing gem description comment.
GEM
end
end

context 'and has extra unrelated keyword arguments' do
it 'registers an offense' do
expect_offense(<<-GEM, 'Gemfile')
gem 'rubocop', '~> 12.0', required: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Missing gem description comment.
GEM
end
end
end
end
ric2b marked this conversation as resolved.
Show resolved Hide resolved
end
end