diff options
author | schneems <[email protected]> | 2021-11-07 20:30:50 -0600 |
---|---|---|
committer | Yusuke Endoh <[email protected]> | 2021-12-02 15:55:42 +0900 |
commit | 3685b5af95fc31b99b34a5a4f75bdc7c0ba622f4 (patch) | |
tree | 689f51a4dc665b8a33c055b2df24975dca598384 | |
parent | 3f74eaa7a83e42b31c219a534ec5330e511d2921 (diff) |
Only iterate Lexer heredoc arrays
The last element in the `@buf` may be either an array or an `Elem`. In the case it is an `Elem` we iterate over every element, when we do not need to. This check guards that case by ensuring that we only iterate over an array of elements.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5093
-rw-r--r-- | ext/ripper/lib/ripper/lexer.rb | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/ext/ripper/lib/ripper/lexer.rb b/ext/ripper/lib/ripper/lexer.rb index 92ee4ef7df..a2d4d655b5 100644 --- a/ext/ripper/lib/ripper/lexer.rb +++ b/ext/ripper/lib/ripper/lexer.rb @@ -166,17 +166,19 @@ class Ripper def on_heredoc_dedent(v, w) ignored_sp = [] heredoc = @buf.last - heredoc.each_with_index do |e, i| - if Elem === e and e.event == :on_tstring_content and e.pos[1].zero? - tok = e.tok.dup if w > 0 and /\A\s/ =~ e.tok - if (n = dedent_string(e.tok, w)) > 0 - if e.tok.empty? - e.tok = tok[0, n] - e.event = :on_ignored_sp - next + if Array === heredoc + heredoc.each_with_index do |e, i| + if Elem === e and e.event == :on_tstring_content and e.pos[1].zero? + tok = e.tok.dup if w > 0 and /\A\s/ =~ e.tok + if (n = dedent_string(e.tok, w)) > 0 + if e.tok.empty? + e.tok = tok[0, n] + e.event = :on_ignored_sp + next + end + ignored_sp << [i, Elem.new(e.pos.dup, :on_ignored_sp, tok[0, n], e.state)] + e.pos[1] += n end - ignored_sp << [i, Elem.new(e.pos.dup, :on_ignored_sp, tok[0, n], e.state)] - e.pos[1] += n end end end |