Skip to content

Commit

Permalink
Include expanded EnforcedStyle options when --no-auto-gen-enforced-st…
Browse files Browse the repository at this point in the history
…yle is given
  • Loading branch information
kpost committed Nov 14, 2023
1 parent 16e8a30 commit 9613eba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12388](https://github.com/rubocop/rubocop/pull/12388): Reject additional 'expanded' `EnforcedStyle` options when `--no-auto-gen-enforced-style` is given. ([@kpost][])
10 changes: 8 additions & 2 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def filtered_config(cfg)
# 'Enabled' option will be put into file only if exclude
# limit is exceeded.
rejected_keys = ['Enabled']
rejected_keys << 'EnforcedStyle' unless auto_gen_enforced_style?
cfg.reject { |key| rejected_keys.include?(key) }
rejected_keys << /^EnforcedStyle\w*/ unless auto_gen_enforced_style?
cfg.reject { |key| include_or_match?(rejected_keys, key) }
end

def output_offending_files(output_buffer, cfg, cop_name)
Expand Down Expand Up @@ -262,6 +262,12 @@ def safe_autocorrect?(config)
def no_exclude_limit?
@options[:no_exclude_limit] == false
end

# Returns true if the given arr include the given elm or if any of the
# given arr is a regexp that matches the given elm.
def include_or_match?(arr, elm)
arr.include?(elm) || arr.any? { |x| x.is_a?(Regexp) && x.match?(elm) }
end
end
end
end
15 changes: 15 additions & 0 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,21 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
- 'example1.rb'
YAML
end

it 'generates Exclude for expanded EnforcedStyle if it solves all offenses' do
create_file('example1.rb', ['# frozen_string_literal: true', '', 'h{}'])

expect(cli.run(['--auto-gen-config', '--no-auto-gen-enforced-style'])).to eq(0)
expect(File.readlines('.rubocop_todo.yml')[10..].join)
.to eq(<<~YAML)
# Configuration parameters: EnforcedStyle.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceBeforeBlockBraces:
Exclude:
- 'example1.rb'
YAML
end
end

context 'when hash value omission enabled', :ruby31 do
Expand Down

0 comments on commit 9613eba

Please sign in to comment.