Skip to content

Commit 1ea0ccd

Browse files
author
s.vanriessen
committed
fix: duplicated function names for same interfaces and export
1 parent 2330f65 commit 1ea0ccd

File tree

1 file changed

+24
-8
lines changed
  • crates/spidermonkey-embedding-splicer/src

1 file changed

+24
-8
lines changed

crates/spidermonkey-embedding-splicer/src/bindgen.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ pub fn componentize_bindgen(
172172
// consolidate import specifiers and generate wrappers
173173
// we do this separately because function index order matters
174174
let mut import_bindings = Vec::new();
175-
for (_, item) in bindgen.imports.iter() {
175+
for (specifier, item) in bindgen.imports.iter() {
176176
// this import binding order matters
177-
import_bindings.push(binding_name(
177+
import_bindings.push(binding_name_import(
178178
&item.resource.func_name(&item.name),
179179
&item.iface_name,
180+
specifier,
180181
));
181182
}
182183

@@ -204,7 +205,7 @@ pub fn componentize_bindgen(
204205
let item = items.first().unwrap();
205206
if let Some(resource) = resource {
206207
let export_name = resource.to_upper_camel_case();
207-
let binding_name = binding_name(&export_name, &item.iface_name);
208+
let binding_name = binding_name_import(&export_name, &item.iface_name, &item.binding_name);
208209
if item.iface {
209210
specifier_list.push(format!("{export_name}: import_{binding_name}"));
210211
} else {
@@ -213,13 +214,12 @@ pub fn componentize_bindgen(
213214
} else {
214215
for BindingItem {
215216
iface,
216-
iface_name,
217217
name,
218+
binding_name,
218219
..
219220
} in items
220221
{
221222
let export_name = name.to_lower_camel_case();
222-
let binding_name = binding_name(&export_name, iface_name);
223223
if *iface {
224224
specifier_list.push(format!("{export_name}: import_{binding_name}"));
225225
} else {
@@ -654,11 +654,11 @@ impl JsBindgen<'_> {
654654
let fn_name = func.item_name();
655655
let fn_camel_name = fn_name.to_lower_camel_case();
656656

657-
use binding_name as binding_name_fn;
657+
use binding_name_import as binding_name_fn;
658658

659659
let (binding_name, resource) = match &func.kind {
660660
FunctionKind::Freestanding => {
661-
let binding_name = binding_name(&fn_camel_name, &iface_name);
661+
let binding_name = binding_name_import(&fn_camel_name, &iface_name, &import_name);
662662

663663
uwrite!(self.src, "\nfunction import_{binding_name}");
664664

@@ -702,7 +702,7 @@ impl JsBindgen<'_> {
702702
func.params.len(),
703703
&format!(
704704
"$import_{}",
705-
binding_name_fn(&resource.func_name(fn_name), &iface_name)
705+
binding_name_fn(&resource.func_name(fn_name), &iface_name, import_name.as_str())
706706
),
707707
StringEncoding::UTF8,
708708
func,
@@ -1294,6 +1294,22 @@ fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
12941294
}
12951295
}
12961296

1297+
fn binding_name_import(func_name: &str, iface_name: &Option<String>, import_name: &str) -> String {
1298+
let valid_import = import_name
1299+
.chars()
1300+
.map(|c| if c.is_alphanumeric() { c } else { '_' })
1301+
.collect::<String>();
1302+
1303+
if import_name != "<<INVALID>>" {
1304+
let s = valid_import.to_string();
1305+
format!("{s}${func_name}")
1306+
} else if let Some(iface_name) = iface_name {
1307+
format!("{iface_name}${func_name}")
1308+
} else {
1309+
func_name.to_string()
1310+
}
1311+
}
1312+
12971313
/// Extract success and error types from a given optional type, if it is a Result
12981314
pub fn get_result_types(
12991315
resolve: &Resolve,

0 commit comments

Comments
 (0)