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

[Fix #7669] Add Bundler/GemVersion cop #9727

Merged
merged 8 commits into from May 5, 2021

Conversation

timlkelly
Copy link
Contributor

@timlkelly timlkelly commented Apr 22, 2021

  • Bundler/GemVersion enforces that gem version declarations are either required or prohibited in the Gemfile.
  • Bundler/GemVersion is disabled by default.
  • Gems can be omitted from this check by adding the gem to the AllowedGems configuration option.

I originally wrote this cop for a private repository and then added more configuration options after seeing issue #7669.

The AST checks and regex caught everything I could throw at it, but I realize that is probably limited compared to the larger Ruby ecosystem as a whole. I would appreciate any feedback you may have on those checks.

References:
#9720
#7669


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

- 'required'
- 'prohibited'
Include:
- '**/Gemfile'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I saw that other Bundler cops also include *.gemfile and gems.rb. I've personally never worked with this format of gem file, so I was unsure what exactly to test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The format is exactly the same, it's just a different filename.

@timlkelly
Copy link
Contributor Author

The CircleCI builds do not provide a link to check their status. Is there a way I can check on their status? They seem to be queued but haven't been processed as far as I can tell.

class GemVersionDeclaration < Base
include ConfigurableEnforcedStyle

REQUIRED_MSG = 'Gem version declaration is required.'
Copy link
Collaborator

Choose a reason for hiding this comment

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

declaration -> specification.

EnforcedStyle: 'required'
SupportedStyles:
- 'required'
- 'prohibited'
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we've typically used "forbidden" in the codebase, but you'll have to check this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forbidden definitely seems to be more common than prohibited, good to know!

- 'prohibited'
Include:
- '**/Gemfile'
IgnoredGems: []
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lately I've favoured more names like "AllowedGems" instead of "IgnoredGems". Sadly, our use of both is a bit inconsistent.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for AllowedGems.

@bbatsov
Copy link
Collaborator

bbatsov commented May 3, 2021

I'd probably name this just "GemVersion" or something like this. Overall, I think that's a nice cop to have.

@bbatsov bbatsov requested review from dvandersluis and koic May 3, 2021 08:38

REQUIRED_MSG = 'Gem version declaration is required.'
PROHIBITED_MSG = 'Gem version declaration is prohibited.'
VERSION_DECLARATION_REGEX = /^[~<>=]*\s?[0-9.]+/.freeze
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
VERSION_DECLARATION_REGEX = /^[~<>=]*\s?[0-9.]+/.freeze
VERSION_DECLARATION_REGEX = /^\s*[~<>=]*\s*[0-9.]+/.freeze

Multiple spaces are valid in bundler before and after the operator. We also could be more specific about the format here, but it probably doesn't really matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added the suggested change.

We also could be more specific about the format here

Do you mind detailing what you mean by this? Or is it the change you provided?

Copy link
Member

Choose a reason for hiding this comment

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

What I mean is the Regexp will match ...... or >>>>> 7 or other random strings that aren’t version specifications, but again it probably doesn't really matter.

It could be more strict with something like ^\s*(?:~>|[<>]?=?)?\s*[0-9]+(\.[0-9]+)*

Comment on lines 42 to 43
# @!method gem_declaration?(node)
def_node_matcher :gem_declaration?, '(send nil? :gem str ...)'
Copy link
Member

Choose a reason for hiding this comment

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

This method is in Bundler::GemComment as well, it might be useful to extract them to a shared mixin.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, definitely. I did copy this from Bundler::GemComment to get it working originally. But I completely agree that a mixin will DRY this up.

@bbatsov bbatsov changed the title [Fix #7669] Add Bundler/GemVersionDeclaration Cop [Fix #7669] Add Bundler/GemVersion cop May 5, 2021
@bbatsov bbatsov merged commit 762655e into rubocop:master May 5, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented May 5, 2021

Great work! Thanks!

@timlkelly timlkelly deleted the gem-version-declaration branch May 5, 2021 17:46
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.

None yet

3 participants