Skip to content

Commit

Permalink
Inline ParserDiagnostic module
Browse files Browse the repository at this point in the history
Follow #8395 (comment).

This PR does a refactoring to inline `ParserDiagnostic` module.
It seems that it is simpler and easier to maintain if it is implemented
by each cop rather than mix-in the module.

I don't think `ParserDiagnostic` module is used outside of RuboCop core,
at least not in the RuboCop HQ products.
On the other hand, I added it to the changelog just in case for 3rd party gems.
  • Loading branch information
koic authored and marcandre committed Aug 3, 2020
1 parent d17ed26 commit 8842e13
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -50,6 +50,7 @@
* [#8276](https://github.com/rubocop-hq/rubocop/issues/8276): Cop `Metrics/CyclomaticComplexity` not longer counts `&.` when repeated on the same variable. ([@marcandre][])
* [#8204](https://github.com/rubocop-hq/rubocop/pull/8204): **(Breaking)** Cop `Metrics/PerceivedComplexity` now counts `else` in `case` statements, `&.`, `||=`, `&&=` and blocks known to iterate. Default bumped from 7 to 8. Consider using `rubocop -a --disable-uncorrectable` to ease transition. ([@marcandre][])
* [#8416](https://github.com/rubocop-hq/rubocop/issues/8416): Cop `Lint/InterpolationCheck` marked as unsafe. ([@marcandre][])
* [#8442](https://github.com/rubocop-hq/rubocop/pull/8442): Remove `RuboCop::Cop::ParserDiagnostic` mixin module. ([@koic][])

## 0.88.0 (2020-07-13)

Expand Down
1 change: 0 additions & 1 deletion lib/rubocop.rb
Expand Up @@ -95,7 +95,6 @@
require_relative 'rubocop/cop/mixin/on_normal_if_unless'
require_relative 'rubocop/cop/mixin/ordered_gem_node'
require_relative 'rubocop/cop/mixin/parentheses'
require_relative 'rubocop/cop/mixin/parser_diagnostic'
require_relative 'rubocop/cop/mixin/percent_array'
require_relative 'rubocop/cop/mixin/percent_literal'
require_relative 'rubocop/cop/mixin/preceding_following_alignment'
Expand Down
22 changes: 14 additions & 8 deletions lib/rubocop/cop/lint/ambiguous_operator.rb
Expand Up @@ -21,7 +21,6 @@ module Lint
# # With parentheses, there's no ambiguity.
# do_something(*some_array)
class AmbiguousOperator < Base
include ParserDiagnostic
extend AutoCorrector

AMBIGUITIES = {
Expand All @@ -39,16 +38,23 @@ class AmbiguousOperator < Base
'a whitespace to the right of the `%<operator>s` if it ' \
'should be a %<possible>s.'

private
def on_new_investigation
processed_source.diagnostics.each do |diagnostic|
next unless diagnostic.reason == :ambiguous_prefix

def autocorrect(corrector, node)
add_parentheses(node, corrector)
end
offense_node = find_offense_node_by(diagnostic)
message = message(diagnostic)

def relevant_diagnostic?(diagnostic)
diagnostic.reason == :ambiguous_prefix
add_offense(
diagnostic.location, message: message, severity: diagnostic.level
) do |corrector|
add_parentheses(offense_node, corrector)
end
end
end

private

def find_offense_node_by(diagnostic)
ast = processed_source.ast
ast.each_node(:splat, :block_pass, :kwsplat) do |node|
Expand All @@ -67,7 +73,7 @@ def find_offense_node_by(diagnostic)
end
end

def alternative_message(diagnostic)
def message(diagnostic)
operator = diagnostic.location.source
hash = AMBIGUITIES[operator]
format(MSG_FORMAT, hash)
Expand Down
21 changes: 10 additions & 11 deletions lib/rubocop/cop/lint/ambiguous_regexp_literal.rb
Expand Up @@ -22,34 +22,33 @@ module Lint
# # With parentheses, there's no ambiguity.
# do_something(/pattern/i)
class AmbiguousRegexpLiteral < Base
include ParserDiagnostic
extend AutoCorrector

MSG = 'Ambiguous regexp literal. Parenthesize the method arguments ' \
"if it's surely a regexp literal, or add a whitespace to the " \
'right of the `/` if it should be a division.'

private
def on_new_investigation
processed_source.diagnostics.each do |diagnostic|
next unless diagnostic.reason == :ambiguous_literal

def autocorrect(corrector, node)
add_parentheses(node, corrector)
end
offense_node = find_offense_node_by(diagnostic)

def relevant_diagnostic?(diagnostic)
diagnostic.reason == :ambiguous_literal
add_offense(diagnostic.location, severity: diagnostic.level) do |corrector|
add_parentheses(offense_node, corrector)
end
end
end

private

def find_offense_node_by(diagnostic)
node = processed_source.ast.each_node(:regexp).find do |regexp_node|
regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
end

node.parent
end

def alternative_message(_diagnostic)
MSG
end
end
end
end
Expand Down
19 changes: 5 additions & 14 deletions lib/rubocop/cop/lint/useless_else_without_rescue.rb
Expand Up @@ -30,23 +30,14 @@ module Lint
# do_something_else
# end
class UselessElseWithoutRescue < Base
include ParserDiagnostic

MSG = '`else` without `rescue` is useless.'

private

def relevant_diagnostic?(diagnostic)
diagnostic.reason == :useless_else
end

def find_offense_node_by(diagnostic)
# TODO: When implementing auto-correction, this method should return
# an offense node passed as first argument of `add_offense` method.
end
def on_new_investigation
processed_source.diagnostics.each do |diagnostic|
next unless diagnostic.reason == :useless_else

def alternative_message(_diagnostic)
MSG
add_offense(diagnostic.location, severity: diagnostic.level)
end
end
end
end
Expand Down
42 changes: 0 additions & 42 deletions lib/rubocop/cop/mixin/parser_diagnostic.rb

This file was deleted.

0 comments on commit 8842e13

Please sign in to comment.