rubocop-performance 1.22.1 → 1.23.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/config/default.yml +6 -0
- data/lib/rubocop/cop/performance/count.rb +2 -2
- data/lib/rubocop/cop/performance/redundant_match.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_string_chars.rb +1 -1
- data/lib/rubocop/cop/performance/squeeze.rb +5 -1
- data/lib/rubocop/cop/performance/string_bytesize.rb +45 -0
- data/lib/rubocop/cop/performance/sum.rb +1 -1
- data/lib/rubocop/cop/performance_cops.rb +1 -0
- data/lib/rubocop/performance/version.rb +1 -1
- metadata +5 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 332ca643753a81ff66c4a6050117f7a479bd3aaa3f186c361a596d06b16aa9b5
|
4
|
+
data.tar.gz: 2fb149f85f986d9d0302e6fbe4c7389f312bb93a15a93cec49ffd74495853c78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e03b8e178f4cde75ef5d82aa7483da512f2816afd82c97b414a4bfcd7afacf589ebb033d6bed40d0968217ec0ff24cb69e2a6c4f28a52ecbdb31cef5599e5386
|
7
|
+
data.tar.gz: 410a72e5e85fe6cdc8002b51c8b185dc4d2a1dea5f3a5889c146364c2a20b5b50256c2a90fef59604137dd75d83f20c17ec65991eac6b8dab15e046184e1f006
|
data/LICENSE.txt
CHANGED
data/config/default.yml
CHANGED
@@ -326,6 +326,12 @@ Performance/StartWith:
|
|
326
326
|
VersionAdded: '0.36'
|
327
327
|
VersionChanged: '1.10'
|
328
328
|
|
329
|
+
Performance/StringBytesize:
|
330
|
+
Description: "Use `String#bytesize` instead of calculating the size of the bytes array."
|
331
|
+
Safe: false
|
332
|
+
Enabled: 'pending'
|
333
|
+
VersionAdded: '1.23'
|
334
|
+
|
329
335
|
Performance/StringIdentifierArgument:
|
330
336
|
Description: 'Use symbol identifier argument instead of string identifier argument.'
|
331
337
|
Enabled: pending
|
@@ -18,14 +18,14 @@ module RuboCop
|
|
18
18
|
#
|
19
19
|
# [source,ruby]
|
20
20
|
# ----
|
21
|
-
#
|
21
|
+
# Model.where(id: [1, 2, 3]).select { |m| m.method == true }.size
|
22
22
|
# ----
|
23
23
|
#
|
24
24
|
# becomes:
|
25
25
|
#
|
26
26
|
# [source,ruby]
|
27
27
|
# ----
|
28
|
-
#
|
28
|
+
# Model.where(id: [1, 2, 3]).to_a.count { |m| m.method == true }
|
29
29
|
# ----
|
30
30
|
#
|
31
31
|
# @example
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
|
73
73
|
def requires_parentheses?(arg)
|
74
74
|
return true if arg.if_type? && arg.ternary?
|
75
|
-
return true if arg.
|
75
|
+
return true if arg.operator_keyword? || arg.range_type?
|
76
76
|
|
77
77
|
call_like?(arg) && requires_parentheses_for_call_like?(arg)
|
78
78
|
end
|
@@ -46,7 +46,11 @@ module RuboCop
|
|
46
46
|
message = format(MSG, current: bad_method, prefer: good_method)
|
47
47
|
|
48
48
|
add_offense(node.loc.selector, message: message) do |corrector|
|
49
|
-
|
49
|
+
# FIXME: When requiring only RuboCop 1.70.0 and above,
|
50
|
+
# `dup` in `replace_str.dup` becomes unnecessary, as
|
51
|
+
# frozen strings are handled in the `to_string_literal`
|
52
|
+
# implementation. Please remove it.
|
53
|
+
string_literal = to_string_literal(replace_str.dup)
|
50
54
|
new_code = "#{receiver.source}#{node.loc.dot.source}#{good_method}(#{string_literal})"
|
51
55
|
|
52
56
|
corrector.replace(node, new_code)
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Performance
|
6
|
+
# Checks for calls to `#bytes` counting method and suggests using `bytesize` instead.
|
7
|
+
# The `bytesize` method is more efficient and directly returns the size in bytes,
|
8
|
+
# avoiding the intermediate array allocation that `bytes.size` incurs.
|
9
|
+
#
|
10
|
+
# @safety
|
11
|
+
# This cop is unsafe because it assumes that the receiver
|
12
|
+
# responds to `#bytesize` method.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# # bad
|
16
|
+
# string_var.bytes.count
|
17
|
+
# "foobar".bytes.size
|
18
|
+
#
|
19
|
+
# # good
|
20
|
+
# string_var.bytesize
|
21
|
+
# "foobar".bytesize
|
22
|
+
class StringBytesize < Base
|
23
|
+
extend AutoCorrector
|
24
|
+
|
25
|
+
MSG = 'Use `String#bytesize` instead of calculating the size of the bytes array.'
|
26
|
+
RESTRICT_ON_SEND = %i[size length count].freeze
|
27
|
+
|
28
|
+
def_node_matcher :string_bytes_method?, <<~MATCHER
|
29
|
+
(call (call !{nil? int} :bytes) {:size :length :count})
|
30
|
+
MATCHER
|
31
|
+
|
32
|
+
def on_send(node)
|
33
|
+
string_bytes_method?(node) do
|
34
|
+
range = node.receiver.loc.selector.begin.join(node.source_range.end)
|
35
|
+
|
36
|
+
add_offense(range) do |corrector|
|
37
|
+
corrector.replace(range, 'bytesize')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
alias on_csend on_send
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -44,6 +44,7 @@ require_relative 'performance/select_map'
|
|
44
44
|
require_relative 'performance/size'
|
45
45
|
require_relative 'performance/sort_reverse'
|
46
46
|
require_relative 'performance/squeeze'
|
47
|
+
require_relative 'performance/string_bytesize'
|
47
48
|
require_relative 'performance/start_with'
|
48
49
|
require_relative 'performance/string_identifier_argument'
|
49
50
|
require_relative 'performance/string_include'
|
metadata
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-performance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.23.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
8
8
|
- Jonas Arvidsson
|
9
9
|
- Yuji Nakayama
|
10
|
-
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2025-01-04 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: rubocop
|
@@ -112,6 +111,7 @@ files:
|
|
112
111
|
- lib/rubocop/cop/performance/sort_reverse.rb
|
113
112
|
- lib/rubocop/cop/performance/squeeze.rb
|
114
113
|
- lib/rubocop/cop/performance/start_with.rb
|
114
|
+
- lib/rubocop/cop/performance/string_bytesize.rb
|
115
115
|
- lib/rubocop/cop/performance/string_identifier_argument.rb
|
116
116
|
- lib/rubocop/cop/performance/string_include.rb
|
117
117
|
- lib/rubocop/cop/performance/string_replacement.rb
|
@@ -130,10 +130,9 @@ metadata:
|
|
130
130
|
homepage_uri: https://docs.rubocop.org/rubocop-performance/
|
131
131
|
changelog_uri: https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md
|
132
132
|
source_code_uri: https://github.com/rubocop/rubocop-performance/
|
133
|
-
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.
|
133
|
+
documentation_uri: https://docs.rubocop.org/rubocop-performance/1.23/
|
134
134
|
bug_tracker_uri: https://github.com/rubocop/rubocop-performance/issues
|
135
135
|
rubygems_mfa_required: 'true'
|
136
|
-
post_install_message:
|
137
136
|
rdoc_options: []
|
138
137
|
require_paths:
|
139
138
|
- lib
|
@@ -148,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
147
|
- !ruby/object:Gem::Version
|
149
148
|
version: '0'
|
150
149
|
requirements: []
|
151
|
-
rubygems_version: 3.
|
152
|
-
signing_key:
|
150
|
+
rubygems_version: 3.6.1
|
153
151
|
specification_version: 4
|
154
152
|
summary: Automatic performance checking tool for Ruby code.
|
155
153
|
test_files: []
|