Skip to content

Commit

Permalink
Merge pull request #12121 from chulkilee/fix-warning
Browse files Browse the repository at this point in the history
Fix instance variable not initialized warning
  • Loading branch information
koic committed Aug 17, 2023
2 parents 909564c + f8f5875 commit 2cb9dea
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions lib/rubocop/cop/utils/regexp_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,40 @@ module Utils
# Helper to abstract complexity of building range pairs
# with octal escape reconstruction (needed for regexp_parser < 2.7).
class RegexpRanges
attr_reader :compound_token, :root
attr_reader :root

def initialize(root)
@root = root
@compound_token = []
@pairs = []
@populated = false
end

def compound_token
populate_all unless @populated

@compound_token
end

def pairs
unless @pairs
@pairs = []
populate(root)
end
populate_all unless @populated

@pairs
end

private

def populate_all
populate(@root)

# If either bound is a compound the first one is an escape
# and that's all we need to work with.
# If there are any cops that wanted to operate on the compound
# expression we could wrap it with a facade class.
@pairs.map { |pair| pair.map(&:first) }
end
@pairs.map! { |pair| pair.map(&:first) }

private
@populated = true
end

def populate(expr)
expressions = expr.expressions.to_a
Expand All @@ -35,15 +48,15 @@ def populate(expr)
current = expressions.shift

if escaped_octal?(current)
compound_token << current
compound_token.concat(pop_octal_digits(expressions))
@compound_token << current
@compound_token.concat(pop_octal_digits(expressions))
# If we have all the digits we can discard.
end

next unless current.type == :set

process_set(expressions, current)
compound_token.clear
@compound_token.clear
end
end

Expand All @@ -64,8 +77,8 @@ def process_set(expressions, current)

def compose_range(expressions, current)
range_start, range_end = current.expressions
range_start = if compound_token.size.between?(1, 2) && octal_digit?(range_start.text)
compound_token.dup << range_start
range_start = if @compound_token.size.between?(1, 2) && octal_digit?(range_start.text)
@compound_token.dup << range_start
else
[range_start]
end
Expand Down

0 comments on commit 2cb9dea

Please sign in to comment.