Skip to content

Commit

Permalink
Merge pull request #10803 from koic/require_rubocop_ast_1_9
Browse files Browse the repository at this point in the history
Use `RuboCop::AST::Token#regexp_dots?`
  • Loading branch information
koic committed Jul 11, 2022
2 parents 87a0497 + 819a677 commit ac20d11
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
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

0 comments on commit ac20d11

Please sign in to comment.