From 3d57dc5492051c1cbde21823f5161a289b6bd807 Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Thu, 1 Jul 2021 21:00:36 +0200 Subject: [PATCH 1/2] [Fix #9891] Fix --auto-gen-config bug for Style/HashSyntax We must call ruby19_check() even if there are no hash rockets in the inspected node, to give it a chance to register that the correct style has been detected. The generation of .rubocop_todo.yml depends on it. --- .../fix_autogenconfig_bug_for_HashSyntax.md | 1 + lib/rubocop/cop/style/hash_syntax.rb | 2 +- spec/rubocop/cli/auto_gen_config_spec.rb | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_autogenconfig_bug_for_HashSyntax.md diff --git a/changelog/fix_autogenconfig_bug_for_HashSyntax.md b/changelog/fix_autogenconfig_bug_for_HashSyntax.md new file mode 100644 index 00000000000..b1ee3866321 --- /dev/null +++ b/changelog/fix_autogenconfig_bug_for_HashSyntax.md @@ -0,0 +1 @@ +* [#9891](https://github.com/rubocop/rubocop/issues/9891): Fix `--auto-gen-config` bug for `Style/HashSyntax`. ([@jonas054][]) diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index 031b1c619ed..2d7c2e63022 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -74,7 +74,7 @@ def on_hash(node) ruby19_no_mixed_keys_check(pairs) elsif style == :no_mixed_keys no_mixed_keys_check(pairs) - elsif node.source.include?('=>') + else ruby19_check(pairs) end end diff --git a/spec/rubocop/cli/auto_gen_config_spec.rb b/spec/rubocop/cli/auto_gen_config_spec.rb index c41c63c4924..de5ba8238b0 100644 --- a/spec/rubocop/cli/auto_gen_config_spec.rb +++ b/spec/rubocop/cli/auto_gen_config_spec.rb @@ -1190,6 +1190,33 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7) end end + it 'generates EnforcedStyle parameter if it solves all offenses' do + create_file('example1.rb', ['# frozen_string_literal: true', '', 'h(:a => 1)']) + + expect(cli.run(['--auto-gen-config'])).to eq(0) + expect(File.readlines('.rubocop_todo.yml')[10..-1].join) + .to eq(<<~YAML) + # Configuration parameters: UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. + # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys + Style/HashSyntax: + EnforcedStyle: hash_rockets + YAML + end + + it 'generates Exclude if no EnforcedStyle solves all offenses' do + create_file('example1.rb', ['# frozen_string_literal: true', '', 'h(:a => 1)', 'h(b: 2)']) + + expect(cli.run(['--auto-gen-config'])).to eq(0) + expect(File.readlines('.rubocop_todo.yml')[10..-1].join) + .to eq(<<~YAML) + # Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. + # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys + Style/HashSyntax: + Exclude: + - 'example1.rb' + YAML + end + it 'can be called when there are no files to inspection' do expect(cli.run(['--auto-gen-config'])).to eq(0) end From 98d229fdf34c109fa240810aee9578ed010ed22f Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Thu, 1 Jul 2021 23:16:59 +0200 Subject: [PATCH 2/2] Make auto_gen_config_spec.rb more stable The previous commit caused failures in this file for some ruby versions in the CI builds. I have not been able to reproduce locally, but I suspect that since DisabledConfigFormatter has two singleton variables that keep their values between test cases, we need to clear both of them. --- spec/rubocop/cli/auto_gen_config_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/rubocop/cli/auto_gen_config_spec.rb b/spec/rubocop/cli/auto_gen_config_spec.rb index de5ba8238b0..78bb5b2659d 100644 --- a/spec/rubocop/cli/auto_gen_config_spec.rb +++ b/spec/rubocop/cli/auto_gen_config_spec.rb @@ -8,7 +8,10 @@ include_context 'cli spec behavior' describe '--auto-gen-config' do - before { RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} } + before do + RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {} + RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} + end shared_examples 'LineLength handling' do |ctx, initial_dotfile, exp_dotfile| context ctx do @@ -446,11 +449,10 @@ def fooBar; end todo_contents = File.read('.rubocop_todo.yml').lines[8..-1].join expect(todo_contents).to eq(<<~YAML) # Offense count: 1 - # Configuration parameters: EnforcedStyle, IgnoredPatterns. + # Configuration parameters: IgnoredPatterns. # SupportedStyles: snake_case, camelCase Naming/MethodName: - Exclude: - - 'example1.rb' + EnforcedStyle: camelCase # Offense count: 1 # Cop supports --auto-correct.