diff --git a/changelog/fix_fix_an_obsolete_option_configuration.md b/changelog/fix_fix_an_obsolete_option_configuration.md new file mode 100644 index 00000000000..e4f2b9cdfa0 --- /dev/null +++ b/changelog/fix_fix_an_obsolete_option_configuration.md @@ -0,0 +1 @@ +* [#10875](https://github.com/rubocop/rubocop/pull/10875): Fix an obsolete option configuration values are duplicated when generating `.rubocop_todo.yml`. ([@ydah][]) diff --git a/lib/rubocop/formatter/disabled_config_formatter.rb b/lib/rubocop/formatter/disabled_config_formatter.rb index a2c13a6f281..767dd94abd5 100644 --- a/lib/rubocop/formatter/disabled_config_formatter.rb +++ b/lib/rubocop/formatter/disabled_config_formatter.rb @@ -161,7 +161,7 @@ def output_cop_param_comments(output_buffer, params, default_cfg) next unless value.is_a?(Array) next if value.empty? - output_buffer.puts "# #{param}: #{value.join(', ')}" + output_buffer.puts "# #{param}: #{value.uniq.join(', ')}" end end diff --git a/spec/rubocop/cli/auto_gen_config_spec.rb b/spec/rubocop/cli/auto_gen_config_spec.rb index 4ba3380588f..da17a9d2852 100644 --- a/spec/rubocop/cli/auto_gen_config_spec.rb +++ b/spec/rubocop/cli/auto_gen_config_spec.rb @@ -951,6 +951,33 @@ def a; end YAML end + context 'when configurable parameter is an obsolete parameter' do + it 'displayed as a new parameter setting value without duplication' do + create_file('.rubocop.yml', <<~YAML) + AllCops: + NewCops: enable + Naming/MethodName: + # `IgnoredPatterns` is obsolete. `AllowedPatterns` is newly used. + IgnoredPatterns: + - change + - not_change + YAML + create_file('example1.rb', ['# frozen_string_literal: true', '', 'def fooBar; end']) + create_file('example2.rb', ['# frozen_string_literal: true', '', 'def fooBar; end']) + + expect(cli.run(['--auto-gen-config'])).to eq(0) + File.readlines('.rubocop_todo.yml') + expect(File.readlines('.rubocop_todo.yml')[9..].join) + .to eq(<<~YAML) + # Configuration parameters: AllowedPatterns, IgnoredPatterns. + # SupportedStyles: snake_case, camelCase + # AllowedPatterns: change, not_change + Naming/MethodName: + EnforcedStyle: camelCase + YAML + end + end + it 'does not generate configuration for the Syntax cop' do create_file('example1.rb', <<~RUBY) # frozen_string_literal: true