-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Performance/BigDecimalString
cop
- Loading branch information
Showing
7 changed files
with
114 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
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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where numeric argument to BigDecimal should be | ||
# converted to string. Initializing from String is faster | ||
# than from Numeric for BigDecimal. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# BigDecimal(1, 2) | ||
# BigDecimal(1.2, 3, exception: true) | ||
# | ||
# # good | ||
# BigDecimal('1', 2) | ||
# BigDecimal('1.2', 3, exception: true) | ||
# | ||
class BigDecimalString < Cop | ||
MSG = 'Convert numeric argument to string before passing to `BigDecimal`.' | ||
|
||
def_node_matcher :big_decimal_string_candidate?, <<~PATTERN | ||
(send nil? :BigDecimal $numeric_type? ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
big_decimal_string_candidate?(node) do |numeric| | ||
add_offense(node, location: numeric.source_range) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
big_decimal_string_candidate?(node) do |numeric| | ||
lambda do |corrector| | ||
corrector.wrap(numeric, "'", "'") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::BigDecimalString do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when using `BigDecimal` with integer' do | ||
expect_offense(<<~RUBY) | ||
BigDecimal(1) | ||
^ Convert numeric argument to string before passing to `BigDecimal`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
BigDecimal('1') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `BigDecimal` with float' do | ||
expect_offense(<<~RUBY) | ||
BigDecimal(1.5, 2, exception: true) | ||
^^^ Convert numeric argument to string before passing to `BigDecimal`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
BigDecimal('1.5', 2, exception: true) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `BigDecimal` with string' do | ||
expect_no_offenses(<<~RUBY) | ||
BigDecimal('1') | ||
RUBY | ||
end | ||
end |