Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code terminated check with heredoc and backtick #390

Merged
merged 7 commits into from
Oct 18, 2022
Merged
22 changes: 16 additions & 6 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def self.ripper_lex_without_warning(code, context: nil)
end
end
else
lexer.parse.reject { |it| it.pos.first == 0 }
lexer.parse.reject { |it| it.pos.first == 0 }.sort_by(&:pos)
end
end
ensure
Expand Down Expand Up @@ -706,6 +706,7 @@ def check_string_literal(tokens)
i = 0
start_token = []
end_type = []
pending_heredocs = []
while i < tokens.size
t = tokens[i]
case t.event
Expand All @@ -729,18 +730,27 @@ def check_string_literal(tokens)
end
end
when :on_backtick
start_token << t
end_type << :on_tstring_end
if t.state.allbits?(Ripper::EXPR_BEG)
start_token << t
end_type << :on_tstring_end
end
when :on_qwords_beg, :on_words_beg, :on_qsymbols_beg, :on_symbols_beg
start_token << t
end_type << :on_tstring_end
when :on_heredoc_beg
start_token << t
end_type << :on_heredoc_end
pending_heredocs << t
end

if pending_heredocs.any? && t.tok.include?("\n")
pending_heredocs.reverse_each do |t|
start_token << t
end_type << :on_heredoc_end
end
pending_heredocs = []
end
i += 1
end
start_token.last.nil? ? nil : start_token.last
pending_heredocs.first || start_token.last
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests seem to pass without pending_heredocs.first ||. Would you mind adding another test to cover this scenario?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these test cases.

['<<A;<<B', "<<A;<<B\n", "%W[\#{<<A;<<B", "%W[\#{<<A;<<B\n"]`

process_string_literal of these code should all be <<A.
without pending_heredocs.first ||, it will be

[nil, '<<A', '%W[', '<<A']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thx

end

def process_literal_type(tokens = @tokens)
Expand Down
42 changes: 42 additions & 0 deletions test/irb/test_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,40 @@ def test_endless_range_at_end_of_line
assert_dynamic_prompt(lines, expected_prompt_list)
end

def test_heredoc_with_embexpr
input_with_prompt = [
PromptRow.new('001:0:":* ', %q(<<A+%W[#{<<B)),
PromptRow.new('002:0:":* ', %q(#{<<C+%W[)),
PromptRow.new('003:0:":* ', %q()),
PromptRow.new('004:0:":* ', %q(C)),
PromptRow.new('005:0:]:* ', %q()),
PromptRow.new('006:0:":* ', %q(]})),
PromptRow.new('007:0:":* ', %q(})),
PromptRow.new('008:0:":* ', %q(A)),
PromptRow.new('009:0:]:* ', %q(B)),
PromptRow.new('010:0:]:* ', %q(})),
PromptRow.new('011:0: :> ', %q(])),
PromptRow.new('012:0: :* ', %q()),
]

lines = input_with_prompt.map(&:content)
expected_prompt_list = input_with_prompt.map(&:prompt)
assert_dynamic_prompt(lines, expected_prompt_list)
end

def test_backtick_method
input_with_prompt = [
PromptRow.new('001:0: :> ', %q(self.`(arg))),
PromptRow.new('002:0: :* ', %q()),
PromptRow.new('003:0: :> ', %q(def `(); end)),
PromptRow.new('004:0: :* ', %q()),
]

lines = input_with_prompt.map(&:content)
expected_prompt_list = input_with_prompt.map(&:prompt)
assert_dynamic_prompt(lines, expected_prompt_list)
end

def test_incomplete_coding_magic_comment
input_with_correct_indents = [
Row.new(%q(#coding:u), nil, 0),
Expand Down Expand Up @@ -632,5 +666,13 @@ def test_unterminated_code
assert_empty(error_tokens, 'Error tokens must be ignored if there is corresponding non-error token')
end
end

def test_unterminated_heredoc_string_literal
['<<A;<<B', "<<A;<<B\n", "%W[\#{<<A;<<B", "%W[\#{<<A;<<B\n"].each do |code|
tokens = RubyLex.ripper_lex_without_warning(code)
string_literal = RubyLex.new.check_string_literal(tokens)
assert_equal('<<A', string_literal&.tok)
end
end
end
end