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

Use RuboCop::AST::Token#regexp_dots? #10803

Merged
merged 1 commit into from Jul 11, 2022
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
1 change: 1 addition & 0 deletions changelog/change_require_rubocop_ast_1_9.md
@@ -0,0 +1 @@
* [#10803](https://github.com/rubocop/rubocop/pull/10803): Require RuboCop AST 1.9.1 or higher. ([@koic][])
17 changes: 10 additions & 7 deletions lib/rubocop/cop/style/semicolon.rb
Expand Up @@ -31,7 +31,6 @@ class Semicolon < Base
extend AutoCorrector

MSG = 'Do not use semicolons to terminate expressions.'
REGEXP_DOTS = %i[tDOT2 tDOT3].freeze

def self.autocorrect_incompatible_with
[Style::SingleLineMethods]
Expand All @@ -40,10 +39,6 @@ def self.autocorrect_incompatible_with
def on_new_investigation
return if processed_source.blank?

ast = processed_source.ast
@range_nodes = ast.range_type? ? [ast] : []
@range_nodes.concat(ast.each_descendant(:irange, :erange).to_a)

check_for_line_terminator_or_opener
end

Expand Down Expand Up @@ -95,7 +90,7 @@ def register_semicolon(line, column, after_expression, token_before_semicolon =
# Prevents becoming one range instance with subsequent line when endless range
# without parentheses.
# See: https://github.com/rubocop/rubocop/issues/10791
if REGEXP_DOTS.include?(token_before_semicolon&.type)
if token_before_semicolon&.regexp_dots?
range_node = find_range_node(token_before_semicolon)
corrector.wrap(range_node, '(', ')') if range_node
end
Expand All @@ -120,10 +115,18 @@ def find_semicolon_positions(line)
end

def find_range_node(token_before_semicolon)
@range_nodes.detect do |range_node|
range_nodes.detect do |range_node|
range_node.source_range.contains?(token_before_semicolon.pos)
end
end

def range_nodes
return @range_nodes if instance_variable_defined?(:@range_nodes)

ast = processed_source.ast
@range_nodes = ast.range_type? ? [ast] : []
@range_nodes.concat(ast.each_descendant(:irange, :erange).to_a)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rubocop.gemspec
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('rexml', '>= 3.2.5', '< 4.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.18.0', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.19.1', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0')

Expand Down