class RuboCop::Cop::Rouge::NoBuildingAlternationPatternInRegexp
end
end
token Name
else
token Keyword
if KEYWORDS.include?(m)
rule %r/w+/ do |m|
KEYWORDS = Set.new(%w[if else while …])
# good — simple regex + Set lookup
rule %r/#{Regexp.union(keywords)}/
# bad — Regexp.union inside a regex has the same problem
rule %r/b(#{KEYWORDS.join(‘|’)})b/, Keyword
KEYWORDS = %w[if else while …]
# bad — joins an array into a huge alternation regex
@example
Prefer a ‘Set` lookup with a simple regex pattern instead.
metacharacters.
attempt. It also risks quoting bugs when values contain regex
engine must backtrack through every alternative on each match
`Regexp.union` inside a regexp harms performance — the regex
Building alternation patterns by joining arrays or using
interpolated regular expressions.
Checks for the use of `.join(’|‘)` or `Regexp.union` inside
def find_offending_calls(begin_node)
def find_offending_calls(begin_node) begin_node.each_descendant(:send) do |send_node| next unless join_pipe_call?(send_node) || regexp_union_call?(send_node) add_offense(send_node) end end
def on_regexp(node)
def on_regexp(node) node.children.each do |child| next unless child.begin_type? find_offending_calls(child) end end