gitlab-styles 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88825b6aaa4961f16a29bfc14cdb1d590700a74e81fe68e072fccd2046506c6b
4
- data.tar.gz: 5634afcb51a117e54f4760c43f12d3b2564d08b62bbcd49d97b7cde7c88014d6
3
+ metadata.gz: 7ffdef3ce888f639822f2931093c590fa0ef9979fc38ec290f4a7b4a18393b55
4
+ data.tar.gz: d22d5faa3fbcafe4a899ad1f8315813a5b8dd0d2e9c0baa2aa80cfcf2c56f12a
5
5
  SHA512:
6
- metadata.gz: 99652ceb45ae8e31ac3105bd7913a2c85e0a5a22cd97ad5d30081fd1b55b4d1734671d9cdbfdf1875dbdbc314efb9fe433b8859a18761ebea1d1054f58aa88d1
7
- data.tar.gz: 2b16d7dc347ec66fd1656d735bc6427c485c88e27d7fb3030e0a852d76fb338705a976e85fb949b25b3994035134251da3771a9f3c914767d6d7158ecf5137e4
6
+ metadata.gz: 31dc65dd2a632d76307e85f9350babbda6032e9934fa9a6a363a5aecab344ef744a741f5f69c185eea7826391cfea4ad5f02ccda540f35b3b0baf9cecf331e65
7
+ data.tar.gz: b39f7fead8a619e687543a451a31870b9f3c05f7c06e88b16e2d120c5ae28e71e33edbc333660ce5e8560daee737870f7049f7f2cde776ad21bfd092efab52b6
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.add_dependency 'rubocop', '~> 0.91.1'
25
+ spec.add_dependency 'rubocop', '~> 0.91', '>= 0.91.1'
26
26
  spec.add_dependency 'rubocop-gitlab-security', '~> 0.1.1'
27
27
  spec.add_dependency 'rubocop-performance', '~> 1.9.2'
28
28
  spec.add_dependency 'rubocop-rails', '~> 2.9'
@@ -44,7 +44,6 @@ module Gitlab
44
44
  ids: false,
45
45
  includes: true,
46
46
  joins: true,
47
- limit: true,
48
47
  lock: false,
49
48
  many?: false,
50
49
  offset: true,
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab
4
+ module Styles
5
+ module Rubocop
6
+ module Cop
7
+ module Style
8
+ # This cop identifies places where `map { ... }.to_h` or
9
+ # `Hash[map { ... }]` can be replaced with `to_h { ... }`,
10
+ # saving an intermediate array allocation.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # hash.map { |k, v| [v.upcase, k.downcase] }.to_h
15
+ # hash.collect { |k, v| [v.upcase, k.downcase] }.to_h
16
+ # Hash[hash.map { |k, v| [v.upcase, k.downcase] }]
17
+ # Hash[hash.collect { |k, v| [v.upcase, k.downcase] }]
18
+ # array.map { |x| [x, x + 1] }.to_h
19
+ #
20
+ # # good
21
+ # hash.to_h { |k, v| [v.upcase, k.downcase] }
22
+ # array.to_h { |x| [x, x + 1] }
23
+ #
24
+ # Full credit: https://github.com/eugeneius/rubocop-performance/blob/hash_transformation/lib/rubocop/cop/performance/hash_transformation.rb
25
+ class HashTransformation < RuboCop::Cop::Cop
26
+ include RuboCop::Cop::RangeHelp
27
+
28
+ MSG = 'Use `to_h { ... }` instead of `%<current>s`.'
29
+
30
+ def_node_matcher :to_h_candidate?, <<~PATTERN
31
+ {
32
+ [(send
33
+ $(block $(send _ {:map :collect}) ...) :to_h) !block_literal?]
34
+ (send (const nil? :Hash) :[]
35
+ $(block $(send _ {:map :collect}) ...))
36
+ }
37
+ PATTERN
38
+
39
+ def on_send(node)
40
+ to_h_candidate?(node) do |_block, call|
41
+ range = offense_range(node, call)
42
+ message = message(node, call)
43
+ add_offense(node, location: range, message: message)
44
+ end
45
+ end
46
+
47
+ def autocorrect(node)
48
+ block, call = to_h_candidate?(node)
49
+
50
+ lambda do |corrector|
51
+ corrector.remove(after_block(node, block))
52
+ corrector.replace(call.loc.selector, 'to_h')
53
+ corrector.remove(before_block(node, block))
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def offense_range(node, call)
60
+ return node.source_range if node.children.first.const_type?
61
+
62
+ range_between(call.loc.selector.begin_pos, node.loc.selector.end_pos)
63
+ end
64
+
65
+ def message(node, call)
66
+ current = if node.children.first.const_type?
67
+ "Hash[#{call.method_name} { ... }]"
68
+ else
69
+ "#{call.method_name} { ... }.to_h"
70
+ end
71
+
72
+ format(MSG, current: current)
73
+ end
74
+
75
+ def after_block(node, block)
76
+ block.source_range.end.join(node.source_range.end)
77
+ end
78
+
79
+ def before_block(node, block)
80
+ node.source_range.begin.join(block.source_range.begin)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gitlab
4
4
  module Styles
5
- VERSION = '6.0.0'
5
+ VERSION = '6.1.0'
6
6
  end
7
7
  end
data/rubocop-style.yml CHANGED
@@ -187,6 +187,13 @@ Style/HashTransformKeys:
187
187
  Style/HashTransformValues:
188
188
  Enabled: true
189
189
 
190
+
191
+ # This cop identifies places where `map { ... }.to_h` or
192
+ # `Hash[map { ... }]` can be replaced with `to_h { ... }`,
193
+ # saving an intermediate array allocation.
194
+ Style/HashTransformation:
195
+ Enabled: true
196
+
190
197
  # Checks that conditional statements do not have an identical line at the
191
198
  # end of each branch, which can validly be moved out of the conditional.
192
199
  Style/IdenticalConditionalBranches:
metadata CHANGED
@@ -1,20 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-styles
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-07 00:00:00.000000000 Z
11
+ date: 2021-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.91'
20
+ - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 0.91.1
20
23
  type: :runtime
@@ -22,6 +25,9 @@ dependencies:
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.91'
30
+ - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 0.91.1
27
33
  - !ruby/object:Gem::Dependency
@@ -166,6 +172,7 @@ files:
166
172
  - lib/gitlab/styles/rubocop/cop/rspec/have_link_parameters.rb
167
173
  - lib/gitlab/styles/rubocop/cop/rspec/single_line_hook.rb
168
174
  - lib/gitlab/styles/rubocop/cop/rspec/verbose_include_metadata.rb
175
+ - lib/gitlab/styles/rubocop/cop/style/hash_transformation.rb
169
176
  - lib/gitlab/styles/rubocop/cop/without_reactive_cache.rb
170
177
  - lib/gitlab/styles/rubocop/migration_helpers.rb
171
178
  - lib/gitlab/styles/rubocop/model_helpers.rb