forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
UnifiedInteger
cop (rubocop#3492)
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 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
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,38 @@ | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for using Fixnum or Bignum constant. | ||
# | ||
# @example | ||
# # bad | ||
# 1.is_a?(Fixnum) | ||
# 1.is_a?(Bignum) | ||
# | ||
# # good | ||
# 1.is_a?(Integer) | ||
class UnifiedInteger < Cop | ||
MSG = 'Use `Integer` instead of `%s`.'.freeze | ||
|
||
def_node_matcher :fixnum_or_bignum_const?, <<-PATTERN | ||
(:const {nil (:cbase)} ${:Fixnum :Bignum}) | ||
PATTERN | ||
|
||
def on_const(node) | ||
klass = fixnum_or_bignum_const?(node) | ||
return unless klass | ||
|
||
add_offense(node, :expression, format(MSG, klass)) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.replace(node.loc.name, 'Integer') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Lint::UnifiedInteger do | ||
let(:config) { RuboCop::Config.new } | ||
subject(:cop) { described_class.new(config) } | ||
|
||
shared_examples 'registers an offence' do |klass| | ||
context "when #{klass}" do | ||
context 'without any decorations' do | ||
let(:source) { "1.is_a?(#{klass})" } | ||
|
||
it 'registers an offence' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages).to eq(["Use `Integer` instead of `#{klass}`."]) | ||
end | ||
|
||
it 'autocorrects' do | ||
new_source = autocorrect_source(cop, source) | ||
expect(new_source).to eq('1.is_a?(Integer)') | ||
end | ||
end | ||
|
||
context 'when explicitly specified as toplevel constant' do | ||
let(:source) { "1.is_a?(::#{klass})" } | ||
|
||
it 'registers an offence' do | ||
inspect_source(cop, source) | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages).to eq(["Use `Integer` instead of `#{klass}`."]) | ||
end | ||
|
||
it 'autocorrects' do | ||
new_source = autocorrect_source(cop, source) | ||
expect(new_source).to eq('1.is_a?(::Integer)') | ||
end | ||
end | ||
|
||
context 'with MyNamespace' do | ||
let(:source) { "1.is_a?(MyNamespace::#{klass})" } | ||
|
||
include_examples 'accepts' | ||
end | ||
end | ||
end | ||
|
||
include_examples 'registers an offence', 'Fixnum' | ||
include_examples 'registers an offence', 'Bignum' | ||
|
||
context 'when Integer' do | ||
context 'without any decorations' do | ||
let(:source) { '1.is_a?(Integer)' } | ||
|
||
include_examples 'accepts' | ||
end | ||
|
||
context 'when explicitly specified as toplevel constant' do | ||
let(:source) { '1.is_a?(::Integer)' } | ||
|
||
include_examples 'accepts' | ||
end | ||
|
||
context 'with MyNamespace' do | ||
let(:source) { '1.is_a?(MyNamespace::Integer)' } | ||
|
||
include_examples 'accepts' | ||
end | ||
end | ||
end |