Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show warning message if passed string to 'Enabled' key in .rubocop.yml #7272

Merged
merged 1 commit into from Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, This URL seems to be #7056.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoud I have to change the url to #7056 ? (But already merged it... Sorry too late.)


### 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