Skip to content

Commit

Permalink
Skip LineLength phase on --auto-gen-only-exclude
Browse files Browse the repository at this point in the history
Phase 1 of config auto-generation runs the `Layout/LineLength` cop on
all files to discover the `Max` line length, so it can be used by cops
which depend on it.

The `--auto-gen-only-exclude` config skips generating any `Max` configs,
meaning phase 1 is redundant, as we won't generate the `Max` for use by
phase 2.

Therefore, we can skip phase 1 entirely.
  • Loading branch information
sambostock committed Mar 6, 2024
1 parent 90b4283 commit 15fb5c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12730](https://github.com/rubocop/rubocop/pull/12730): Skip `LineLength` phase on `--auto-gen-only-exclude`. ([@sambostock][])
15 changes: 12 additions & 3 deletions lib/rubocop/cli/command/auto_generate_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class AutoGenerateConfig < Base

PHASE_1_OVERRIDDEN = '(skipped because the default Layout/LineLength:Max is overridden)'
PHASE_1_DISABLED = '(skipped because Layout/LineLength is disabled)'
PHASE_1_SKIPPED = '(skipped because a list of cops is passed to the `--only` flag)'
PHASE_1_SKIPPED_ONLY_COPS =
'(skipped because a list of cops is passed to the `--only` flag)'
PHASE_1_SKIPPED_ONLY_EXCLUDE =
'(skipped because only excludes will be generated due to `--auto-gen-only-exclude` flag)'

def run
add_formatter
Expand All @@ -29,12 +32,14 @@ def run
private

def maybe_run_line_length_cop
if !line_length_enabled?(@config_store.for_pwd)
if only_exclude?
skip_line_length_cop(PHASE_1_SKIPPED_ONLY_EXCLUDE)
elsif !line_length_enabled?(@config_store.for_pwd)
skip_line_length_cop(PHASE_1_DISABLED)
elsif !same_max_line_length?(@config_store.for_pwd, ConfigLoader.default_configuration)
skip_line_length_cop(PHASE_1_OVERRIDDEN)
elsif options_has_only_flag?
skip_line_length_cop(PHASE_1_SKIPPED)
skip_line_length_cop(PHASE_1_SKIPPED_ONLY_COPS)
else
run_line_length_cop
end
Expand Down Expand Up @@ -65,6 +70,10 @@ def options_has_only_flag?
@options[:only]
end

def only_exclude?
@options[:auto_gen_only_exclude]
end

# Do an initial run with only Layout/LineLength so that cops that
# depend on Layout/LineLength:Max get the correct value for that
# parameter.
Expand Down
11 changes: 8 additions & 3 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,13 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
# absolute Exclude paths will point into this example's work directory.
RuboCop::ConfigLoader.default_configuration = nil

$stdout = StringIO.new
expect(cli.run(['--auto-gen-config', '--auto-gen-only-exclude',
'--exclude-limit', '1'])).to eq(0)
expect($stdout.string.include?(<<~STRING)).to be(true)
Phase 1 of 2: run Layout/LineLength cop (skipped because only excludes will be generated due to `--auto-gen-only-exclude` flag)
Phase 2 of 2: run all cops
STRING
actual = File.read('.rubocop_todo.yml').split($RS)

# With --exclude-limit 1 we get MinDigits generated for NumericLiterals
Expand All @@ -1319,6 +1324,9 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
# the same cop in a single file. Exclude properties are generated for
# them.
expect(actual.grep(/^[^#]/).join($RS)).to eq(<<~YAML.chomp)
Layout/LineLength:
Exclude:
- 'example1.rb'
Lint/UnusedMethodArgument:
Exclude:
- 'example2.rb'
Expand All @@ -1329,9 +1337,6 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
Enabled: false
Style/NumericLiterals:
MinDigits: 7
Layout/LineLength:
Exclude:
- 'example1.rb'
YAML
end

Expand Down

0 comments on commit 15fb5c7

Please sign in to comment.