rubocop-performance 1.22.0 → 1.22.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f39eb7cfffa29b6374302e0fae0b46f55a1ccce24798fa2609c4f80c3dea277
|
4
|
+
data.tar.gz: 87b1a993f80ae214d981e95cc5a7645f4ea92dca4255f64fa53ec29f5959500e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99a4776f28e154fe3017ed3e97be8b9121a262e8f40af1aed8d4085fc5ba4e1be7499ea12fe3d76e6858ac45ff88e031dfce6dc129743b52d5fd5815da596775
|
7
|
+
data.tar.gz: 9160234e8fcdf23f550d41d6429910e09276806f40ec494331d52e5f8480b2d066ac8ede04bc0144c8dc9e9e3a2fce16a8b64746e9ed0c794c278e3426d76a7b
|
@@ -3,22 +3,28 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Performance
|
6
|
-
# Identifies places where
|
7
|
-
#
|
8
|
-
#
|
6
|
+
# Identifies places where a float argument to BigDecimal should be converted to a string.
|
7
|
+
# Initializing from String is faster than from Float for BigDecimal.
|
8
|
+
#
|
9
|
+
# Also identifies places where an integer string argument to BigDecimal should be converted to
|
10
|
+
# an integer. Initializing from Integer is faster than from String for BigDecimal.
|
9
11
|
#
|
10
12
|
# @example
|
11
13
|
# # bad
|
12
|
-
# BigDecimal(
|
13
|
-
#
|
14
|
+
# BigDecimal(1.2, 3, exception: true)
|
15
|
+
# 4.5.to_d(6, exception: true)
|
16
|
+
#
|
17
|
+
# # good
|
14
18
|
# BigDecimal('1.2', 3, exception: true)
|
15
19
|
# BigDecimal('4.5', 6, exception: true)
|
16
20
|
#
|
21
|
+
# # bad
|
22
|
+
# BigDecimal('1', 2)
|
23
|
+
# BigDecimal('4', 6)
|
24
|
+
#
|
17
25
|
# # good
|
18
26
|
# BigDecimal(1, 2)
|
19
27
|
# 4.to_d(6)
|
20
|
-
# BigDecimal(1.2, 3, exception: true)
|
21
|
-
# 4.5.to_d(6, exception: true)
|
22
28
|
#
|
23
29
|
class BigDecimalWithNumericArgument < Base
|
24
30
|
extend AutoCorrector
|
@@ -26,30 +32,45 @@ module RuboCop
|
|
26
32
|
|
27
33
|
minimum_target_ruby_version 3.1
|
28
34
|
|
29
|
-
|
35
|
+
MSG_FROM_FLOAT_TO_STRING = 'Convert float literal to string and pass it to `BigDecimal`.'
|
36
|
+
MSG_FROM_INTEGER_TO_STRING = 'Convert string literal to integer and pass it to `BigDecimal`.'
|
30
37
|
RESTRICT_ON_SEND = %i[BigDecimal to_d].freeze
|
31
38
|
|
32
|
-
def_node_matcher :big_decimal_with_numeric_argument
|
33
|
-
(send nil? :BigDecimal $str_type? ...)
|
39
|
+
def_node_matcher :big_decimal_with_numeric_argument, <<~PATTERN
|
40
|
+
(send nil? :BigDecimal ${float_type? str_type?} ...)
|
34
41
|
PATTERN
|
35
42
|
|
36
|
-
def_node_matcher :to_d
|
37
|
-
(send [!nil? $str_type?] :to_d ...)
|
43
|
+
def_node_matcher :to_d, <<~PATTERN
|
44
|
+
(send [!nil? ${float_type? str_type?}] :to_d ...)
|
38
45
|
PATTERN
|
39
46
|
|
47
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
|
40
48
|
def on_send(node)
|
41
|
-
if (
|
42
|
-
|
43
|
-
|
49
|
+
if (numeric = big_decimal_with_numeric_argument(node))
|
50
|
+
if numeric.numeric_type?
|
51
|
+
add_offense(numeric, message: MSG_FROM_FLOAT_TO_STRING) do |corrector|
|
52
|
+
corrector.wrap(numeric, "'", "'")
|
53
|
+
end
|
54
|
+
elsif numeric.value.match?(/\A\d+\z/)
|
55
|
+
add_offense(numeric, message: MSG_FROM_INTEGER_TO_STRING) do |corrector|
|
56
|
+
corrector.replace(numeric, numeric.value)
|
57
|
+
end
|
44
58
|
end
|
45
|
-
elsif (
|
46
|
-
|
47
|
-
|
59
|
+
elsif (numeric_to_d = to_d(node))
|
60
|
+
if numeric_to_d.numeric_type?
|
61
|
+
add_offense(numeric_to_d, message: MSG_FROM_FLOAT_TO_STRING) do |corrector|
|
62
|
+
big_decimal_args = node.arguments.map(&:source).unshift("'#{numeric_to_d.source}'").join(', ')
|
48
63
|
|
49
|
-
|
64
|
+
corrector.replace(node, "BigDecimal(#{big_decimal_args})")
|
65
|
+
end
|
66
|
+
elsif numeric_to_d.value.match?(/\A\d+\z/)
|
67
|
+
add_offense(numeric_to_d, message: MSG_FROM_INTEGER_TO_STRING) do |corrector|
|
68
|
+
corrector.replace(node, "#{numeric_to_d.value}.to_d")
|
69
|
+
end
|
50
70
|
end
|
51
71
|
end
|
52
72
|
end
|
73
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
|
53
74
|
end
|
54
75
|
end
|
55
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-performance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.22.
|
4
|
+
version: 1.22.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2024-09-
|
13
|
+
date: 2024-09-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|