Skip to content

Commit

Permalink
fix: syntax error when return; is not last statement of block (#893)
Browse files Browse the repository at this point in the history
Fixes #495.
  • Loading branch information
catwell authored Jan 3, 2025
1 parent 3bfa31b commit 2d73d3f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/lang/parser/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ describe("parser", function()
assert.same("return", result.ast[1].kind)
end)

it("does not accept statements after 'return;' (regression test for #495)", function ()
local result = tl.process_string([[
return;
print("")
]])
assert.same(1, #result.syntax_errors)
assert.same(result.syntax_errors[1].msg, "return must be the last statement of its block")

local result = tl.process_string([[
local function get_foo(): string
if true then
return "foo";
print("")
end
end
]])
assert.same(1, #result.syntax_errors)
assert.same(result.syntax_errors[1].msg, "return must be the last statement of its block")
end)

it("accepts semicolons in tables (regression test for #54)", function ()
local result = tl.process_string([[
local t = {
Expand Down
3 changes: 3 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -3708,6 +3708,9 @@ local function parse_return(ps: ParseState, i: integer): integer, Node
i = parse_list(ps, i, node.exps, stop_return_list, "sep", parse_expression)
if ps.tokens[i].kind == ";" then
i = i + 1
if ps.tokens[i].kind ~= "$EOF$" and not stop_statement_list[ps.tokens[i].kind] then
return fail(ps, i, "return must be the last statement of its block")
end
end
return i, node
end
Expand Down

0 comments on commit 2d73d3f

Please sign in to comment.