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

[Fix #9175] Fix status for offenses that are not correctable. #9190

Merged
merged 1 commit into from
Dec 8, 2020
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
32 changes: 17 additions & 15 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,6 @@ def apply_correction(corrector)
@current_corrector&.merge!(corrector) if corrector
end

def correction_strategy
return :unsupported unless correctable?
return :uncorrected unless autocorrect?

:attempt_correction
end

### Reserved for Commissioner:

def current_offense_locations
Expand Down Expand Up @@ -341,33 +334,42 @@ def reset_investigation

# @return [Symbol, Corrector] offense status
def correct(range)
status = correction_strategy

if block_given?
corrector = Corrector.new(self)
yield corrector
if !corrector.empty? && !self.class.support_autocorrect?
if corrector.empty?
corrector = nil
elsif !self.class.support_autocorrect?
raise "The Cop #{name} must `extend AutoCorrector` to be able to autocorrect"
end
end

status = attempt_correction(range, corrector) if status == :attempt_correction
[use_corrector(range, corrector), corrector]
end

[status, corrector]
# @return [Symbol] offense status
def use_corrector(range, corrector)
if autocorrect?
attempt_correction(range, corrector)
elsif corrector
:uncorrected
else
:unsupported
end
end

# @return [Symbol] offense status
def attempt_correction(range, corrector)
if corrector && !corrector.empty?
if corrector
status = :corrected
elsif disable_uncorrectable?
corrector = disable_uncorrectable(range)
status = :corrected_with_todo
else
return :uncorrected
return :unsupported
end

apply_correction(corrector) if corrector
apply_correction(corrector)
status
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call(corrector)
def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
@v0_argument = node_or_range
range = find_location(node_or_range, location)
if block.nil? && !autocorrect?
if block.nil? && !support_autocorrect?
super(range, message: message, severity: severity)
else
super(range, message: message, severity: severity) do |corrector|
Expand Down Expand Up @@ -136,7 +136,7 @@ def emulate_v0_callsequence(corrector)
end

def correction_lambda
return unless correction_strategy == :attempt_correction && support_autocorrect?
return unless support_autocorrect?

dedup_on_node(@v0_argument) do
autocorrect(@v0_argument)
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cli/cli_autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ def self.some_method(foo, bar: 1)
C: 2: 34: Style/Semicolon: Do not use semicolons to terminate expressions.
W: 3: 27: Lint/UnusedMethodArgument: Unused method argument - bar.

1 file inspected, 3 offenses detected, 3 more offenses can be corrected with `rubocop -A`
1 file inspected, 3 offenses detected
RESULT
end

Expand Down