Skip to content

Commit

Permalink
Support autocorrect for Lint/DisjunctiveAssignmentInConstructor cop
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jul 13, 2020
1 parent a88857e commit 1a5d073
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### New features

* [#7333](https://github.com/rubocop-hq/rubocop/issues/7333): Add new `Style/RedundantFileExtensionInRequire` cop. ([@fatkodima][])
* [#8316](https://github.com/rubocop-hq/rubocop/pull/8316): Support autocorrect for `Lint/DisjunctiveAssignmentInConstructor` cop. ([@fatkodima][])
* [#8242](https://github.com/rubocop-hq/rubocop/pull/8242): Internal profiling available with `bin/rubocop-profile` and rake tasks. ([@marcandre][])
* [#8295](https://github.com/rubocop-hq/rubocop/pull/8295): Add new `Style/ArrayCoercion` cop. ([@fatkodima][])
* [#8293](https://github.com/rubocop-hq/rubocop/pull/8293): Add new `Lint/DuplicateElsifCondition` cop. ([@fatkodima][])
Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1397,6 +1397,7 @@ Lint/DisjunctiveAssignmentInConstructor:
Enabled: true
Safe: false
VersionAdded: '0.62'
VersionChanged: '0.88'

Lint/DuplicateCaseCondition:
Description: 'Do not repeat values in case conditionals.'
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -539,9 +539,9 @@ OpenSSL::Digest.digest('SHA256', 'foo')

| Enabled
| No
| No
| Yes (Unsafe)
| 0.62
| -
| 0.88
|===

This cop checks constructors for disjunctive assignments that should
Expand Down
10 changes: 8 additions & 2 deletions lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb
Expand Up @@ -22,7 +22,9 @@ module Lint
# def initialize
# @x = 1
# end
class DisjunctiveAssignmentInConstructor < Cop
class DisjunctiveAssignmentInConstructor < Base
extend AutoCorrector

MSG = 'Unnecessary disjunctive assignment. Use plain assignment.'

def on_def(node)
Expand Down Expand Up @@ -73,7 +75,11 @@ def check_body_lines(lines)
# @param [Node] node a disjunctive assignment
def check_disjunctive_assignment(node)
lhs = node.child_nodes.first
add_offense(node, location: :operator) if lhs.ivasgn_type?
return unless lhs.ivasgn_type?

add_offense(node.loc.operator) do |corrector|
corrector.replace(node.loc.operator, '=')
end
end
end
end
Expand Down
Expand Up @@ -43,7 +43,7 @@ def initialize
end

context 'LHS is ivar' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class Banana
def initialize
Expand All @@ -52,10 +52,18 @@ def initialize
end
end
RUBY

expect_correction(<<~RUBY)
class Banana
def initialize
@delicious = true
end
end
RUBY
end

context 'constructor calls super after assignment' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class Banana
def initialize
Expand All @@ -65,6 +73,15 @@ def initialize
end
end
RUBY

expect_correction(<<~RUBY)
class Banana
def initialize
@delicious = true
super
end
end
RUBY
end
end

Expand Down

0 comments on commit 1a5d073

Please sign in to comment.