From 8cfdc57c2ca20685dae4a6ea2872bb38f544ef71 Mon Sep 17 00:00:00 2001 From: Yusuke Nakamura Date: Thu, 8 Aug 2019 18:50:02 +0900 Subject: [PATCH] Show warning message if passed string to 'Enabled' key in .rubocop.yml It causes unexpected cop warnings because treats as truthy value if passed neither true nor false in 'Enabled' key's value like "Enabled: disable". #7056 --- .rubocop_todo.yml | 2 +- CHANGELOG.md | 1 + lib/rubocop/config_loader.rb | 18 ++++++++++++++++++ spec/rubocop/config_loader_spec.rb | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) 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 5eef53554354..5404c040b817 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' }