gitlab-styles 7.1.0 → 8.0.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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gitlab/styles/rubocop/model_helpers.rb +1 -1
  3. data/lib/gitlab/styles/rubocop.rb +2 -2
  4. data/lib/gitlab/styles/version.rb +1 -1
  5. data/lib/rubocop/cop/active_record_dependent.rb +32 -0
  6. data/lib/rubocop/cop/active_record_serialize.rb +20 -0
  7. data/lib/rubocop/cop/avoid_return_from_blocks.rb +77 -0
  8. data/lib/rubocop/cop/code_reuse/active_record.rb +80 -0
  9. data/lib/rubocop/cop/custom_error_class.rb +69 -0
  10. data/lib/rubocop/cop/fips/md5.rb +27 -0
  11. data/lib/rubocop/cop/fips/open_ssl.rb +31 -0
  12. data/lib/rubocop/cop/fips/sha1.rb +27 -0
  13. data/lib/rubocop/cop/gem_fetcher.rb +37 -0
  14. data/lib/rubocop/cop/in_batches.rb +18 -0
  15. data/lib/rubocop/cop/internal_affairs/deprecate_cop_helper.rb +39 -0
  16. data/lib/rubocop/cop/line_break_after_guard_clauses.rb +100 -0
  17. data/lib/rubocop/cop/line_break_around_conditional_block.rb +128 -0
  18. data/lib/rubocop/cop/migration/update_large_table.rb +60 -0
  19. data/lib/rubocop/cop/performance/rubyzip.rb +35 -0
  20. data/lib/rubocop/cop/polymorphic_associations.rb +25 -0
  21. data/lib/rubocop/cop/rails/include_url_helper.rb +27 -0
  22. data/lib/rubocop/cop/redirect_with_status.rb +46 -0
  23. data/lib/rubocop/cop/rspec/base.rb +14 -0
  24. data/lib/rubocop/cop/rspec/empty_line_after_final_let_it_be.rb +47 -0
  25. data/lib/rubocop/cop/rspec/empty_line_after_let_block.rb +61 -0
  26. data/lib/rubocop/cop/rspec/empty_line_after_shared_example.rb +61 -0
  27. data/lib/rubocop/cop/rspec/example_starting_character.rb +120 -0
  28. data/lib/rubocop/cop/rspec/have_link_parameters.rb +44 -0
  29. data/lib/rubocop/cop/rspec/single_line_hook.rb +41 -0
  30. data/lib/rubocop/cop/rspec/verbose_include_metadata.rb +71 -0
  31. data/lib/rubocop/cop/style/hash_transformation.rb +83 -0
  32. data/lib/rubocop/cop/style/open_struct_use.rb +39 -0
  33. data/lib/rubocop/cop/without_reactive_cache.rb +16 -0
  34. metadata +31 -31
  35. data/lib/gitlab/styles/rubocop/cop/active_record_dependent.rb +0 -36
  36. data/lib/gitlab/styles/rubocop/cop/active_record_serialize.rb +0 -24
  37. data/lib/gitlab/styles/rubocop/cop/avoid_return_from_blocks.rb +0 -81
  38. data/lib/gitlab/styles/rubocop/cop/code_reuse/active_record.rb +0 -84
  39. data/lib/gitlab/styles/rubocop/cop/custom_error_class.rb +0 -73
  40. data/lib/gitlab/styles/rubocop/cop/fips/md5.rb +0 -31
  41. data/lib/gitlab/styles/rubocop/cop/fips/open_ssl.rb +0 -35
  42. data/lib/gitlab/styles/rubocop/cop/fips/sha1.rb +0 -31
  43. data/lib/gitlab/styles/rubocop/cop/gem_fetcher.rb +0 -41
  44. data/lib/gitlab/styles/rubocop/cop/in_batches.rb +0 -22
  45. data/lib/gitlab/styles/rubocop/cop/internal_affairs/deprecate_cop_helper.rb +0 -43
  46. data/lib/gitlab/styles/rubocop/cop/line_break_after_guard_clauses.rb +0 -104
  47. data/lib/gitlab/styles/rubocop/cop/line_break_around_conditional_block.rb +0 -132
  48. data/lib/gitlab/styles/rubocop/cop/migration/update_large_table.rb +0 -64
  49. data/lib/gitlab/styles/rubocop/cop/performance/rubyzip.rb +0 -39
  50. data/lib/gitlab/styles/rubocop/cop/polymorphic_associations.rb +0 -29
  51. data/lib/gitlab/styles/rubocop/cop/rails/include_url_helper.rb +0 -31
  52. data/lib/gitlab/styles/rubocop/cop/redirect_with_status.rb +0 -50
  53. data/lib/gitlab/styles/rubocop/cop/rspec/base.rb +0 -18
  54. data/lib/gitlab/styles/rubocop/cop/rspec/empty_line_after_final_let_it_be.rb +0 -51
  55. data/lib/gitlab/styles/rubocop/cop/rspec/empty_line_after_let_block.rb +0 -65
  56. data/lib/gitlab/styles/rubocop/cop/rspec/empty_line_after_shared_example.rb +0 -65
  57. data/lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb +0 -124
  58. data/lib/gitlab/styles/rubocop/cop/rspec/have_link_parameters.rb +0 -48
  59. data/lib/gitlab/styles/rubocop/cop/rspec/single_line_hook.rb +0 -45
  60. data/lib/gitlab/styles/rubocop/cop/rspec/verbose_include_metadata.rb +0 -75
  61. data/lib/gitlab/styles/rubocop/cop/style/hash_transformation.rb +0 -87
  62. data/lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb +0 -43
  63. data/lib/gitlab/styles/rubocop/cop/without_reactive_cache.rb +0 -20
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop-rspec'
4
+ require_relative 'base'
5
+
6
+ module Rubocop
7
+ module Cop
8
+ module RSpec
9
+ # This cop checks for single-line hook blocks
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # before { do_something }
15
+ # after(:each) { undo_something }
16
+ #
17
+ # # good
18
+ # before do
19
+ # do_something
20
+ # end
21
+ #
22
+ # after(:each) do
23
+ # undo_something
24
+ # end
25
+ class SingleLineHook < Base
26
+ MESSAGE = "Don't use single-line hook blocks."
27
+
28
+ def_node_search :rspec_hook?, <<~PATTERN
29
+ (send nil? {:after :around :before} ...)
30
+ PATTERN
31
+
32
+ def on_block(node)
33
+ return unless node.single_line?
34
+ return unless rspec_hook?(node)
35
+
36
+ add_offense(node, message: MESSAGE)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop-rspec'
4
+ require_relative 'base'
5
+
6
+ module Rubocop
7
+ module Cop
8
+ module RSpec
9
+ # Checks for verbose include metadata used in the specs.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # describe MyClass, js: true do
14
+ # end
15
+ #
16
+ # # good
17
+ # describe MyClass, :js do
18
+ # end
19
+ class VerboseIncludeMetadata < Base
20
+ extend RuboCop::Cop::AutoCorrector
21
+
22
+ MSG = 'Use `%s` instead of `%s`.'
23
+
24
+ SELECTORS = %i[describe context feature example_group it specify example scenario its].freeze
25
+
26
+ def_node_matcher :include_metadata, <<-PATTERN
27
+ (send {(const nil? :RSpec) nil?} {#{SELECTORS.map(&:inspect).join(' ')}}
28
+ !const
29
+ ...
30
+ (hash $...))
31
+ PATTERN
32
+
33
+ def_node_matcher :invalid_metadata?, <<-PATTERN
34
+ (pair
35
+ (sym $...)
36
+ (true))
37
+ PATTERN
38
+
39
+ def on_send(node)
40
+ invalid_metadata_matches(node) do |match|
41
+ add_offense(node, message: format(MSG, good(match), bad(match))) do |corrector|
42
+ invalid_metadata_matches(node) do |match|
43
+ corrector.replace(match.loc.expression, good(match))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def invalid_metadata_matches(node, &block)
52
+ include_metadata(node) do |matches|
53
+ matches.select { |match| invalid_metadata?(match) }.each(&block)
54
+ end
55
+ end
56
+
57
+ def bad(match)
58
+ "#{metadata_key(match)}: true"
59
+ end
60
+
61
+ def good(match)
62
+ ":#{metadata_key(match)}"
63
+ end
64
+
65
+ def metadata_key(match)
66
+ match.children[0].source
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cop identifies places where `map { ... }.to_h` or
7
+ # `Hash[map { ... }]` can be replaced with `to_h { ... }`,
8
+ # saving an intermediate array allocation.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # hash.map { |k, v| [v.upcase, k.downcase] }.to_h
13
+ # hash.collect { |k, v| [v.upcase, k.downcase] }.to_h
14
+ # Hash[hash.map { |k, v| [v.upcase, k.downcase] }]
15
+ # Hash[hash.collect { |k, v| [v.upcase, k.downcase] }]
16
+ # array.map { |x| [x, x + 1] }.to_h
17
+ #
18
+ # # good
19
+ # hash.to_h { |k, v| [v.upcase, k.downcase] }
20
+ # array.to_h { |x| [x, x + 1] }
21
+ #
22
+ # Full credit: https://github.com/eugeneius/rubocop-performance/blob/hash_transformation/lib/rubocop/cop/performance/hash_transformation.rb
23
+ class HashTransformation < RuboCop::Cop::Cop
24
+ include RuboCop::Cop::RangeHelp
25
+
26
+ MSG = 'Use `to_h { ... }` instead of `%<current>s`.'
27
+
28
+ def_node_matcher :to_h_candidate?, <<~PATTERN
29
+ {
30
+ [(send
31
+ $(block $(send _ {:map :collect}) ...) :to_h) !block_literal?]
32
+ (send (const nil? :Hash) :[]
33
+ $(block $(send _ {:map :collect}) ...))
34
+ }
35
+ PATTERN
36
+
37
+ def on_send(node)
38
+ to_h_candidate?(node) do |_block, call|
39
+ range = offense_range(node, call)
40
+ message = message(node, call)
41
+ add_offense(node, location: range, message: message)
42
+ end
43
+ end
44
+
45
+ def autocorrect(node)
46
+ block, call = to_h_candidate?(node)
47
+
48
+ lambda do |corrector|
49
+ corrector.remove(after_block(node, block))
50
+ corrector.replace(call.loc.selector, 'to_h')
51
+ corrector.remove(before_block(node, block))
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def offense_range(node, call)
58
+ return node.source_range if node.children.first.const_type?
59
+
60
+ range_between(call.loc.selector.begin_pos, node.loc.selector.end_pos)
61
+ end
62
+
63
+ def message(node, call)
64
+ current = if node.children.first.const_type?
65
+ "Hash[#{call.method_name} { ... }]"
66
+ else
67
+ "#{call.method_name} { ... }.to_h"
68
+ end
69
+
70
+ format(MSG, current: current)
71
+ end
72
+
73
+ def after_block(node, block)
74
+ block.source_range.end.join(node.source_range.end)
75
+ end
76
+
77
+ def before_block(node, block)
78
+ node.source_range.begin.join(block.source_range.begin)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cop flags uses of OpenStruct, as it is now officially discouraged
7
+ # to be used for performance, version compatibility, and potential security issues.
8
+ #
9
+ # See also:
10
+ # - https://rubyreferences.github.io/rubychanges/3.0.html#standard-library
11
+ # - https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats
12
+ # - https://gitlab.com/gitlab-org/gitlab/-/merge_requests/67855
13
+ class OpenStructUse < RuboCop::Cop::Cop
14
+ MSG = 'Avoid using `OpenStruct`. It is officially discouraged. ' \
15
+ 'Replace it with `Struct`, `Hash`, or RSpec doubles. ' \
16
+ 'See https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats'
17
+
18
+ def_node_matcher :uses_open_struct?, <<-PATTERN
19
+ (const {nil? (cbase)} :OpenStruct)
20
+ PATTERN
21
+
22
+ def on_const(node)
23
+ return unless uses_open_struct?(node)
24
+ return if custom_class_or_module_definition?(node)
25
+
26
+ add_offense(node)
27
+ end
28
+
29
+ private
30
+
31
+ def custom_class_or_module_definition?(node)
32
+ parent = node.parent
33
+
34
+ (parent.class_type? || parent.module_type?) && node.left_siblings.empty?
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubocop
4
+ module Cop
5
+ # Cop that prevents the use of `without_reactive_cache`
6
+ class WithoutReactiveCache < RuboCop::Cop::Cop
7
+ MSG = 'without_reactive_cache is for debugging purposes only. Please use with_reactive_cache.'
8
+
9
+ def on_send(node)
10
+ return unless node.children[1] == :without_reactive_cache
11
+
12
+ add_offense(node, location: :selector)
13
+ end
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-styles
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.0
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-07 00:00:00.000000000 Z
11
+ date: 2022-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -184,39 +184,39 @@ files:
184
184
  - lib/gitlab/styles.rb
185
185
  - lib/gitlab/styles/common/banned_constants.rb
186
186
  - lib/gitlab/styles/rubocop.rb
187
- - lib/gitlab/styles/rubocop/cop/active_record_dependent.rb
188
- - lib/gitlab/styles/rubocop/cop/active_record_serialize.rb
189
- - lib/gitlab/styles/rubocop/cop/avoid_return_from_blocks.rb
190
- - lib/gitlab/styles/rubocop/cop/code_reuse/active_record.rb
191
- - lib/gitlab/styles/rubocop/cop/custom_error_class.rb
192
- - lib/gitlab/styles/rubocop/cop/fips/md5.rb
193
- - lib/gitlab/styles/rubocop/cop/fips/open_ssl.rb
194
- - lib/gitlab/styles/rubocop/cop/fips/sha1.rb
195
- - lib/gitlab/styles/rubocop/cop/gem_fetcher.rb
196
- - lib/gitlab/styles/rubocop/cop/in_batches.rb
197
- - lib/gitlab/styles/rubocop/cop/internal_affairs/deprecate_cop_helper.rb
198
- - lib/gitlab/styles/rubocop/cop/line_break_after_guard_clauses.rb
199
- - lib/gitlab/styles/rubocop/cop/line_break_around_conditional_block.rb
200
- - lib/gitlab/styles/rubocop/cop/migration/update_large_table.rb
201
- - lib/gitlab/styles/rubocop/cop/performance/rubyzip.rb
202
- - lib/gitlab/styles/rubocop/cop/polymorphic_associations.rb
203
- - lib/gitlab/styles/rubocop/cop/rails/include_url_helper.rb
204
- - lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
205
- - lib/gitlab/styles/rubocop/cop/rspec/base.rb
206
- - lib/gitlab/styles/rubocop/cop/rspec/empty_line_after_final_let_it_be.rb
207
- - lib/gitlab/styles/rubocop/cop/rspec/empty_line_after_let_block.rb
208
- - lib/gitlab/styles/rubocop/cop/rspec/empty_line_after_shared_example.rb
209
- - lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb
210
- - lib/gitlab/styles/rubocop/cop/rspec/have_link_parameters.rb
211
- - lib/gitlab/styles/rubocop/cop/rspec/single_line_hook.rb
212
- - lib/gitlab/styles/rubocop/cop/rspec/verbose_include_metadata.rb
213
- - lib/gitlab/styles/rubocop/cop/style/hash_transformation.rb
214
- - lib/gitlab/styles/rubocop/cop/style/open_struct_use.rb
215
- - lib/gitlab/styles/rubocop/cop/without_reactive_cache.rb
216
187
  - lib/gitlab/styles/rubocop/migration_helpers.rb
217
188
  - lib/gitlab/styles/rubocop/model_helpers.rb
218
189
  - lib/gitlab/styles/rubocop/rspec/helpers.rb
219
190
  - lib/gitlab/styles/version.rb
191
+ - lib/rubocop/cop/active_record_dependent.rb
192
+ - lib/rubocop/cop/active_record_serialize.rb
193
+ - lib/rubocop/cop/avoid_return_from_blocks.rb
194
+ - lib/rubocop/cop/code_reuse/active_record.rb
195
+ - lib/rubocop/cop/custom_error_class.rb
196
+ - lib/rubocop/cop/fips/md5.rb
197
+ - lib/rubocop/cop/fips/open_ssl.rb
198
+ - lib/rubocop/cop/fips/sha1.rb
199
+ - lib/rubocop/cop/gem_fetcher.rb
200
+ - lib/rubocop/cop/in_batches.rb
201
+ - lib/rubocop/cop/internal_affairs/deprecate_cop_helper.rb
202
+ - lib/rubocop/cop/line_break_after_guard_clauses.rb
203
+ - lib/rubocop/cop/line_break_around_conditional_block.rb
204
+ - lib/rubocop/cop/migration/update_large_table.rb
205
+ - lib/rubocop/cop/performance/rubyzip.rb
206
+ - lib/rubocop/cop/polymorphic_associations.rb
207
+ - lib/rubocop/cop/rails/include_url_helper.rb
208
+ - lib/rubocop/cop/redirect_with_status.rb
209
+ - lib/rubocop/cop/rspec/base.rb
210
+ - lib/rubocop/cop/rspec/empty_line_after_final_let_it_be.rb
211
+ - lib/rubocop/cop/rspec/empty_line_after_let_block.rb
212
+ - lib/rubocop/cop/rspec/empty_line_after_shared_example.rb
213
+ - lib/rubocop/cop/rspec/example_starting_character.rb
214
+ - lib/rubocop/cop/rspec/have_link_parameters.rb
215
+ - lib/rubocop/cop/rspec/single_line_hook.rb
216
+ - lib/rubocop/cop/rspec/verbose_include_metadata.rb
217
+ - lib/rubocop/cop/style/hash_transformation.rb
218
+ - lib/rubocop/cop/style/open_struct_use.rb
219
+ - lib/rubocop/cop/without_reactive_cache.rb
220
220
  - rubocop-all.yml
221
221
  - rubocop-bundler.yml
222
222
  - rubocop-code_reuse.yml
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../model_helpers'
4
-
5
- module Gitlab
6
- module Styles
7
- module Rubocop
8
- module Cop
9
- # Cop that prevents the use of `dependent: ...` in ActiveRecord models.
10
- class ActiveRecordDependent < RuboCop::Cop::Cop
11
- include ModelHelpers
12
-
13
- MSG = 'Do not use `dependent:` to remove associated data, ' \
14
- 'use foreign keys with cascading deletes instead.'
15
-
16
- METHOD_NAMES = [:has_many, :has_one, :belongs_to].freeze
17
- ALLOWED_OPTIONS = [:restrict_with_error].freeze
18
-
19
- def on_send(node)
20
- return unless in_model?(node)
21
- return unless METHOD_NAMES.include?(node.children[1])
22
-
23
- node.children.last.each_node(:pair) do |pair|
24
- key_name = pair.children[0].children[0]
25
- option_name = pair.children[1].children[0]
26
-
27
- break if ALLOWED_OPTIONS.include?(option_name)
28
-
29
- add_offense(pair) if key_name == :dependent
30
- end
31
- end
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../model_helpers'
4
-
5
- module Gitlab
6
- module Styles
7
- module Rubocop
8
- module Cop
9
- # Cop that prevents the use of `serialize` in ActiveRecord models.
10
- class ActiveRecordSerialize < RuboCop::Cop::Cop
11
- include ModelHelpers
12
-
13
- MSG = 'Do not store serialized data in the database, use separate columns and/or tables instead'
14
-
15
- def on_send(node)
16
- return unless in_model?(node)
17
-
18
- add_offense(node, location: :selector) if node.children[1] == :serialize
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Gitlab
4
- module Styles
5
- module Rubocop
6
- module Cop
7
- # Checks for return inside blocks.
8
- # For more information see: https://gitlab.com/gitlab-org/gitlab-foss/issues/42889
9
- #
10
- # @example
11
- # # bad
12
- # call do
13
- # return if something
14
- #
15
- # do_something_else
16
- # end
17
- #
18
- # # good
19
- # call do
20
- # break if something
21
- #
22
- # do_something_else
23
- # end
24
- #
25
- class AvoidReturnFromBlocks < RuboCop::Cop::Cop
26
- MSG = 'Do not return from a block, use next or break instead.'
27
- DEF_METHODS = %i[define_method lambda].freeze
28
- WHITELISTED_METHODS = %i[each each_filename times loop].freeze
29
-
30
- def on_block(node)
31
- block_body = node.body
32
-
33
- return unless block_body
34
- return unless top_block?(node)
35
-
36
- block_body.each_node(:return) do |return_node|
37
- next if parent_blocks(node, return_node).all? { |block| whitelisted?(block) }
38
-
39
- add_offense(return_node)
40
- end
41
- end
42
-
43
- private
44
-
45
- def top_block?(node)
46
- current_node = node
47
- top_block = nil
48
-
49
- while current_node && current_node.type != :def
50
- top_block = current_node if current_node.block_type?
51
- current_node = current_node.parent
52
- end
53
-
54
- top_block == node
55
- end
56
-
57
- def parent_blocks(node, current_node)
58
- blocks = []
59
-
60
- until node == current_node || def?(current_node)
61
- blocks << current_node if current_node.block_type?
62
- current_node = current_node.parent
63
- end
64
-
65
- blocks << node if node == current_node && !def?(node)
66
- blocks
67
- end
68
-
69
- def def?(node)
70
- node.def_type? || node.defs_type? ||
71
- (node.block_type? && DEF_METHODS.include?(node.method_name))
72
- end
73
-
74
- def whitelisted?(block_node)
75
- WHITELISTED_METHODS.include?(block_node.method_name)
76
- end
77
- end
78
- end
79
- end
80
- end
81
- end
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Gitlab
4
- module Styles
5
- module Rubocop
6
- module Cop
7
- module CodeReuse
8
- # Cop that denies the use of ActiveRecord methods outside of models.
9
- class ActiveRecord < RuboCop::Cop::Cop
10
- MSG = 'This method can only be used inside an ActiveRecord model: ' \
11
- 'https://gitlab.com/gitlab-org/gitlab-foss/issues/49653'
12
-
13
- # Various methods from ActiveRecord::Querying that are denied. We
14
- # exclude some generic ones such as `any?` and `first`, as these may
15
- # lead to too many false positives, since `Array` also supports these
16
- # methods.
17
- #
18
- # The keys of this Hash are the denied method names. The values are
19
- # booleans that indicate if the method should only be denied if any
20
- # arguments are provided.
21
- NOT_ALLOWED = {
22
- average: true,
23
- calculate: true,
24
- count_by_sql: true,
25
- create_with: true,
26
- distinct: false,
27
- eager_load: true,
28
- exists?: true,
29
- find_by: true,
30
- find_by!: true,
31
- find_by_sql: true,
32
- find_each: true,
33
- find_in_batches: true,
34
- find_or_create_by: true,
35
- find_or_create_by!: true,
36
- find_or_initialize_by: true,
37
- first!: false,
38
- first_or_create: true,
39
- first_or_create!: true,
40
- first_or_initialize: true,
41
- from: true,
42
- group: true,
43
- having: true,
44
- ids: false,
45
- includes: true,
46
- joins: true,
47
- lock: false,
48
- many?: false,
49
- offset: true,
50
- order: true,
51
- pluck: true,
52
- preload: true,
53
- readonly: false,
54
- references: true,
55
- reorder: true,
56
- rewhere: true,
57
- take: false,
58
- take!: false,
59
- unscope: false,
60
- where: false,
61
- with: true
62
- }.freeze
63
-
64
- def on_send(node)
65
- receiver = node.children[0]
66
- send_name = node.children[1]
67
- first_arg = node.children[2]
68
-
69
- return unless receiver && NOT_ALLOWED.key?(send_name)
70
-
71
- # If the rule requires an argument to be given, but none are
72
- # provided, we won't register an offense. This prevents us from
73
- # adding offenses for `project.group`, while still covering
74
- # `Project.group(:name)`.
75
- return if NOT_ALLOWED[send_name] && !first_arg
76
-
77
- add_offense(node, location: :selector)
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Gitlab
4
- module Styles
5
- module Rubocop
6
- module Cop
7
- # This cop makes sure that custom error classes, when empty, are declared
8
- # with Class.new.
9
- #
10
- # @example
11
- # # bad
12
- # class FooError < StandardError
13
- # end
14
- #
15
- # # okish
16
- # class FooError < StandardError; end
17
- #
18
- # # good
19
- # FooError = Class.new(StandardError)
20
- class CustomErrorClass < RuboCop::Cop::Cop
21
- MSG = 'Use `Class.new(SuperClass)` to define an empty custom error class.'
22
-
23
- def on_class(node)
24
- parent = node.parent_class
25
- body = node.body
26
-
27
- return if body
28
-
29
- parent_klass = class_name_from_node(parent)
30
-
31
- return unless parent_klass&.to_s&.end_with?('Error')
32
-
33
- add_offense(node)
34
- end
35
-
36
- def autocorrect(node)
37
- klass = node.identifier
38
- parent = node.parent_class
39
-
40
- replacement = "#{class_name_from_node(klass)} = Class.new(#{class_name_from_node(parent)})"
41
-
42
- lambda do |corrector|
43
- corrector.replace(node.source_range, replacement)
44
- end
45
- end
46
-
47
- private
48
-
49
- # The nested constant `Foo::Bar::Baz` looks like:
50
- #
51
- # s(:const,
52
- # s(:const,
53
- # s(:const, nil, :Foo), :Bar), :Baz)
54
- #
55
- # So recurse through that to get the name as written in the source.
56
- #
57
- def class_name_from_node(node, suffix = nil)
58
- return unless node&.type == :const
59
-
60
- name = node.children[1].to_s
61
- name = "#{name}::#{suffix}" if suffix
62
-
63
- if node.children[0]
64
- class_name_from_node(node.children[0], name)
65
- else
66
- name
67
- end
68
- end
69
- end
70
- end
71
- end
72
- end
73
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../../common/banned_constants'
4
-
5
- module Gitlab
6
- module Styles
7
- module Rubocop
8
- module Cop
9
- module Fips
10
- class MD5 < RuboCop::Cop::Base
11
- include Gitlab::Styles::Common::BannedConstants
12
-
13
- MESSAGE_TEMPLATE = 'MD5 is not FIPS-compliant. Use %{replacement} instead.'
14
-
15
- REPLACEMENTS = {
16
- 'OpenSSL::Digest::MD5' => 'OpenSSL::Digest::SHA256',
17
- 'Digest::MD5' => 'OpenSSL::Digest::SHA256'
18
- }.freeze
19
-
20
- def initialize(config = nil, options = nil)
21
- @message_template = MESSAGE_TEMPLATE
22
- @replacements = REPLACEMENTS
23
- @autocorrect = false
24
- super(config, options)
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end