Skip to content

Commit

Permalink
+ builder.rb: extract named captures only from static regexes. (#979)
Browse files Browse the repository at this point in the history
This commit tracks upstream commit ruby/ruby@a607d62.
  • Loading branch information
iliabylich committed Dec 29, 2023
1 parent ea3108e commit fd23193
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,10 @@ def static_regexp(parts, options)

def static_regexp_node(node)
if node.type == :regexp
if @parser.version >= 33 && node.children[0..-2].any? { |child| child.type != :str }
return nil
end

parts, options = node.children[0..-2], node.children[-1]
static_regexp(parts, options)
end
Expand Down
19 changes: 19 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3788,6 +3788,25 @@ def test_lvar_injecting_match
%q{ ~~ selector (match_with_lvasgn)
|~~~~~~~~~~~~~~~~~~~~~~~~ expression (match_with_lvasgn)},
SINCE_1_9)

assert_parses(
s(:begin,
s(:match_with_lvasgn,
s(:regexp,
s(:str, "(?<a>a)"),
s(:regopt)),
s(:str, "a")),
s(:send,
s(:regexp,
s(:begin),
s(:str, "(?<b>b)"),
s(:regopt)), :=~,
s(:str, "b")),
s(:lvar, :a),
s(:send, nil, :b)),
%q{/(?<a>a)/ =~ 'a'; /#{}(?<b>b)/ =~ 'b'; a; b},
%q{},
SINCE_3_3)
end

def test_non_lvar_injecting_match
Expand Down

0 comments on commit fd23193

Please sign in to comment.