Skip to content

Commit

Permalink
Show warning message if passed string to 'Enabled' key in .rubocop.yml
Browse files Browse the repository at this point in the history
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
  • Loading branch information
unasuke authored and bbatsov committed Aug 19, 2019
1 parent 5657091 commit 8cfdc57
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions lib/rubocop/config_loader.rb
Expand Up @@ -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

Expand All @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -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' }

Expand Down

0 comments on commit 8cfdc57

Please sign in to comment.