gitlab-styles 13.1.0 → 15.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.
- checksums.yaml +4 -4
- data/.gitlab/merge_request_templates/New Static Analysis Check.md +26 -0
- data/.gitlab-ci.yml +3 -4
- data/.rubocop.yml +2 -23
- data/.rubocop_todo.yml +2 -19
- data/Gemfile.lock +248 -133
- data/README.md +34 -10
- data/gitlab-styles.gemspec +21 -22
- data/lib/gitlab/styles/rubocop/rspec.rb +4 -0
- data/lib/gitlab/styles/rubocop.rb +6 -3
- data/lib/gitlab/styles/version.rb +1 -1
- data/lib/rubocop/cop/avoid_return_from_blocks.rb +4 -4
- data/lib/rubocop/cop/fips/md5.rb +10 -0
- data/lib/rubocop/cop/fips/open_ssl.rb +11 -0
- data/lib/rubocop/cop/fips/sha1.rb +8 -0
- data/lib/rubocop/cop/gem_fetcher.rb +6 -0
- data/lib/rubocop/cop/gitlab_security/deep_munge.rb +10 -0
- data/lib/rubocop/cop/in_batches.rb +8 -0
- data/lib/rubocop/cop/internal_affairs/cop_description_with_example.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/deprecate_cop_helper.rb +12 -0
- data/lib/rubocop/cop/internal_affairs/use_restrict_on_send.rb +17 -16
- data/lib/rubocop/cop/line_break_around_conditional_block.rb +31 -31
- data/lib/rubocop/cop/migration/update_large_table.rb +24 -1
- data/lib/rubocop/cop/performance/rubyzip.rb +6 -0
- data/lib/rubocop/cop/polymorphic_associations.rb +11 -0
- data/lib/rubocop/cop/rspec/base.rb +1 -0
- data/lib/rubocop/cop/rspec/empty_line_after_let_block.rb +1 -0
- data/lib/rubocop/cop/rspec/empty_line_after_shared_example.rb +3 -2
- data/lib/rubocop/cop/rspec/example_starting_character.rb +3 -4
- data/lib/rubocop/cop/rspec/feature_spec_max_examples.rb +46 -0
- data/lib/rubocop/cop/rspec/single_line_hook.rb +1 -0
- data/lib/rubocop/cop/tailwind/max_width_media_queries.rb +2 -1
- data/rubocop-all.yml +1 -1
- data/rubocop-capybara.yml +1 -2
- data/rubocop-code_reuse.yml +1 -1
- data/rubocop-default.yml +0 -3
- data/rubocop-factory_bot.yml +1 -2
- data/rubocop-fips.yml +3 -1
- data/rubocop-graphql.yml +1 -1
- data/rubocop-internal-affairs.yml +4 -1
- data/rubocop-layout.yml +6 -1
- data/rubocop-migrations.yml +1 -1
- data/rubocop-naming.yml +1 -1
- data/rubocop-performance.yml +3 -8
- data/rubocop-rails.yml +8 -2
- data/rubocop-rspec.yml +25 -2
- data/rubocop-security.yml +7 -1
- data/rubocop-style.yml +8 -1
- data/rubocop-tailwind.yml +2 -1
- metadata +58 -72
- data/lib/rubocop/cop/line_break_after_guard_clauses.rb +0 -99
- data/lib/rubocop/cop/style/open_struct_use.rb +0 -49
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Gitlab::Styles
|
|
2
2
|
|
|
3
|
-
`Gitlab::Styles` centralizes
|
|
3
|
+
`Gitlab::Styles` centralizes shared GitLab style configurations (only RuboCop
|
|
4
4
|
for now), as well as custom RuboCop cops.
|
|
5
5
|
|
|
6
6
|
## Installation
|
|
@@ -19,6 +19,19 @@ Or install it yourself as:
|
|
|
19
19
|
|
|
20
20
|
$ gem install gitlab-styles
|
|
21
21
|
|
|
22
|
+
## Why We Pin Gem Versions
|
|
23
|
+
|
|
24
|
+
`gitlab-styles` pins specific versions of RuboCop and related gems (e.g., `rubocop-rails`, `rubocop-rspec`, `rubocop-performance`) to ensure **consistent rule configuration and behavior across all GitLab projects**.
|
|
25
|
+
|
|
26
|
+
When gems are left unpinned, minor or patch version updates can introduce:
|
|
27
|
+
|
|
28
|
+
- **New or modified RuboCop rules** that may conflict with your project's configuration
|
|
29
|
+
- **Inconsistent linting results** across different environments and CI/CD pipelines
|
|
30
|
+
- **Unexpected build failures** when developers or CI systems install different versions
|
|
31
|
+
- **Configuration drift** between projects using `gitlab-styles`
|
|
32
|
+
|
|
33
|
+
By pinning versions, we guarantee that all projects using `gitlab-styles` enforce the same coding standards and rules, regardless of when they install the gem.
|
|
34
|
+
|
|
22
35
|
## Usage
|
|
23
36
|
|
|
24
37
|
### Inherit all rules
|
|
@@ -39,11 +52,19 @@ rules:
|
|
|
39
52
|
|
|
40
53
|
- `rubocop-all.yml`
|
|
41
54
|
- `rubocop-bundler.yml`
|
|
55
|
+
- `rubocop-capybara.yml`
|
|
56
|
+
- `rubocop-code_reuse.yml`
|
|
57
|
+
- `rubocop-factory_bot.yml`
|
|
58
|
+
- `rubocop-fips.yml`
|
|
42
59
|
- `rubocop-gemspec.yml`
|
|
60
|
+
- `rubocop-graphql.yml`
|
|
61
|
+
- `rubocop-internal-affairs.yml`
|
|
43
62
|
- `rubocop-layout.yml`
|
|
44
63
|
- `rubocop-lint.yml`
|
|
45
|
-
- `rubocop-migrations.yml`
|
|
46
64
|
- `rubocop-metrics.yml`
|
|
65
|
+
- `rubocop-migrations.yml`
|
|
66
|
+
- `rubocop-minimal.yml`: for plain Ruby projects, the rules that don't depend on
|
|
67
|
+
Rails or GitLab code
|
|
47
68
|
- `rubocop-naming.yml`
|
|
48
69
|
- `rubocop-performance.yml`
|
|
49
70
|
- `rubocop-rails.yml`
|
|
@@ -61,20 +82,23 @@ inherit_gem:
|
|
|
61
82
|
- rubocop-naming.yml
|
|
62
83
|
```
|
|
63
84
|
|
|
85
|
+
Each file loads only the custom cops it configures. For example,
|
|
86
|
+
`CodeReuse/ActiveRecord` is only available if you inherit
|
|
87
|
+
`rubocop-code_reuse.yml`, directly or through `rubocop-default.yml`:
|
|
88
|
+
|
|
89
|
+
- If RuboCop reports `unrecognized cop or department` for one of this gem's
|
|
90
|
+
cops, either inherit the file that configures it, or remove that cop from
|
|
91
|
+
your configuration.
|
|
92
|
+
- If `Lint/RedundantCopDisableDirective` reports a `rubocop:disable` comment for
|
|
93
|
+
one of this gem's cops as unnecessary, the cop isn't loaded: remove the
|
|
94
|
+
comment, or inherit the file that configures the cop.
|
|
95
|
+
|
|
64
96
|
## Development
|
|
65
97
|
|
|
66
98
|
After checking out the repo, run `bin/setup` to install dependencies.
|
|
67
99
|
Then, run `rake spec` to run the tests. You can also run `bin/console` for an
|
|
68
100
|
interactive prompt that will allow you to experiment.
|
|
69
101
|
|
|
70
|
-
To test some rules locally, there is a test application [in the playground folder](./playground/). It is a vanilla Rails 6 application with your local `gitlab-styles` included, and it is used to test RuboCop rules. You can add code in it (preferably RuboCop offenses) and run the following command to test the RuboCop policy we setup:
|
|
71
|
-
|
|
72
|
-
```shell
|
|
73
|
-
cd playground
|
|
74
|
-
bundle install
|
|
75
|
-
bundle exec rubocop -c .rubocop.yml
|
|
76
|
-
```
|
|
77
|
-
|
|
78
102
|
### Activate lefthook locally
|
|
79
103
|
|
|
80
104
|
```shell
|
data/gitlab-styles.gemspec
CHANGED
|
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
|
5
5
|
require 'gitlab/styles/version'
|
|
6
6
|
|
|
7
7
|
Gem::Specification.new do |spec|
|
|
8
|
-
spec.required_ruby_version = '>= 3.
|
|
8
|
+
spec.required_ruby_version = '>= 3.3'
|
|
9
9
|
spec.name = 'gitlab-styles'
|
|
10
10
|
spec.version = Gitlab::Styles::VERSION
|
|
11
11
|
spec.authors = ['GitLab']
|
|
@@ -16,31 +16,30 @@ Gem::Specification.new do |spec|
|
|
|
16
16
|
spec.license = 'MIT'
|
|
17
17
|
|
|
18
18
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
|
-
f.match(%r{^(docs|test|spec|features
|
|
19
|
+
f.match(%r{^(docs|test|spec|features)/})
|
|
20
20
|
end
|
|
21
21
|
spec.bindir = 'exe'
|
|
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', '1.
|
|
26
|
-
spec.add_dependency 'rubocop-capybara', '
|
|
27
|
-
spec.add_dependency 'rubocop-factory_bot', '
|
|
28
|
-
spec.add_dependency 'rubocop-graphql', '
|
|
29
|
-
spec.add_dependency 'rubocop-performance', '
|
|
30
|
-
spec.add_dependency 'rubocop-rails', '
|
|
31
|
-
spec.add_dependency 'rubocop-rspec', '
|
|
32
|
-
spec.add_dependency 'rubocop-rspec_rails', '
|
|
25
|
+
spec.add_dependency 'rubocop', '1.92.1'
|
|
26
|
+
spec.add_dependency 'rubocop-capybara', '3.1.0'
|
|
27
|
+
spec.add_dependency 'rubocop-factory_bot', '2.28.0'
|
|
28
|
+
spec.add_dependency 'rubocop-graphql', '1.8.0'
|
|
29
|
+
spec.add_dependency 'rubocop-performance', '1.27.0'
|
|
30
|
+
spec.add_dependency 'rubocop-rails', '2.38.0'
|
|
31
|
+
spec.add_dependency 'rubocop-rspec', '3.10.2'
|
|
32
|
+
spec.add_dependency 'rubocop-rspec_rails', '2.33.0'
|
|
33
33
|
|
|
34
|
-
spec.add_development_dependency 'bundler', '~>
|
|
35
|
-
spec.add_development_dependency 'gitlab-dangerfiles', '~> 4.
|
|
36
|
-
spec.add_development_dependency 'lefthook', '~>
|
|
37
|
-
spec.add_development_dependency 'pry-byebug', '~> 3.
|
|
38
|
-
spec.add_development_dependency 'rake', '~> 13.
|
|
39
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
|
40
|
-
spec.add_development_dependency 'rspec-parameterized-table_syntax', '~> 1
|
|
41
|
-
spec.add_development_dependency 'rubocop-rake', '~> 0.
|
|
42
|
-
spec.add_development_dependency 'simplecov', '~>
|
|
43
|
-
spec.add_development_dependency 'simplecov-cobertura', '~>
|
|
44
|
-
spec.add_development_dependency '
|
|
45
|
-
spec.add_development_dependency 'test_file_finder', '~> 0.1.4'
|
|
34
|
+
spec.add_development_dependency 'bundler', '~> 4.0'
|
|
35
|
+
spec.add_development_dependency 'gitlab-dangerfiles', '~> 4.14.0'
|
|
36
|
+
spec.add_development_dependency 'lefthook', '~> 2.2'
|
|
37
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.12'
|
|
38
|
+
spec.add_development_dependency 'rake', '~> 13.4'
|
|
39
|
+
spec.add_development_dependency 'rspec', '~> 3.13'
|
|
40
|
+
spec.add_development_dependency 'rspec-parameterized-table_syntax', '~> 2.1'
|
|
41
|
+
spec.add_development_dependency 'rubocop-rake', '~> 0.7'
|
|
42
|
+
spec.add_development_dependency 'simplecov', '~> 1.3'
|
|
43
|
+
spec.add_development_dependency 'simplecov-cobertura', '~> 4.0'
|
|
44
|
+
spec.add_development_dependency 'test_file_finder', '~> 0.3'
|
|
46
45
|
end
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Auto-require all cops under `rubocop/cop/**/*.rb
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
# Auto-require all cops under `rubocop/cop/**/*.rb`, except the RSpec cops, which need rubocop-rspec.
|
|
4
|
+
# The rubocop-*.yml configs don't use this: each requires only the cops it configures, and
|
|
5
|
+
# `rubocop-rspec.yml` loads the RSpec cops through `gitlab/styles/rubocop/rspec`.
|
|
6
|
+
cops_dir = File.expand_path('../../rubocop/cop', __dir__)
|
|
7
|
+
rspec_cops_dir = File.join(cops_dir, 'rspec')
|
|
8
|
+
Dir[File.join(cops_dir, '**', '*.rb')].each { |cop| require(cop) unless cop.start_with?("#{rspec_cops_dir}/") }
|
|
6
9
|
|
|
7
10
|
module Gitlab
|
|
8
11
|
module Styles
|
|
@@ -39,6 +39,7 @@ module Rubocop
|
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
alias_method :on_numblock, :on_block
|
|
42
|
+
alias_method :on_itblock, :on_block
|
|
42
43
|
|
|
43
44
|
private
|
|
44
45
|
|
|
@@ -47,7 +48,7 @@ module Rubocop
|
|
|
47
48
|
top_block = nil
|
|
48
49
|
|
|
49
50
|
while current_node && current_node.type != :def
|
|
50
|
-
top_block = current_node if current_node.
|
|
51
|
+
top_block = current_node if current_node.any_block_type?
|
|
51
52
|
current_node = current_node.parent
|
|
52
53
|
end
|
|
53
54
|
|
|
@@ -58,7 +59,7 @@ module Rubocop
|
|
|
58
59
|
blocks = []
|
|
59
60
|
|
|
60
61
|
until node == current_node || def?(current_node)
|
|
61
|
-
blocks << current_node if current_node.
|
|
62
|
+
blocks << current_node if current_node.any_block_type?
|
|
62
63
|
current_node = current_node.parent
|
|
63
64
|
end
|
|
64
65
|
|
|
@@ -67,8 +68,7 @@ module Rubocop
|
|
|
67
68
|
end
|
|
68
69
|
|
|
69
70
|
def def?(node)
|
|
70
|
-
node.
|
|
71
|
-
(node.block_type? && DEF_METHODS.include?(node.method_name))
|
|
71
|
+
node.any_def_type? || (node.any_block_type? && DEF_METHODS.include?(node.method_name))
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def allowed?(block_node)
|
data/lib/rubocop/cop/fips/md5.rb
CHANGED
|
@@ -5,6 +5,16 @@ require_relative '../../../gitlab/styles/common/banned_constants'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
module Fips
|
|
8
|
+
# Checks for the usage of MD5, which is not FIPS-compliant.
|
|
9
|
+
# It suggests using a FIPS-compliant alternative like SHA256.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# # bad
|
|
13
|
+
# OpenSSL::Digest::MD5.hexdigest('foo')
|
|
14
|
+
# Digest::MD5.hexdigest('foo')
|
|
15
|
+
#
|
|
16
|
+
# # good
|
|
17
|
+
# OpenSSL::Digest::SHA256.hexdigest('foo')
|
|
8
18
|
class MD5 < RuboCop::Cop::Base
|
|
9
19
|
include Gitlab::Styles::Common::BannedConstants
|
|
10
20
|
|
|
@@ -5,6 +5,17 @@ require_relative '../../../gitlab/styles/common/banned_constants'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
module Fips
|
|
8
|
+
# Flags usage of the Digest class (which is not FIPS-compliant) and suggests replacing it with OpenSSL::Digest
|
|
9
|
+
# (which is FIPS-compliant).
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# # bad
|
|
13
|
+
# Digest::SHA1.hexdigest('foo')
|
|
14
|
+
# Digest::SHA512('foo')
|
|
15
|
+
#
|
|
16
|
+
# # good
|
|
17
|
+
# OpenSSL::Digest::SHA1.hexdigest('foo')
|
|
18
|
+
# OpenSSL::Digest::SHA512.hexdigest('foo')
|
|
8
19
|
class OpenSSL < RuboCop::Cop::Base
|
|
9
20
|
extend RuboCop::Cop::AutoCorrector
|
|
10
21
|
include Gitlab::Styles::Common::BannedConstants
|
|
@@ -5,6 +5,14 @@ require_relative '../../../gitlab/styles/common/banned_constants'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
module Fips
|
|
8
|
+
# Cop that denies the use of Digest::SHA1
|
|
9
|
+
# @example
|
|
10
|
+
# # bad
|
|
11
|
+
# OpenSSL::Digest::SHA1.hexdigest('foo')
|
|
12
|
+
# Digest::SHA1('ABC')
|
|
13
|
+
#
|
|
14
|
+
# # good
|
|
15
|
+
# OpenSSL::Digest::SHA256.hexdigest('foo')
|
|
8
16
|
class SHA1 < RuboCop::Cop::Base
|
|
9
17
|
include Gitlab::Styles::Common::BannedConstants
|
|
10
18
|
|
|
@@ -5,6 +5,12 @@ module Rubocop
|
|
|
5
5
|
# Prevents usage of the `git` and `github` arguments to `gem` in a
|
|
6
6
|
# `Gemfile` in order to avoid additional points of failure beyond
|
|
7
7
|
# rubygems.org.
|
|
8
|
+
# @example
|
|
9
|
+
# # bad
|
|
10
|
+
# gem 'rack', git: 'https://github.com/rack/rack'
|
|
11
|
+
#
|
|
12
|
+
# # good
|
|
13
|
+
# gem "rack"
|
|
8
14
|
class GemFetcher < RuboCop::Cop::Base
|
|
9
15
|
MSG = 'Do not use gems from git repositories, only use gems from RubyGems or vendored gems. ' \
|
|
10
16
|
'See https://docs.gitlab.com/ee/development/gemfile.html#no-gems-fetched-from-git-repositories'
|
|
@@ -13,7 +13,17 @@ module RuboCop
|
|
|
13
13
|
# # bad
|
|
14
14
|
# config.action_dispatch.perform_deep_munge = false
|
|
15
15
|
#
|
|
16
|
+
# # bad
|
|
17
|
+
# config.action_dispatch.perform_deep_munge = !true
|
|
18
|
+
#
|
|
19
|
+
# # good
|
|
20
|
+
# config.action_dispatch.perform_deep_munge = true
|
|
21
|
+
#
|
|
22
|
+
# # good
|
|
23
|
+
# # Deep munge is not explicitly disabled
|
|
24
|
+
#
|
|
16
25
|
# See CVE-2012-2660, CVE-2012-2694, and CVE-2013-0155.
|
|
26
|
+
|
|
17
27
|
class DeepMunge < RuboCop::Cop::Base
|
|
18
28
|
MSG = 'Never disable the deep munge security option.'
|
|
19
29
|
|
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
module Rubocop
|
|
4
4
|
module Cop
|
|
5
5
|
# Cop that prevents the use of `in_batches`
|
|
6
|
+
# @example
|
|
7
|
+
# # bad
|
|
8
|
+
# Foo.in_batches do |relation|
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# # good
|
|
12
|
+
# Foo.each_batch do |relation|
|
|
13
|
+
# end
|
|
6
14
|
class InBatches < RuboCop::Cop::Base
|
|
7
15
|
MSG = 'Do not use `in_batches`, use `each_batch` from the EachBatch module instead'
|
|
8
16
|
|
|
@@ -81,7 +81,7 @@ module Rubocop
|
|
|
81
81
|
index_of_example = lines.index { |line| line.text.match?(/# @example( |$)/) }
|
|
82
82
|
return false unless index_of_example
|
|
83
83
|
|
|
84
|
-
lines_after_example = lines[index_of_example + 1..]
|
|
84
|
+
lines_after_example = lines[(index_of_example + 1)..]
|
|
85
85
|
|
|
86
86
|
lines_after_example.any? { |line| line.text.downcase.match?(/# # good( |$)/) } &&
|
|
87
87
|
lines_after_example.any? { |line| line.text.downcase.match?(/# # bad( |$)/) }
|
|
@@ -4,6 +4,18 @@ module Rubocop
|
|
|
4
4
|
module Cop
|
|
5
5
|
module InternalAffairs
|
|
6
6
|
# Cop that denies the use of CopHelper.
|
|
7
|
+
# @example
|
|
8
|
+
# # bad
|
|
9
|
+
# include CopHelper
|
|
10
|
+
#
|
|
11
|
+
# inspect_source(<<~RUBY)
|
|
12
|
+
# #....
|
|
13
|
+
# RUBY
|
|
14
|
+
#
|
|
15
|
+
# # good
|
|
16
|
+
# expect_offense(<<~RUBY)
|
|
17
|
+
# # ....
|
|
18
|
+
# RUBY
|
|
7
19
|
class DeprecateCopHelper < RuboCop::Cop::Base
|
|
8
20
|
MSG = 'Do not use `CopHelper` or methods from it, use improved patterns described in https://www.rubydoc.info/gems/rubocop/RuboCop/RSpec/ExpectOffense'
|
|
9
21
|
|
|
@@ -45,37 +45,35 @@ module RuboCop
|
|
|
45
45
|
MSG = 'Define constant `RESTRICT_ON_SEND` to speed up calls to `on_send`. ' \
|
|
46
46
|
'The following line is then no longer necessary:'
|
|
47
47
|
|
|
48
|
-
# @!method method_name_plain(node)
|
|
48
|
+
# @!method method_name_plain(node, send_param)
|
|
49
49
|
def_node_matcher :method_name_plain, <<~PATTERN
|
|
50
50
|
{
|
|
51
|
-
(send _ :method_name
|
|
52
|
-
(send
|
|
53
|
-
(send _ :children) :[] (int 1) # node.children[1]
|
|
54
|
-
)
|
|
51
|
+
(send _ :method_name (lvar %1) ...) # method_name(on_send_param)
|
|
52
|
+
(send (send (lvar %1) :children) :[] (int 1)) # on_send_param.children[1]
|
|
55
53
|
}
|
|
56
54
|
PATTERN
|
|
57
55
|
|
|
58
|
-
# @!method method_name_call(node)
|
|
56
|
+
# @!method method_name_call(node, send_param, local_assignments)
|
|
59
57
|
def_node_matcher :method_name_call, <<~PATTERN
|
|
60
58
|
{
|
|
61
|
-
#method_name_plain
|
|
62
|
-
(lvar %
|
|
59
|
+
#method_name_plain(%1) # direct method_name/children[1] call on the on_send param
|
|
60
|
+
(lvar %2) # local variable previously assigned from such a call
|
|
63
61
|
}
|
|
64
62
|
PATTERN
|
|
65
63
|
|
|
66
|
-
# @!method method_name_assignment(node)
|
|
64
|
+
# @!method method_name_assignment(node, send_param)
|
|
67
65
|
def_node_search :method_name_assignment, <<~PATTERN
|
|
68
|
-
(lvasgn $
|
|
66
|
+
(lvasgn $_ #method_name_plain(%1))
|
|
69
67
|
PATTERN
|
|
70
68
|
|
|
71
|
-
# @!method method_name_check(node)
|
|
69
|
+
# @!method method_name_check(node, send_param, local_assignments)
|
|
72
70
|
def_node_search :method_name_check, <<~PATTERN
|
|
73
71
|
(if
|
|
74
72
|
${
|
|
75
|
-
(send #method_name_call(%1) {:== :!=} _) # method_name(
|
|
76
|
-
(send _ :include? #method_name_call(%1)) # a.include?(method_name(
|
|
73
|
+
(send #method_name_call(%1, %2) {:== :!=} _) # method_name(on_send_param) == foo
|
|
74
|
+
(send _ :include? #method_name_call(%1, %2)) # a.include?(method_name(on_send_param))
|
|
77
75
|
}
|
|
78
|
-
{!nil? nil? | nil? !nil?}
|
|
76
|
+
{!nil? nil? | nil? !nil?} # has either `if` or `else` branch - not both
|
|
79
77
|
)
|
|
80
78
|
PATTERN
|
|
81
79
|
|
|
@@ -83,9 +81,12 @@ module RuboCop
|
|
|
83
81
|
return unless node.method?(:on_send)
|
|
84
82
|
return if @restrict_on_send_set
|
|
85
83
|
|
|
86
|
-
|
|
84
|
+
send_param = node.first_argument&.name
|
|
85
|
+
return unless send_param
|
|
87
86
|
|
|
88
|
-
|
|
87
|
+
local_assignments = method_name_assignment(node, send_param).to_set
|
|
88
|
+
|
|
89
|
+
method_name_check(node, send_param, local_assignments) do |call_node|
|
|
89
90
|
add_offense(call_node)
|
|
90
91
|
end
|
|
91
92
|
end
|
|
@@ -5,43 +5,43 @@ module Rubocop
|
|
|
5
5
|
# Ensures a line break around conditional blocks.
|
|
6
6
|
#
|
|
7
7
|
# @example
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
8
|
+
# # bad
|
|
9
|
+
# do_something
|
|
10
|
+
# if condition
|
|
11
|
+
# do_extra_stuff
|
|
12
|
+
# end
|
|
13
|
+
# do_something_more
|
|
14
14
|
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
15
|
+
# # good
|
|
16
|
+
# do_something
|
|
17
17
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
18
|
+
# if condition
|
|
19
|
+
# do_extra_stuff
|
|
20
|
+
# end
|
|
21
21
|
#
|
|
22
|
-
#
|
|
22
|
+
# do_something_more
|
|
23
23
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
24
|
+
# # bad
|
|
25
|
+
# do_something
|
|
26
|
+
# unless condition
|
|
27
|
+
# do_extra_stuff
|
|
28
|
+
# end
|
|
29
29
|
#
|
|
30
|
-
#
|
|
30
|
+
# do_something_more
|
|
31
31
|
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
32
|
+
# # good
|
|
33
|
+
# def a_method
|
|
34
|
+
# if condition
|
|
35
|
+
# do_something
|
|
36
|
+
# end
|
|
37
|
+
# end
|
|
38
38
|
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
39
|
+
# # good
|
|
40
|
+
# on_block do
|
|
41
|
+
# if condition
|
|
42
|
+
# do_something
|
|
43
|
+
# end
|
|
44
|
+
# end
|
|
45
45
|
class LineBreakAroundConditionalBlock < RuboCop::Cop::Base
|
|
46
46
|
extend RuboCop::Cop::AutoCorrector
|
|
47
47
|
include RuboCop::Cop::RangeHelp
|
|
@@ -100,7 +100,7 @@ module Rubocop
|
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
def end_clause_line?(line)
|
|
103
|
-
line =~ /^\s*(#|rescue|else|elsif|when)/
|
|
103
|
+
line =~ /^\s*(#|rescue|else|elsif|when|ensure)/
|
|
104
104
|
end
|
|
105
105
|
|
|
106
106
|
def begin_line?(line)
|
|
@@ -8,10 +8,33 @@ module Rubocop
|
|
|
8
8
|
# Checks for methods that may lead to batch type issues on a table that's been
|
|
9
9
|
# explicitly denied because of its size.
|
|
10
10
|
#
|
|
11
|
-
# Even though
|
|
11
|
+
# Even though these methods perform functions to avoid
|
|
12
12
|
# downtime, using it with tables with millions of rows still causes a
|
|
13
13
|
# significant delay in the deploy process and is best avoided.
|
|
14
14
|
#
|
|
15
|
+
# @example
|
|
16
|
+
#
|
|
17
|
+
# # bad
|
|
18
|
+
# class ExampleMigration < ActiveRecord::Migration[7.0]
|
|
19
|
+
# def change
|
|
20
|
+
# denied_method(:denied_table, column: :value)
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# # good
|
|
25
|
+
# class ExampleMigration < ActiveRecord::Migration[7.0]
|
|
26
|
+
# def change
|
|
27
|
+
# denied_method(:allowed_table, column: :value)
|
|
28
|
+
# end
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
# # good
|
|
32
|
+
# class ExampleMigration < ActiveRecord::Migration[7.0]
|
|
33
|
+
# def change
|
|
34
|
+
# allowed_method(:denied_table, column: :value)
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
15
38
|
# See https://gitlab.com/gitlab-com/infrastructure/issues/1602 for more
|
|
16
39
|
# information.
|
|
17
40
|
class UpdateLargeTable < RuboCop::Cop::Base
|
|
@@ -8,6 +8,12 @@ module Rubocop
|
|
|
8
8
|
# files and directories, this can be very expensive even when the archive's size
|
|
9
9
|
# in bytes is small.
|
|
10
10
|
#
|
|
11
|
+
# @example
|
|
12
|
+
# # bad
|
|
13
|
+
# Zip::File.open('file_path.zip', &block)
|
|
14
|
+
#
|
|
15
|
+
# # good
|
|
16
|
+
# Zip::InputStream.open('file_path.zip')
|
|
11
17
|
# See also:
|
|
12
18
|
# - https://github.com/rubyzip/rubyzip/issues/506
|
|
13
19
|
# - https://github.com/rubyzip/rubyzip#notes-on-zipinputstream
|
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
module Rubocop
|
|
4
4
|
module Cop
|
|
5
5
|
# Cop that prevents the use of polymorphic associations
|
|
6
|
+
# @example
|
|
7
|
+
# # bad
|
|
8
|
+
# class Comment < ApplicationRecord
|
|
9
|
+
# belongs_to :commentable, polymorphic: true
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# # good
|
|
13
|
+
# class Comment < ApplicationRecord
|
|
14
|
+
# belongs_to :post
|
|
15
|
+
# belongs_to :article
|
|
16
|
+
# end
|
|
6
17
|
class PolymorphicAssociations < RuboCop::Cop::Base
|
|
7
18
|
MSG = 'Do not use polymorphic associations, use separate tables instead'
|
|
8
19
|
|
|
@@ -46,9 +46,9 @@ module Rubocop
|
|
|
46
46
|
# @!method shared_examples(node)
|
|
47
47
|
def_node_matcher :shared_examples, <<~PATTERN
|
|
48
48
|
{
|
|
49
|
-
(
|
|
49
|
+
(any_block (send #rspec? #SharedGroups.all ...) ...)
|
|
50
50
|
{
|
|
51
|
-
(
|
|
51
|
+
(any_block (send nil? #Includes.all ...) ...)
|
|
52
52
|
(send nil? #Includes.all ...)
|
|
53
53
|
}
|
|
54
54
|
}
|
|
@@ -64,6 +64,7 @@ module Rubocop
|
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
66
|
alias_method :on_numblock, :on_block
|
|
67
|
+
alias_method :on_itblock, :on_block
|
|
67
68
|
end
|
|
68
69
|
end
|
|
69
70
|
end
|
|
@@ -54,7 +54,7 @@ module Rubocop
|
|
|
54
54
|
|
|
55
55
|
# @!method it_description(node)
|
|
56
56
|
def_node_matcher :it_description, <<-PATTERN
|
|
57
|
-
(
|
|
57
|
+
(any_block (send _ :it ${
|
|
58
58
|
(str $_)
|
|
59
59
|
(dstr (str $_ ) ...)
|
|
60
60
|
} ...) ...)
|
|
@@ -66,6 +66,7 @@ module Rubocop
|
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
alias_method :on_numblock, :on_block
|
|
69
|
+
alias_method :on_itblock, :on_block
|
|
69
70
|
|
|
70
71
|
private
|
|
71
72
|
|
|
@@ -91,9 +92,7 @@ module Rubocop
|
|
|
91
92
|
end
|
|
92
93
|
|
|
93
94
|
def replacement_text(node)
|
|
94
|
-
text = text(node)
|
|
95
|
-
|
|
96
|
-
text.strip!
|
|
95
|
+
text = text(node).strip
|
|
97
96
|
|
|
98
97
|
text = downcase_first_letter(text) if invalid_description?(text)
|
|
99
98
|
|