Skip to content

Commit

Permalink
Merge pull request #9901 from jonas054/9891_auto_gen_config_hash_syntax
Browse files Browse the repository at this point in the history
[Fix #9891] Fix --auto-gen-config bug for Style/HashSyntax
  • Loading branch information
koic committed Jul 5, 2021
2 parents d2fbf72 + 98d229f commit 61dc5da
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions 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][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_syntax.rb
Expand Up @@ -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
Expand Down
37 changes: 33 additions & 4 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1190,6 +1192,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
Expand Down

0 comments on commit 61dc5da

Please sign in to comment.