fix: Move variable definition to the end

This commit is contained in:
2025-09-20 02:33:17 +02:00
parent 326af7ea83
commit 21bc70cbcd
5 changed files with 25 additions and 7 deletions

View File

@@ -54,7 +54,7 @@ impl ToTokens for Struct {
tokens.extend(quote! {
fn generate_header() -> Option<String> {
let type_name = <Self as Typed>::type_name();
Some(format!("---@class {type_name}\nlocal {type_name}\n"))
Some(format!("---@class {type_name}\n"))
}
fn generate_members() -> Option<String> {
@@ -66,6 +66,11 @@ impl ToTokens for Struct {
Some(output)
}
fn generate_footer() -> Option<String> {
let type_name = <Self as Typed>::type_name();
Some(format!("local {type_name}\n"))
}
});
}
}
@@ -202,7 +207,7 @@ impl ToTokens for ExtTaggedEnum {
tokens.extend(quote! {
fn generate_header() -> Option<String> {
let type_name = <Self as Typed>::type_name();
Some(format!("---@class {type_name}\nlocal {type_name}\n"))
Some(format!("---@class {type_name}\n"))
}
fn generate_members() -> Option<String> {
@@ -219,6 +224,11 @@ impl ToTokens for ExtTaggedEnum {
Some(output)
}
fn generate_footer() -> Option<String> {
let type_name = <Self as Typed>::type_name();
Some(format!("local {type_name}\n"))
}
});
}
}

View File

@@ -12,6 +12,10 @@ pub trait Typed {
None
}
fn generate_footer() -> Option<String> {
None
}
fn generate_full() -> Option<String> {
let mut output = String::new();
@@ -24,6 +28,10 @@ pub trait Typed {
output += members;
}
if let Some(footer) = &Self::generate_footer() {
output += footer;
}
Some(output)
}
}

View File

@@ -17,9 +17,9 @@ pub struct A {
fn flatten() {
insta::assert_snapshot!(<A as Typed>::generate_full().unwrap(), @r"
---@class A
local A
---@field hello string
---@field world boolean
---@field cool integer
local A
");
}

View File

@@ -14,10 +14,10 @@ pub enum ActionType {
fn action_type() {
insta::assert_snapshot!(<ActionType as Typed>::generate_full().unwrap(), @r#"
---@class ActionType
local ActionType
---@field action
---| "broadcast"
---@field extras table<string, string>?
local ActionType
"#);
}
@@ -56,12 +56,12 @@ pub struct Action {
fn action() {
insta::assert_snapshot!(<Action as Typed>::generate_full().unwrap(), @r#"
---@class Action
local Action
---@field action
---| "broadcast"
---@field extras table<string, string>?
---@field label string
---@field clear boolean?
local Action
"#);
}
@@ -80,11 +80,11 @@ pub struct Notification {
fn notification() {
insta::assert_snapshot!(<Notification as Typed>::generate_full().unwrap(), @r"
---@class Notification
local Notification
---@field title string
---@field message string?
---@field tags string[]?
---@field priority Priority?
---@field actions Action[]?
local Notification
");
}

View File

@@ -21,7 +21,7 @@ fn rename() {
fn rename_nested() {
insta::assert_snapshot!(<Other as Typed>::generate_full().unwrap(), @r"
---@class Other
local Other
---@field rust Lua
local Other
");
}