feat(printer): render `require` statements

This commit is contained in:
Aire-One 2023-05-21 20:17:37 +02:00
parent e076a08572
commit 4d81aaef56
2 changed files with 11 additions and 1 deletions

View File

@ -161,7 +161,7 @@ ast.in_order_visitor(module_ast, function(node: Node)
module_dependencies.visit(node, module_ast, {
modules = {
Screen = {
name = "screen",
name = "Screen",
module_path = "awful.screen",
token = "module",
},

View File

@ -28,6 +28,14 @@ local function render_code(code: string, indent_level: integer): string
return generated
end
local function render_require(dependencies: { string : string }): string
local generated = ""
for dependency, path in pairs(dependencies) do
generated = generated .. string.format("local %s = require(\"%s\")\n", dependency, path)
end
return generated:sub(1, -2) -- remove the last newline
end
local record Node_Printer_Function
on_node: function(node: Node, indent_level: integer): string, integer
before_node: nil | function(node: Node, indent_level: integer): string, integer
@ -45,9 +53,11 @@ local node_printer <total>: { Node.Token : Node_Printer_Function } = {
string.format(
[[
-- This file was auto-generated.
%s
local record %s
]],
render_require(node.dependencies),
node.name),
indent_level), indent_level + 1
end,