Skip to content

Commit

Permalink
Add new UnifiedInteger cop (rubocop#3492)
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke authored and bbatsov committed Sep 17, 2016
1 parent 5833952 commit 57273d9
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Add check for `persisted?` method call when using a create method in `Rails/SaveBang`. ([@QuinnHarris][])
* Add new `Style/SafeNavigation` cop to convert method calls safeguarded by a non `nil` check for the object to `&.`. ([@rrosenblum][])
* Add new `Performance/SortWithBlock` cop to use `sort_by(&:foo)` instead of `sort_by { |a, b| a.foo <=> b.foo }`. ([@koic][])
* [#3492](https://github.com/bbatsov/rubocop/pull/3492): Add new `UnifiedInteger` cop. ([@pocke][])

### Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,10 @@ Lint/UnderscorePrefixedVariableName:
Description: 'Do not use prefix `_` for a variable that is used.'
Enabled: true

Lint/UnifiedInteger:
Description: 'Use Integer instead of Fixnum or Bignum'
Enabled: true

Lint/UnneededDisable:
Description: >-
Checks for rubocop:disable comments that can be removed.
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
require 'rubocop/cop/lint/string_conversion_in_interpolation'
require 'rubocop/cop/lint/syntax'
require 'rubocop/cop/lint/underscore_prefixed_variable_name'
require 'rubocop/cop/lint/unified_integer'
require 'rubocop/cop/lint/unneeded_disable'
require 'rubocop/cop/lint/unneeded_splat_expansion'
require 'rubocop/cop/lint/unreachable_code'
Expand Down
38 changes: 38 additions & 0 deletions lib/rubocop/cop/lint/unified_integer.rb
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
72 changes: 72 additions & 0 deletions spec/rubocop/cop/lint/unified_integer_spec.rb
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

0 comments on commit 57273d9

Please sign in to comment.