Commit e1b82207 authored by Peter Leitzen's avatar Peter Leitzen Committed by Mayra Cabrera

Add cop Style/RegexpLiteralMixedPreserve [RUN AS-IF-FOSS]

parent 42954bd5
......@@ -644,3 +644,16 @@ Cop/UserAdmin:
Performance/OpenStruct:
Exclude:
- 'ee/spec/**/*.rb'
# See https://gitlab.com/gitlab-org/gitlab/-/issues/327495
Style/RegexpLiteral:
Enabled: false
Style/RegexpLiteralMixedPreserve:
Enabled: true
SupportedStyles:
- slashes
- percent_r
- mixed
- mixed_preserve
EnforcedStyle: mixed_preserve
......@@ -3337,3 +3337,60 @@ Gitlab/FeatureAvailableUsage:
- 'ee/spec/models/project_spec.rb'
- 'lib/api/helpers/related_resources_helpers.rb'
- 'spec/models/concerns/featurable_spec.rb'
# WIP See https://gitlab.com/gitlab-org/gitlab/-/issues/327490
Style/RegexpLiteralMixedPreserve:
Exclude:
- 'app/controllers/projects/repositories_controller.rb'
- 'app/helpers/ci/variables_helper.rb'
- 'app/models/alert_management/alert.rb'
- 'app/models/application_setting.rb'
- 'app/models/blob_viewer/go_mod.rb'
- 'app/models/concerns/ci/maskable.rb'
- 'app/models/operations/feature_flag.rb'
- 'app/models/packages/go/module.rb'
- 'app/models/project_services/chat_message/base_message.rb'
- 'app/services/packages/conan/search_service.rb'
- 'app/services/projects/update_remote_mirror_service.rb'
- 'config/initializers/rspec_profiling.rb'
- 'ee/app/models/status_page/project_setting.rb'
- 'ee/app/presenters/vulnerability_presenter.rb'
- 'ee/lib/api/geo_nodes.rb'
- 'ee/lib/gitlab/vulnerabilities/standard_vulnerability.rb'
- 'ee/spec/controllers/concerns/ee/routable_actions/sso_enforcement_redirect_spec.rb'
- 'ee/spec/controllers/concerns/routable_actions_spec.rb'
- 'ee/spec/controllers/groups/groups_controller_spec.rb'
- 'ee/spec/features/groups/saml_enforcement_spec.rb'
- 'ee/spec/features/markdown/metrics_spec.rb'
- 'ee/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb'
- 'ee/spec/models/project_services/jira_service_spec.rb'
- 'ee/spec/services/jira/requests/issues/list_service_spec.rb'
- 'lib/api/invitations.rb'
- 'lib/gitlab/ci/pipeline/expression/lexeme/pattern.rb'
- 'lib/gitlab/metrics/requests_rack_middleware.rb'
- 'lib/gitlab/metrics/subscribers/active_record.rb'
- 'lib/gitlab/regex.rb'
- 'lib/gitlab/utils.rb'
- 'lib/product_analytics/tracker.rb'
- 'qa/qa/page/project/settings/advanced.rb'
- 'qa/spec/service/docker_run/gitlab_runner_spec.rb'
- 'rubocop/cop/gitlab/duplicate_spec_location.rb'
- 'spec/features/clusters/cluster_health_dashboard_spec.rb'
- 'spec/features/markdown/metrics_spec.rb'
- 'spec/features/search/user_searches_for_code_spec.rb'
- 'spec/features/snippets/embedded_snippet_spec.rb'
- 'spec/helpers/diff_helper_spec.rb'
- 'spec/helpers/releases_helper_spec.rb'
- 'spec/lib/gitlab/ci/reports/test_case_spec.rb'
- 'spec/lib/gitlab/consul/internal_spec.rb'
- 'spec/lib/gitlab/import_export/shared_spec.rb'
- 'spec/lib/gitlab/utils/usage_data_spec.rb'
- 'spec/presenters/ci/build_runner_presenter_spec.rb'
- 'spec/requests/api/projects_spec.rb'
- 'spec/services/jira/requests/projects/list_service_spec.rb'
- 'spec/support/capybara.rb'
- 'spec/support/helpers/grafana_api_helpers.rb'
- 'spec/support/helpers/query_recorder.rb'
- 'spec/support/helpers/require_migration.rb'
- 'spec/support/shared_examples/models/slack_mattermost_notifications_shared_examples.rb'
- 'spec/views/layouts/_head.html.haml_spec.rb'
......@@ -922,13 +922,6 @@ Style/RedundantRegexpEscape:
Style/RedundantSelf:
Enabled: false
# Offense count: 213
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, AllowInnerSlashes.
# SupportedStyles: slashes, percent_r, mixed
Style/RegexpLiteral:
Enabled: false
# Offense count: 53
# Cop supports --auto-correct.
Style/RescueModifier:
......
- current_route_path = request.fullpath.match(/-\/tree\/[^\/]+\/(.+$)/).to_a[1]
- current_route_path = request.fullpath.match(%r{-/tree/[^/]+/(.+$)}).to_a[1]
- add_page_startup_graphql_call('repository/path_last_commit', { projectPath: @project.full_path, ref: current_ref, path: current_route_path || "" })
- @content_class = "limit-container-width" unless fluid_layout
- @skip_current_level_breadcrumb = true
......
- current_route_path = request.fullpath.match(/-\/tree\/[^\/]+\/(.+$)/).to_a[1]
- current_route_path = request.fullpath.match(%r{-/tree/[^/]+/(.+$)}).to_a[1]
- add_page_startup_graphql_call('repository/path_last_commit', { projectPath: @project.full_path, ref: current_ref, path: current_route_path || "" })
- add_page_startup_graphql_call('repository/permissions', { projectPath: @project.full_path })
- add_page_startup_graphql_call('repository/files', { nextPageCursor: "", pageSize: 100, projectPath: @project.full_path, ref: current_ref, path: current_route_path || "/"})
......
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop is based on `Style/RegexpLiteral` but adds a new
# `EnforcedStyle` option `mixed_preserve`.
#
# This cop will be removed once the upstream PR is merged and RuboCop upgraded.
#
# See https://github.com/rubocop/rubocop/pull/9688
class RegexpLiteralMixedPreserve < RuboCop::Cop::Style::RegexpLiteral
module Patch
private
def allowed_slash_literal?(node)
super || allowed_mixed_preserve?(node)
end
def allowed_percent_r_literal?(node)
super || allowed_mixed_preserve?(node)
end
def allowed_mixed_preserve?(node)
style == :mixed_preserve && !contains_disallowed_slash?(node)
end
end
prepend Patch
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/style/regexp_literal_mixed_preserve'
# This spec contains only relevant examples.
#
# See also https://github.com/rubocop/rubocop/pull/9688
RSpec.describe RuboCop::Cop::Style::RegexpLiteralMixedPreserve, :config do
let(:config) do
supported_styles = { 'SupportedStyles' => %w[slashes percent_r mixed mixed_preserve] }
RuboCop::Config.new('Style/PercentLiteralDelimiters' =>
percent_literal_delimiters_config,
'Style/RegexpLiteralMixedPreserve' =>
cop_config.merge(supported_styles))
end
let(:percent_literal_delimiters_config) { { 'PreferredDelimiters' => { '%r' => '{}' } } }
context 'when EnforcedStyle is set to mixed_preserve' do
let(:cop_config) { { 'EnforcedStyle' => 'mixed_preserve' } }
describe 'a single-line `//` regex without slashes' do
it 'is accepted' do
expect_no_offenses('foo = /a/')
end
end
describe 'a single-line `//` regex with slashes' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
foo = /home\//
^^^^^^^^ Use `%r` around regular expression.
RUBY
expect_correction(<<~'RUBY')
foo = %r{home/}
RUBY
end
describe 'when configured to allow inner slashes' do
before do
cop_config['AllowInnerSlashes'] = true
end
it 'is accepted' do
expect_no_offenses('foo = /home\\//')
end
end
end
describe 'a multi-line `//` regex without slashes' do
it 'is accepted' do
expect_no_offenses(<<~'RUBY')
foo = /
foo
bar
/x
RUBY
end
end
describe 'a multi-line `//` regex with slashes' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
foo = /
^ Use `%r` around regular expression.
https?:\/\/
example\.com
/x
RUBY
expect_correction(<<~'RUBY')
foo = %r{
https?://
example\.com
}x
RUBY
end
end
describe 'a single-line %r regex without slashes' do
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{a}
RUBY
end
end
describe 'a single-line %r regex with slashes' do
it 'is accepted' do
expect_no_offenses('foo = %r{home/}')
end
describe 'when configured to allow inner slashes' do
before do
cop_config['AllowInnerSlashes'] = true
end
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{home/}
RUBY
end
end
end
describe 'a multi-line %r regex without slashes' do
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{
foo
bar
}x
RUBY
end
end
describe 'a multi-line %r regex with slashes' do
it 'is accepted' do
expect_no_offenses(<<~RUBY)
foo = %r{
https?://
example\.com
}x
RUBY
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment