-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
land #14898, Add rubocop rule to correct
Gem::Version
to `Rex::Vers…
…ion`
- Loading branch information
Showing
7 changed files
with
108 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
class DeprecatedGemVersion < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `Rex::Version` instead of `Gem::Version`.' | ||
|
||
# @!method gem_version_const(node) | ||
def_node_matcher :gem_version_const, <<~PATTERN | ||
(const | ||
$(const {nil? cbase} :Gem) {:Version}) | ||
PATTERN | ||
|
||
# @!method gem_version_const_cbase(node) | ||
def_node_matcher :gem_version_const_cbase, <<~PATTERN | ||
(const | ||
$(const {cbase} :Gem) {:Version}) | ||
PATTERN | ||
|
||
def on_const(node) | ||
return unless gem_version_const(node) | ||
|
||
add_offense(node, message: MSG) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect(corrector, node) | ||
if gem_version_const_cbase(node) | ||
corrector.replace(gem_version_const_cbase(node), '::Rex') | ||
else | ||
corrector.replace(gem_version_const(node), 'Rex') | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'spec_helper' | ||
require 'rubocop/cop/lint/deprecated_gem_version' | ||
|
||
RSpec.describe RuboCop::Cop::Lint::DeprecatedGemVersion do | ||
subject(:cop) { described_class.new(config) } | ||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'corrects `Gem::Version`' do | ||
expect_offense(<<~RUBY) | ||
Gem::Version | ||
^^^^^^^^^^^^ Use `Rex::Version` instead of `Gem::Version`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rex::Version | ||
RUBY | ||
end | ||
|
||
it 'corrects `Gem::Version.new`' do | ||
expect_offense(<<~RUBY) | ||
Gem::Version.new("1.0.0") | ||
^^^^^^^^^^^^ Use `Rex::Version` instead of `Gem::Version`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rex::Version.new("1.0.0") | ||
RUBY | ||
end | ||
|
||
it 'corrects `::Gem::Version`' do | ||
expect_offense(<<~RUBY) | ||
::Gem::Version | ||
^^^^^^^^^^^^^^ Use `Rex::Version` instead of `Gem::Version`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::Rex::Version | ||
RUBY | ||
end | ||
|
||
it 'does not correct `Abc::Gem::Version`' do | ||
expect_no_offenses(<<~RUBY) | ||
Abc::Gem::Version | ||
RUBY | ||
end | ||
end |