diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 787dbedd7e92..adad740830b1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -17,7 +17,7 @@ Metrics/AbcSize: # Offense count: 47 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 174 + Max: 182 # Offense count: 219 # Configuration parameters: CountComments, ExcludedMethods. diff --git a/CHANGELOG.md b/CHANGELOG.md index 4335492dad05..94f4ac515928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#7274](https://github.com/rubocop-hq/rubocop/issues/7274): Add new `Lint/SendWithMixinArgument` cop. ([@koic][]) +* [#7272](https://github.com/rubocop-hq/rubocop/pull/7272): Show warning message if passed string to `Enabled`, `Safe`, `SafeAutocorrect`, and `AutoCorrect` keys in .rubocop.yml. ([@unasuke][]) ### Bug fixes diff --git a/lib/rubocop/config_loader.rb b/lib/rubocop/config_loader.rb index 4a474ba3e89a..9a7cf24b9251 100644 --- a/lib/rubocop/config_loader.rb +++ b/lib/rubocop/config_loader.rb @@ -199,6 +199,8 @@ def load_yaml_configuration(absolute_path) raise(TypeError, "Malformed configuration in #{absolute_path}") end + check_cop_config_value(hash) + hash end @@ -220,6 +222,22 @@ def check_duplication(yaml_code, absolute_path) end end + def check_cop_config_value(hash, parent = nil) + hash.each do |key, value| + check_cop_config_value(value, key) if value.is_a?(Hash) + + next unless %w[Enabled + Safe + SafeAutoCorrect + AutoCorrect].include?(key) && value.is_a?(String) + + abort( + "Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow}" \ + " is supposed to be a boolean and #{Rainbow(value).yellow} is not." + ) + end + end + # Read the specified file, or exit with a friendly, concise message on # stderr. Care is taken to use the standard OS exit code for a "file not # found" error. diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 839f3f06ef5d..42ae90e4e188 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -992,6 +992,24 @@ def cop_enabled?(cop_class) end end + context 'set neither true nor false to value to Enabled' do + before do + create_file(configuration_path, <<~YAML) + Layout/AlignArray: + Enabled: disable + YAML + end + + it 'gets a warning message' do + expect do + load_file + end.to raise_error( + SystemExit, + /supposed to be a boolean and disable is not/ + ) + end + end + context 'when the file does not exist' do let(:configuration_path) { 'file_that_does_not_exist.yml' }