Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include expanded EnforcedStyle options when --no-auto-gen-enforced-style is given #12388

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a logic by which the following would work, instead of creating the new include_or_match method?

Suggested change
rejected_keys << /^EnforcedStyle\w*/ unless auto_gen_enforced_style?
cfg.reject { |key| include_or_match?(rejected_keys, key) }
rejected_keys << 'EnforcedStyle' unless auto_gen_enforced_style?
cfg.reject do |key|
rejected_keys.any? { |rejected_key| key.start_with?(rejected_key) }
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this would work too! However then it only works for prefixed strings of course. I tried to anticipate for possible use cases where a key would not necessarily just start with the rejected key.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the current logic is left as is, /^EnforcedStyle\w*/ appears like it should be /\AEnforcedStyle\w*/.

Copy link
Contributor Author

@kpost kpost Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but any regexp could be used. :)

Edit: Ah, sorry, understood you wrong. You're right, \A should be added! What is your opinion on using regexes?

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