Skip to content

Commit

Permalink
fix: hashbang, if exists and wanted, stays on line 1
Browse files Browse the repository at this point in the history
Fixes: #729
Closes: #730

Co-authored-by: JLPLabs LLC <jlplabs@man.com>
  • Loading branch information
hishamhm and JLPLabs LLC committed Jan 5, 2024
1 parent 4c0c162 commit a9f2e9c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
62 changes: 40 additions & 22 deletions spec/cli/gen_spec.lua
Expand Up @@ -72,15 +72,31 @@ end
local c = 100
]]

local script_with_hashbang = [[
local hashbang_cases = {
[1] = {
with_hashbang = [[
#!/usr/bin/env lua
print("hello world")
]]
]],
without_hashbang = [[
local script_without_hashbang = [[
print("hello world")
]]
]],
},

[2] = {
with_hashbang = [[
#!/usr/bin/env lua
print("hello world")
]],
without_hashbang = [[
print("hello world")
]],
},
}

local function tl_to_lua(name)
return (name:gsub("%.tl$", ".lua"):gsub("^" .. util.os_tmp .. util.os_sep, ""))
Expand Down Expand Up @@ -195,25 +211,27 @@ describe("tl gen", function()
end)
end)

it("preserves hashbang with --keep-hashbang", function()
local name = util.write_tmp_file(finally, script_with_hashbang)
local pd = io.popen(util.tl_cmd("gen", "--keep-hashbang", name), "r")
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
util.assert_line_by_line(script_with_hashbang, util.read_file(lua_name))
end)
for i, case in ipairs(hashbang_cases) do
it("[" .. i .. "] preserves hashbang with --keep-hashbang", function()
local name = util.write_tmp_file(finally, case.with_hashbang)
local pd = io.popen(util.tl_cmd("gen", "--keep-hashbang", name), "r")
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
assert.equal(case.with_hashbang, util.read_file(lua_name))
end)

it("drops hashbang when not using --keep-hashbang", function()
local name = util.write_tmp_file(finally, script_with_hashbang)
local pd = io.popen(util.tl_cmd("gen", name), "r")
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
util.assert_line_by_line(script_without_hashbang, util.read_file(lua_name))
end)
it("[" .. i .. "] drops hashbang when not using --keep-hashbang", function()
local name = util.write_tmp_file(finally, case.with_hashbang)
local pd = io.popen(util.tl_cmd("gen", name), "r")
local output = pd:read("*a")
util.assert_popen_close(0, pd:close())
local lua_name = tl_to_lua(name)
assert.match("Wrote: " .. lua_name, output, 1, true)
util.assert_line_by_line(case.without_hashbang, util.read_file(lua_name))
end)
end

describe("with --gen-target=5.1", function()
it("targets generated code to Lua 5.1+", function()
Expand Down
2 changes: 1 addition & 1 deletion spec/lexer/hashbang_spec.lua
Expand Up @@ -12,7 +12,7 @@ describe("lexer", function()
it("skips hashbang at the beginning of a file", function()
local syntax_errors = {}
local tokens = tl.lex("#!/usr/bin/env lua\nlocal x = 1")
assert.same({"#!/usr/bin/env lua\n", "local", "x", "=", "1", "$EOF$"}, map(function(x) return x.tk end, tokens))
assert.same({"#!/usr/bin/env lua", "local", "x", "=", "1", "$EOF$"}, map(function(x) return x.tk end, tokens))

tl.parse_program(tokens, syntax_errors)
assert.same({}, syntax_errors)
Expand Down
7 changes: 5 additions & 2 deletions tl.lua
Expand Up @@ -605,7 +605,7 @@ do
if not i then
i = len + 1
end
end_token_here("hashbang")
end_token_prev("hashbang")
y = 2
x = 0
end
Expand Down Expand Up @@ -3854,9 +3854,12 @@ function tl.pretty_print_ast(ast, gen_target, mode)
visit_node.cbs = {
["statements"] = {
after = function(node, children)
local out = { y = node.y, h = 0 }
local out
if opts.preserve_hashbang and node.hashbang then
out = { y = 1, h = 0 }
table.insert(out, node.hashbang)
else
out = { y = node.y, h = 0 }
end
local space
for i, child in ipairs(children) do
Expand Down
7 changes: 5 additions & 2 deletions tl.tl
Expand Up @@ -605,7 +605,7 @@ do
if not i then
i = len + 1
end
end_token_here("hashbang")
end_token_prev("hashbang")
y = 2
x = 0
end
Expand Down Expand Up @@ -3854,9 +3854,12 @@ function tl.pretty_print_ast(ast: Node, gen_target: TargetMode, mode: boolean |
visit_node.cbs = {
["statements"] = {
after = function(node: Node, children: {Output}): Output
local out: Output = { y = node.y, h = 0 }
local out: Output
if opts.preserve_hashbang and node.hashbang then
out = { y = 1, h = 0 }
table.insert(out, node.hashbang)
else
out = { y = node.y, h = 0 }
end
local space: string
for i, child in ipairs(children) do
Expand Down

0 comments on commit a9f2e9c

Please sign in to comment.