Skip to content

Commit

Permalink
Add auto-correction for Lint/BooleanSymbol cop
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Mar 24, 2020
1 parent fcfa953 commit dce5ab0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@
* [#7809](https://github.com/rubocop-hq/rubocop/pull/7809): Add auto-correction for `Style/EndBlock` cop. ([@tejasbubane][])
* [#7739](https://github.com/rubocop-hq/rubocop/pull/7739): Add `IgnoreNotImplementedMethods` configuration to `Lint/UnusedMethodArgument`. ([@tejasbubane][])
* [#7740](https://github.com/rubocop-hq/rubocop/issues/7740): Add `AllowModifiersOnSymbols` configuration to `Style/AccessModifierDeclarations`. ([@tejasbubane][])
* [#7812](https://github.com/rubocop-hq/rubocop/pull/7812): Add auto-correction for `Lint/BooleanSymbol` cop. ([@tejasbubane][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1323,6 +1323,7 @@ Lint/BooleanSymbol:
Description: 'Check for `:true` and `:false` symbols.'
Enabled: true
VersionAdded: '0.50'
VersionChanged: '0.81'

Lint/CircularArgumentReference:
Description: "Default values in optional keyword arguments and optional ordinal arguments should not refer back to the name of the argument."
Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/cop/lint/boolean_symbol.rb
Expand Up @@ -32,6 +32,18 @@ def on_sym(node)

add_offense(node, message: format(MSG, boolean: node.value))
end

def autocorrect(node)
lambda do |corrector|
boolean_literal = node.source.delete(':')
parent = node.parent
if parent&.pair_type?
corrector.remove(parent.loc.operator)
boolean_literal = "#{node.source} =>"
end
corrector.replace(node.loc.expression, boolean_literal)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_lint.md
Expand Up @@ -166,7 +166,7 @@ BigDecimal(123.456, 3)

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.50 | -
Enabled | Yes | Yes | 0.50 | 0.81

This cop checks for `:true` and `:false` symbols.
In most cases it would be a typo.
Expand Down
18 changes: 17 additions & 1 deletion spec/rubocop/cop/lint/boolean_symbol_spec.rb
Expand Up @@ -8,13 +8,21 @@
:true
^^^^^ Symbol with a boolean name - you probably meant to use `true`.
RUBY

expect_correction(<<~RUBY)
true
RUBY
end

it 'registers an offense when using `:false`' do
expect_offense(<<~RUBY)
:false
^^^^^^ Symbol with a boolean name - you probably meant to use `false`.
RUBY

expect_correction(<<~RUBY)
false
RUBY
end

context 'when using the new hash syntax' do
Expand All @@ -23,13 +31,21 @@
{ true: 'Foo' }
^^^^ Symbol with a boolean name - you probably meant to use `true`.
RUBY

expect_correction(<<~RUBY)
{ true => 'Foo' }
RUBY
end

it 'registers an offense when using `false:`' do
expect_offense(<<~RUBY)
{ false: 'Bar' }
{ false: :bar }
^^^^^ Symbol with a boolean name - you probably meant to use `false`.
RUBY

expect_correction(<<~RUBY)
{ false => :bar }
RUBY
end
end

Expand Down

0 comments on commit dce5ab0

Please sign in to comment.