baobao_codegen_typescript/
naming.rs1use baobao_codegen::language::NamingConvention;
4use baobao_core::{to_camel_case, to_kebab_case, to_pascal_case};
5
6fn escape_ts_reserved(name: &str) -> String {
7 format!("_{}", name)
8}
9
10pub const TS_NAMING: NamingConvention = NamingConvention {
12 command_to_type: to_pascal_case,
14 command_to_file: to_kebab_case,
16 field_to_name: to_camel_case,
18 reserved_words: &[
19 "break",
21 "case",
22 "catch",
23 "class",
24 "const",
25 "continue",
26 "debugger",
27 "default",
28 "delete",
29 "do",
30 "else",
31 "enum",
32 "export",
33 "extends",
34 "false",
35 "finally",
36 "for",
37 "function",
38 "if",
39 "import",
40 "in",
41 "instanceof",
42 "let",
43 "new",
44 "null",
45 "return",
46 "super",
47 "switch",
48 "this",
49 "throw",
50 "true",
51 "try",
52 "typeof",
53 "var",
54 "void",
55 "while",
56 "with",
57 "yield",
58 "any",
60 "as",
61 "async",
62 "await",
63 "boolean",
64 "constructor",
65 "declare",
66 "get",
67 "implements",
68 "interface",
69 "module",
70 "namespace",
71 "never",
72 "number",
73 "object",
74 "package",
75 "private",
76 "protected",
77 "public",
78 "readonly",
79 "require",
80 "set",
81 "static",
82 "string",
83 "symbol",
84 "type",
85 "undefined",
86 "unknown",
87 ],
88 escape_reserved: escape_ts_reserved,
89};
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn test_ts_naming_type() {
97 assert_eq!(TS_NAMING.type_name("hello-world"), "HelloWorld");
98 assert_eq!(TS_NAMING.type_name("get_user"), "GetUser");
99 }
100
101 #[test]
102 fn test_ts_naming_file() {
103 assert_eq!(TS_NAMING.file_name("HelloWorld"), "hello-world");
104 assert_eq!(TS_NAMING.file_name("GetUser"), "get-user");
105 }
106
107 #[test]
108 fn test_ts_naming_field() {
109 assert_eq!(TS_NAMING.field_name("user_name"), "userName");
110 assert_eq!(TS_NAMING.field_name("user-id"), "userId");
111 }
112
113 #[test]
114 fn test_ts_reserved_words() {
115 assert!(TS_NAMING.is_reserved("class"));
116 assert!(TS_NAMING.is_reserved("async"));
117 assert!(TS_NAMING.is_reserved("interface"));
118 assert!(!TS_NAMING.is_reserved("hello"));
119 }
120
121 #[test]
122 fn test_ts_escape_reserved() {
123 assert_eq!(TS_NAMING.safe_name("class"), "_class");
124 assert_eq!(TS_NAMING.safe_name("hello"), "hello");
125 }
126}