From e42403dce6e186037e0a0b4e2508226426739e70 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Mon, 17 May 2021 20:43:31 +0530 Subject: [PATCH] [Fix #9803] Fix `Bundler/GemVersion` cop not respecting git tags Closes #9803 --- .../fix_gem_version_not_respecting_tags.md | 1 + lib/rubocop/cop/bundler/gem_version.rb | 26 +++++++++++++++++-- spec/rubocop/cop/bundler/gem_version_spec.rb | 13 ++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_gem_version_not_respecting_tags.md diff --git a/changelog/fix_gem_version_not_respecting_tags.md b/changelog/fix_gem_version_not_respecting_tags.md new file mode 100644 index 00000000000..0022e4b1d5d --- /dev/null +++ b/changelog/fix_gem_version_not_respecting_tags.md @@ -0,0 +1 @@ +* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane][]) diff --git a/lib/rubocop/cop/bundler/gem_version.rb b/lib/rubocop/cop/bundler/gem_version.rb index 6d73edbf303..a18aa945eaf 100644 --- a/lib/rubocop/cop/bundler/gem_version.rb +++ b/lib/rubocop/cop/bundler/gem_version.rb @@ -45,6 +45,21 @@ class GemVersion < Base (send nil? :gem <(str #version_specification?) ...>) PATTERN + # @!method with_git_ref?(node) + def_node_matcher :with_git_ref?, <<~PATTERN + (send nil? :gem <(hash <#git? #tag_ref? ...>) ...>) + PATTERN + + # @!method tag_ref?(node) + def_node_matcher :tag_ref?, <<~PATTERN + (pair (sym {:tag :ref}) (str _)) + PATTERN + + # @!method git?(node) + def_node_matcher :git?, <<~PATTERN + (pair (sym {:git :github :bitbucket}) (str _)) + PATTERN + def on_send(node) return unless gem_declaration?(node) return if allowed_gem?(node) @@ -78,8 +93,15 @@ def message(range) end def offense?(node) - (required_style? && !includes_version_specification?(node)) || - (forbidden_style? && includes_version_specification?(node)) + required_offense?(node) || forbidden_offense?(node) + end + + def required_offense?(node) + required_style? && !includes_version_specification?(node) && !with_git_ref?(node) + end + + def forbidden_offense?(node) + forbidden_style? && includes_version_specification?(node) end def forbidden_style? diff --git a/spec/rubocop/cop/bundler/gem_version_spec.rb b/spec/rubocop/cop/bundler/gem_version_spec.rb index 840dbd2e68b..7e58913a8ae 100644 --- a/spec/rubocop/cop/bundler/gem_version_spec.rb +++ b/spec/rubocop/cop/bundler/gem_version_spec.rb @@ -15,6 +15,8 @@ ^^^^^^^^^^^^^ Gem version specification is required. gem 'rubocop', require: false ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required. + gem 'rubocop', tag: '1.2.0' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required. RUBY end @@ -24,6 +26,9 @@ gem 'rubocop', '~> 1' gem 'rubocop', '~> 1.12', require: false gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop' + gem 'rubocop', github: 'rubocop/rubocop', tag: 'v1' + gem 'rubocop', git: 'https://github.com/rubocop/rubocop', ref: 'b3f37bc7f' + gem 'foobar', bitbucket: 'foo/bar', tag: 'v1' RUBY end @@ -67,5 +72,13 @@ gem 'rspec', '~> 3.10' RUBY end + + it 'does not flag gems using git source with tag or ref' do + expect_no_offenses(<<~RUBY) + gem 'rubocop', github: 'rubocop/rubocop', tag: 'v1' + gem 'rubocop', git: 'https://github.com/rubocop/rubocop', ref: 'b3f37bc7f' + gem 'foobar', bitbucket: 'foo/bar', tag: 'v1' + RUBY + end end end