Skip to content

Commit

Permalink
hashbang, if exists and if wanted, stays on line 1
Browse files Browse the repository at this point in the history
  • Loading branch information
JLPLabs LLC committed Jan 1, 2024
1 parent cecb87d commit 024ffc8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions spec/cli/gen_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local util = require("spec.util")

local input_file = [[
#!/usr/bin/env lua
global type1 = 2
global type type_g = record
Expand Down Expand Up @@ -37,6 +38,7 @@ local c = 100
]]

local output_file = [[
type1 = 2
type_g = {}
Expand Down Expand Up @@ -195,6 +197,16 @@ describe("tl gen", function()
end)
end)

it("*equality test* to confirm 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)
assert.equal(script_with_hashbang, util.read_file(lua_name))
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")
Expand Down
5 changes: 4 additions & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4356,7 +4356,10 @@ function tl.pretty_print_ast(ast, gen_target, mode)
end

local code
if opts.preserve_newlines then
if opts.preserve_hashbang and opts.preserve_newlines then
code = { y = out.y, h = 0 }
add_child(code, out)
elseif opts.preserve_newlines then
code = { y = 1, h = 0 }
add_child(code, out)
else
Expand Down
5 changes: 4 additions & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -4356,7 +4356,10 @@ function tl.pretty_print_ast(ast: Node, gen_target: TargetMode, mode: boolean |
end

local code: Output
if opts.preserve_newlines then
if opts.preserve_hashbang and opts.preserve_newlines then
code = { y = out.y, h = 0}
add_child(code, out)
elseif opts.preserve_newlines then
code = { y = 1, h = 0 }
add_child(code, out)
else
Expand Down

0 comments on commit 024ffc8

Please sign in to comment.