Skip to content

Commit

Permalink
Merge pull request madonoharu#3 from Pistonight/quote
Browse files Browse the repository at this point in the history
update is_js_ident to cover most cases
  • Loading branch information
siefkenj authored Jan 13, 2024
2 parents 0c703ed + 2b694ab commit 3e7d11f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 57 additions & 0 deletions tests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,60 @@ fn test_rename_all() {
}
);
}

#[test]
fn test_quote_non_identifiers() {
#[derive(Tsify)]
struct NonIdentifierRenameStruct {
#[serde(rename = "1")]
x: i32,
#[serde(rename = "1x")]
y: i32,
#[serde(rename = "-")]
z: i32,
#[serde(rename = " ")]
w: i32,
#[serde(rename = "#")]
q: i32,
#[serde(rename = "should_not_quote")]
p: i32,
#[serde(rename = "should$not$quote")]
r: i32,
}

assert_eq!(
NonIdentifierRenameStruct::DECL,
indoc! {"
export interface NonIdentifierRenameStruct {
\"1\": number;
\"1x\": number;
\"-\": number;
\" \": number;
\"#\": number;
should_not_quote: number;
should$not$quote: number;
}"
}
);

#[derive(Tsify)]
enum NonIdentifierRenameEnum {
#[serde(rename = "hello-world")]
A(bool),
#[serde(rename = "hel#&*world")]
B(i64),
#[serde(rename = "hello world")]
C(String),
#[serde(rename = "")]
D(i32),
#[serde(rename = "should_not_quote")]
E(String),
}

let expected = indoc! {r#"
export type NonIdentifierRenameEnum = { "hello-world": boolean } | { "hel#&*world": number } | { "hello world": string } | { "": number } | { should_not_quote: string };"#

};

assert_eq!(NonIdentifierRenameEnum::DECL, expected);
}
2 changes: 1 addition & 1 deletion tsify-macros/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ fn parse_len(expr: &syn::Expr) -> Option<usize> {
}

fn is_js_ident(string: &str) -> bool {
!string.contains('-')
!string.is_empty() && !string.starts_with(|c: char| c.is_ascii_digit()) && !string.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '$')
}

impl TsTypeElement {
Expand Down

0 comments on commit 3e7d11f

Please sign in to comment.