Skip to content

Commit 92a9fc6

Browse files
committed
docs: better type annotations typescript/lua codegen
1 parent 49358aa commit 92a9fc6

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

lua/refactoring/code_generation/langs/lua.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local code_utils = require("refactoring.code_generation.utils")
22

3+
---@param opts refactor.FuncParams
4+
---@return string
35
local function lua_function(opts)
46
return ([[
57
local function %s(%s)

lua/refactoring/code_generation/langs/typescriptreact.lua

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
local code_utils = require("refactoring.code_generation.utils")
22
local ts = require("refactoring.code_generation.langs.typescript")
33

4+
---@param args string[]
5+
---@param arg_types table<string, string>
6+
---@return string[]
47
local function build_args(args, arg_types)
5-
local final_args = {}
8+
local final_args = {} ---@type string[]
69
for i, arg in pairs(args) do
710
if arg_types[arg] ~= code_utils.default_func_param_type() then
811
final_args[i] = arg .. ": " .. arg_types[arg]
@@ -15,15 +18,19 @@ end
1518

1619
---@param opts refactor.code_gen.function.Opts
1720
local function tsx_function(opts)
18-
if opts.region_type == "jsx_element" then
19-
local args
20-
if opts.args_types ~= nil then
21-
args = build_args(opts.args, opts.args_types)
22-
else
23-
args = opts.args
24-
end
21+
if opts.region_type ~= "jsx_element" then
22+
return ts["function"](opts)
23+
end
24+
25+
local args ---@type string[]
26+
if opts.args_types ~= nil then
27+
args = build_args(opts.args, opts.args_types)
28+
else
29+
args = opts.args
30+
end
31+
assert(args)
2532

26-
return ([[
33+
return ([[
2734
%sfunction %s({%s}) {
2835
return (
2936
<>
@@ -33,15 +40,12 @@ return (
3340
%s}
3441
3542
]]):format(
36-
opts.func_header,
37-
opts.name,
38-
table.concat(args, ", "),
39-
code_utils.stringify_code(opts.body),
40-
opts.func_header
41-
)
42-
else
43-
return ts["function"](opts)
44-
end
43+
opts.func_header,
44+
opts.name,
45+
table.concat(args, ", "),
46+
code_utils.stringify_code(opts.body),
47+
opts.func_header
48+
)
4549
end
4650

4751
---@param opts refactor.code_gen.call_function.Opts

lua/refactoring/refactor/106.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ end
8181
---@param args string[]
8282
---@return table<string, string|nil>
8383
local function get_function_param_types(refactor, args)
84-
local args_types = {}
84+
local args_types = {} ---@type table<string, string>
8585

8686
local local_types = refactor.ts:get_local_types(refactor.scope)
8787

@@ -166,6 +166,7 @@ end
166166

167167
---@class refactor.FuncParams
168168
---@field func_header? string
169+
---@field args_types? table<string, string>
169170
---@field contains_jsx? boolean
170171
---@field class_name? string
171172
---@field visibility? string

0 commit comments

Comments
 (0)