Skip to content

Commit 645d6dd

Browse files
committed
ref: Make nested objects with same props deduped
1 parent 9f2e0b1 commit 645d6dd

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/swc.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ fn infer_name_from_ctx(path: &AstNodePath) -> ScopeName {
223223
// `{ $name: ... }`
224224
Parent::KeyValueProp(kv, _) => {
225225
if let Some(ident) = kv.key.as_ident() {
226+
// Translate `children.children.children` into `{children}`
227+
// NOTE: We could make it `{children#3}`, but we'd need to count the occurrences.
228+
if let Some(component) = scope_name.components.front() {
229+
if component.text() == ident.sym.to_string() {
230+
scope_name.components.pop_front();
231+
scope_name
232+
.components
233+
.push_front(NameComponent::interp(format!("{{{}}}", ident.sym)));
234+
continue;
235+
}
236+
237+
if component.text() == format!("{{{}}}", ident.sym) {
238+
continue;
239+
}
240+
}
241+
226242
push_sep(&mut scope_name);
227243
scope_name
228244
.components

tests/extract.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,11 @@ fn extract_nested_iife_objects() {
236236
(function () {})({
237237
children: (function () {})({
238238
children: (function () {})({
239-
onSubmitError () {
240-
throw new Error('wat')
241-
}
239+
children: (function () {})({
240+
onSubmitError () {
241+
throw new Error('wat')
242+
}
243+
})
242244
})
243245
})
244246
})
@@ -249,8 +251,9 @@ fn extract_nested_iife_objects() {
249251
let expected = [
250252
None,
251253
Some("<object>.children".into()),
252-
Some("<object>.children.children".into()),
253-
Some("<object>.children.children.onSubmitError".into()),
254+
Some("<object>.{children}".into()),
255+
Some("<object>.{children}".into()),
256+
Some("<object>.{children}.onSubmitError".into()),
254257
];
255258
assert_eq!(scopes, expected);
256259
}

0 commit comments

Comments
 (0)