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 new Gemspec/DuplicatedAssignment cop #5087

Merged

Conversation

koic
Copy link
Member

@koic koic commented Nov 22, 2017

Feature

An attribute assignment method calls should be listed only once in a gemspec.

Assigning to an attribute with the same name using spec.foo = will be an unintended usage. On the other hand, duplication of methods such as spec.requirements, spec.add_runtime_dependency and others are permitted because it is the intended use of appending values.

Bad case

Gem::Specification.new do |spec|
  spec.name = 'rubocop'
  spec.name = 'rubocop2'
end

Good case

Gem::Specification.new do |spec|
  spec.name = 'rubocop'
end
Gem::Specification.new do |spec|
  spec.requirements << 'libmagick, v6.0'
  spec.requirements << 'A good graphics
end
Gem::Specification.new do |spec|
  spec.add_runtime_dependency('parallel', '~> 1.10')
  spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
end

How to use

% cat foo.gemspec
# frozen_string_literal: true

Gem::Specification.new do |spec|
  spec.name = 'rubocop'
  spec.name = 'rubocop2'
end
% rubocop foo.gemspec
Inspecting 1 file
C

Offenses:

/tmp/foo.gemspec:5:3: C: Gemspec/DuplicatedAssignment: name= method
calls already given on line 4 of the gemspec.
  spec.name = 'rubocop2'
  ^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

The implementation approach is based on Bundler/DuplicatedGem.
https://github.com/bbatsov/rubocop/blob/v0.51.0/lib/rubocop/cop/bundler/duplicated_gem.rb

Other Information

RubyGems Specification Reference
http://guides.rubygems.org/specification-reference/


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

koic and others added 3 commits November 22, 2017 18:40
## Feature

An attribute assignment method calls should be listed only once in a
gemspec.

Assigning to an attribute with the same name using `spec.foo =` will be
an unintended usage. On the other hand, duplication of methods such
as `spec.requirements`, `spec.add_runtime_dependency` and others are
permitted because it is the intended use of appending values.

### Bad case

```ruby
Gem::Specification.new do |spec|
  spec.name = 'rubocop'
  spec.name = 'rubocop2'
end
```

### Good case

```ruby
Gem::Specification.new do |spec|
  spec.name = 'rubocop'
end
```

```ruby
Gem::Specification.new do |spec|
  spec.requirements << 'libmagick, v6.0'
  spec.requirements << 'A good graphics
end
```

```ruby
Gem::Specification.new do |spec|
  spec.add_runtime_dependency('parallel', '~> 1.10')
  spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
end
```

### How to use

```console
% cat foo.gemspec
# frozen_string_literal: true

Gem::Specification.new do |spec|
  spec.name = 'rubocop'
  spec.name = 'rubocop2'
end
```

```console
% rubocop foo.gemspec
Inspecting 1 file
C

Offenses:

/tmp/foo.gemspec:5:3: C: Gemspec/DuplicatedAssignment: name= method
calls already given on line 4 of the gemspec.
  spec.name = 'rubocop2'
  ^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

The implementation approach is based on `Bundler/DuplicatedGem`.
https://github.com/bbatsov/rubocop/blob/v0.51.0/lib/rubocop/cop/bundler/duplicated_gem.rb

## Other Information

RubyGems Specification Reference
http://guides.rubygems.org/specification-reference/
@bbatsov bbatsov merged commit 2b43944 into rubocop:master Nov 22, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 22, 2017

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants