The translator now removes type annotations from local variable declarations.
Type annotations are not mandatory for local variable declarations. Therefore, we ensure if
`decl.type` is truthy. To remove the type annotation, we first replace the colon with space.
Any adjacent comment to the right is retained. After which, we remove the type name.
@@ -45,6 +45,7 @@ function translator.translate(input, prog_ast)
end
end
elseif node._tag == "ast.Toplevel.Func" then
+ -- Remove type annotations from function parameters.
for _, arg_decl in ipairs(node.value.arg_decls) do
-- Type annotations are mandatory for function parameters.
-- Remove the colon but retain any adjacent comment to the right.
@@ -52,6 +53,20 @@ function translator.translate(input, prog_ast)
-- Remove the type annotation but exclude the next token.
instance:add_whitespace(input, arg_decl.type.loc.pos, arg_decl.end_loc.pos - 1)
end
+
+ -- Remove type annotations from local declarations.
+ for _, statement in ipairs(node.value.body.stats) do
+ if statement._tag == "ast.Stat.Decl" then
+ for _, decl in ipairs(statement.decls) do
+ if decl.type then
+ -- Remove the colon but retain any adjacent comment to the right.
+ instance:add_whitespace(input, decl.col_loc.pos, decl.col_loc.pos)
+ -- Remove the type annotation but exclude the next token.
+ instance:add_whitespace(input, decl.type.loc.pos, decl.end_loc.pos - 1)
+ end
+ end
+ end
+ end
end
end
-- Whatever characters that were not included in the partials should be added.
