Skip to content

Commit 597b697

Browse files
committed
Forbid duplicated getter/setter names in fields and methods
1 parent af1f051 commit 597b697

File tree

1 file changed

+49
-1
lines changed
  • crates/cli-support/src/js

1 file changed

+49
-1
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,10 @@ impl<'a> Context<'a> {
18191819
}
18201820

18211821
pub fn generate(&mut self, aux: &WasmBindgenAux) -> Result<(), Error> {
1822-
for (id, export) in sorted_iter(&aux.export_map) {
1822+
let mut pairs = aux.export_map.iter().collect::<Vec<_>>();
1823+
pairs.sort_by_key(|(k, _)| *k);
1824+
check_duplicated_getter_and_setter_names(&pairs)?;
1825+
for (id, export) in pairs {
18231826
self.generate_export(*id, export).with_context(|_| {
18241827
format!(
18251828
"failed to generate bindings for Rust export `{}`",
@@ -2122,6 +2125,51 @@ impl<'a> Context<'a> {
21222125
}
21232126
}
21242127

2128+
fn check_duplicated_getter_and_setter_names(
2129+
exports: &[(&ExportId, &AuxExport)],
2130+
) -> Result<(), Error> {
2131+
let verify_exports =
2132+
|first_class, first_field, second_class, second_field| -> Result<(), Error> {
2133+
let both_are_in_the_same_class = first_class == second_class;
2134+
let both_are_referencing_the_same_field = first_field == second_field;
2135+
if both_are_in_the_same_class && both_are_referencing_the_same_field {
2136+
bail!(format!(
2137+
"There can be only one getter/setter definition for `{}` in `{}`",
2138+
first_field, first_class
2139+
));
2140+
}
2141+
Ok(())
2142+
};
2143+
for (idx, (_, first_export)) in exports.iter().enumerate() {
2144+
for (_, second_export) in exports.iter().skip(idx + 1) {
2145+
match (&first_export.kind, &second_export.kind) {
2146+
(
2147+
AuxExportKind::Getter {
2148+
class: first_class,
2149+
field: first_field,
2150+
},
2151+
AuxExportKind::Getter {
2152+
class: second_class,
2153+
field: second_field,
2154+
},
2155+
) => verify_exports(first_class, first_field, second_class, second_field)?,
2156+
(
2157+
AuxExportKind::Setter {
2158+
class: first_class,
2159+
field: first_field,
2160+
},
2161+
AuxExportKind::Setter {
2162+
class: second_class,
2163+
field: second_field,
2164+
},
2165+
) => verify_exports(first_class, first_field, second_class, second_field)?,
2166+
_ => {}
2167+
}
2168+
}
2169+
}
2170+
Ok(())
2171+
}
2172+
21252173
fn generate_identifier(name: &str, used_names: &mut HashMap<String, usize>) -> String {
21262174
let cnt = used_names.entry(name.to_string()).or_insert(0);
21272175
*cnt += 1;

0 commit comments

Comments
 (0)