class RuboCop::Cop::Rouge::NoBuildingAlternationPatternInRegexp

Checks for the use of β€˜.join(’|β€˜)` or `Regexp.union` inside interpolated regular expressions.

Building alternation patterns by joining arrays or using β€˜Regexp.union` inside a regexp harms performance β€” the regex engine must backtrack through every alternative on each match attempt. It also risks quoting bugs when values contain regex metacharacters.

Prefer a β€˜Set` lookup with a simple regex pattern instead.

@example

# bad β€” joins an array into a huge alternation regex
KEYWORDS = %w[if else while ...]
rule %r/\b(#{KEYWORDS.join('|')})\b/, Keyword

# bad β€” Regexp.union inside a regex has the same problem
rule %r/#{Regexp.union(keywords)}/

# good β€” simple regex + Set lookup
KEYWORDS = Set.new(%w[if else while ...])
rule %r/\w+/ do |m|
  if KEYWORDS.include?(m[0])
    token Keyword
  else
    token Name
  end
end