|
1 | 1 | // Copyright 2018-2024 the Deno authors. MIT license.
|
2 | 2 |
|
3 |
| -use std::borrow::Cow; |
4 |
| -use std::collections::HashSet; |
5 |
| - |
| 3 | +use indexmap::IndexSet; |
| 4 | +use string_capacity::StringBuilder; |
6 | 5 | use wasm_dep_analyzer::ValueType;
|
7 | 6 |
|
8 | 7 | pub fn wasm_module_to_dts(
|
@@ -41,87 +40,105 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
|
41 | 40 | && deno_ast::swc::ast::Ident::verify_symbol(export_name).is_ok()
|
42 | 41 | }
|
43 | 42 |
|
44 |
| - let mut text = String::new(); |
45 |
| - let mut internal_names_count = 0; |
| 43 | + let is_valid_export_ident_per_export = wasm_deps |
| 44 | + .exports |
| 45 | + .iter() |
| 46 | + .map(|export| is_valid_ident(export.name)) |
| 47 | + .collect::<Vec<_>>(); |
| 48 | + let unique_import_modules = wasm_deps |
| 49 | + .imports |
| 50 | + .iter() |
| 51 | + .map(|import| import.module) |
| 52 | + .collect::<IndexSet<_>>(); |
46 | 53 |
|
47 |
| - let mut seen_modules = HashSet::with_capacity(wasm_deps.imports.len()); |
48 |
| - for import in &wasm_deps.imports { |
49 |
| - if seen_modules.insert(&import.module) { |
50 |
| - text.push_str(&format!("import \"{}\";\n", import.module)); |
| 54 | + StringBuilder::build(|builder| { |
| 55 | + for import_module in &unique_import_modules { |
| 56 | + builder.append("import \""); |
| 57 | + builder.append(import_module); |
| 58 | + builder.append("\";\n"); |
51 | 59 | }
|
52 |
| - } |
53 | 60 |
|
54 |
| - for export in &wasm_deps.exports { |
55 |
| - let has_valid_export_ident = is_valid_ident(export.name); |
56 |
| - let export_name = if has_valid_export_ident { |
57 |
| - Cow::Borrowed(export.name) |
58 |
| - } else { |
59 |
| - let export_name = |
60 |
| - format!("__deno_wasm_export_{}__", internal_names_count); |
61 |
| - internal_names_count += 1; |
62 |
| - Cow::Owned(export_name) |
63 |
| - }; |
64 |
| - if has_valid_export_ident { |
65 |
| - text.push_str("export "); |
66 |
| - } |
67 |
| - let mut add_var = |type_text: &str| { |
68 |
| - text.push_str("declare const "); |
69 |
| - text.push_str(&export_name); |
70 |
| - text.push_str(": "); |
71 |
| - text.push_str(type_text); |
72 |
| - text.push_str(";\n"); |
73 |
| - }; |
| 61 | + for (i, export) in wasm_deps.exports.iter().enumerate() { |
| 62 | + let has_valid_export_ident = is_valid_export_ident_per_export[i]; |
| 63 | + if has_valid_export_ident { |
| 64 | + builder.append("export "); |
| 65 | + } |
| 66 | + fn write_export_name<'a>( |
| 67 | + builder: &mut StringBuilder<'a>, |
| 68 | + export: &'a wasm_dep_analyzer::Export<'a>, |
| 69 | + has_valid_export_ident: bool, |
| 70 | + index: usize, |
| 71 | + ) { |
| 72 | + if has_valid_export_ident { |
| 73 | + builder.append(export.name); |
| 74 | + } else { |
| 75 | + builder.append("__deno_wasm_export_"); |
| 76 | + builder.append(index); |
| 77 | + builder.append("__"); |
| 78 | + } |
| 79 | + } |
| 80 | + let mut add_var = |type_text: &'static str| { |
| 81 | + builder.append("declare const "); |
| 82 | + write_export_name(builder, export, has_valid_export_ident, i); |
| 83 | + builder.append(": "); |
| 84 | + builder.append(type_text); |
| 85 | + builder.append(";\n"); |
| 86 | + }; |
74 | 87 |
|
75 |
| - match &export.export_type { |
76 |
| - wasm_dep_analyzer::ExportType::Function(function_signature) => { |
77 |
| - match function_signature { |
78 |
| - Ok(signature) => { |
79 |
| - text.push_str("declare function "); |
80 |
| - text.push_str(&export_name); |
81 |
| - text.push('('); |
82 |
| - for (i, param) in signature.params.iter().enumerate() { |
83 |
| - if i > 0 { |
84 |
| - text.push_str(", "); |
| 88 | + match &export.export_type { |
| 89 | + wasm_dep_analyzer::ExportType::Function(function_signature) => { |
| 90 | + match function_signature { |
| 91 | + Ok(signature) => { |
| 92 | + builder.append("declare function "); |
| 93 | + write_export_name(builder, export, has_valid_export_ident, i); |
| 94 | + builder.append('('); |
| 95 | + for (i, param) in signature.params.iter().enumerate() { |
| 96 | + if i > 0 { |
| 97 | + builder.append(", "); |
| 98 | + } |
| 99 | + builder.append("arg"); |
| 100 | + builder.append(i); |
| 101 | + builder.append(": "); |
| 102 | + builder |
| 103 | + .append(value_type_to_ts_type(*param, TypePosition::Input)); |
85 | 104 | }
|
86 |
| - text.push_str("arg"); |
87 |
| - text.push_str(i.to_string().as_str()); |
88 |
| - text.push_str(": "); |
89 |
| - text.push_str(value_type_to_ts_type(*param, TypePosition::Input)); |
| 105 | + builder.append("): "); |
| 106 | + builder.append( |
| 107 | + signature |
| 108 | + .returns |
| 109 | + .first() |
| 110 | + .map(|t| value_type_to_ts_type(*t, TypePosition::Output)) |
| 111 | + .unwrap_or("void"), |
| 112 | + ); |
| 113 | + builder.append(";\n"); |
90 | 114 | }
|
91 |
| - text.push_str("): "); |
92 |
| - text.push_str( |
93 |
| - signature |
94 |
| - .returns |
95 |
| - .first() |
96 |
| - .map(|t| value_type_to_ts_type(*t, TypePosition::Output)) |
97 |
| - .unwrap_or("void"), |
98 |
| - ); |
99 |
| - text.push_str(";\n"); |
| 115 | + Err(_) => add_var("unknown"), |
100 | 116 | }
|
101 |
| - Err(_) => add_var("unknown"), |
102 | 117 | }
|
| 118 | + wasm_dep_analyzer::ExportType::Table => add_var("WebAssembly.Table"), |
| 119 | + wasm_dep_analyzer::ExportType::Memory => add_var("WebAssembly.Memory"), |
| 120 | + wasm_dep_analyzer::ExportType::Global(global_type) => match global_type |
| 121 | + { |
| 122 | + Ok(global_type) => add_var(value_type_to_ts_type( |
| 123 | + global_type.value_type, |
| 124 | + TypePosition::Output, |
| 125 | + )), |
| 126 | + Err(_) => add_var("unknown"), |
| 127 | + }, |
| 128 | + wasm_dep_analyzer::ExportType::Tag |
| 129 | + | wasm_dep_analyzer::ExportType::Unknown => add_var("unknown"), |
103 | 130 | }
|
104 |
| - wasm_dep_analyzer::ExportType::Table => add_var("WebAssembly.Table"), |
105 |
| - wasm_dep_analyzer::ExportType::Memory => add_var("WebAssembly.Memory"), |
106 |
| - wasm_dep_analyzer::ExportType::Global(global_type) => match global_type { |
107 |
| - Ok(global_type) => add_var(value_type_to_ts_type( |
108 |
| - global_type.value_type, |
109 |
| - TypePosition::Output, |
110 |
| - )), |
111 |
| - Err(_) => add_var("unknown"), |
112 |
| - }, |
113 |
| - wasm_dep_analyzer::ExportType::Tag |
114 |
| - | wasm_dep_analyzer::ExportType::Unknown => add_var("unknown"), |
115 |
| - } |
116 | 131 |
|
117 |
| - if !has_valid_export_ident { |
118 |
| - text.push_str(&format!( |
119 |
| - "export {{ {} as \"{}\" }};\n", |
120 |
| - export_name, export.name |
121 |
| - )); |
| 132 | + if !has_valid_export_ident { |
| 133 | + builder.append("export { "); |
| 134 | + write_export_name(builder, export, has_valid_export_ident, i); |
| 135 | + builder.append(" as \""); |
| 136 | + builder.append(export.name); |
| 137 | + builder.append("\" };\n"); |
| 138 | + } |
122 | 139 | }
|
123 |
| - } |
124 |
| - text |
| 140 | + }) |
| 141 | + .unwrap() |
125 | 142 | }
|
126 | 143 |
|
127 | 144 | #[cfg(test)]
|
@@ -220,10 +237,10 @@ export declare const name5: WebAssembly.Memory;
|
220 | 237 | export declare const name6: number;
|
221 | 238 | export declare const name7: unknown;
|
222 | 239 | export declare const name8: unknown;
|
223 |
| -declare const __deno_wasm_export_1__: unknown; |
224 |
| -export { __deno_wasm_export_1__ as \"name9--\" }; |
225 |
| -declare const __deno_wasm_export_2__: unknown; |
226 |
| -export { __deno_wasm_export_2__ as \"default\" }; |
| 240 | +declare const __deno_wasm_export_8__: unknown; |
| 241 | +export { __deno_wasm_export_8__ as \"name9--\" }; |
| 242 | +declare const __deno_wasm_export_9__: unknown; |
| 243 | +export { __deno_wasm_export_9__ as \"default\" }; |
227 | 244 | "
|
228 | 245 | );
|
229 | 246 | }
|
|
0 commit comments