diff --git a/src/swc.rs b/src/swc.rs index fdae5b3..f8e608a 100644 --- a/src/swc.rs +++ b/src/swc.rs @@ -223,6 +223,22 @@ fn infer_name_from_ctx(path: &AstNodePath) -> ScopeName { // `{ $name: ... }` Parent::KeyValueProp(kv, _) => { if let Some(ident) = kv.key.as_ident() { + // Translate `children.children.children` into `{children}` + // NOTE: We could make it `{children#3}`, but we'd need to count the occurrences. + if let Some(component) = scope_name.components.front() { + if component.text() == ident.sym.to_string() { + scope_name.components.pop_front(); + scope_name + .components + .push_front(NameComponent::interp(format!("{{{}}}", ident.sym))); + continue; + } + + if component.text() == format!("{{{}}}", ident.sym) { + continue; + } + } + push_sep(&mut scope_name); scope_name .components diff --git a/tests/extract.rs b/tests/extract.rs index d8c1ad2..b02decd 100644 --- a/tests/extract.rs +++ b/tests/extract.rs @@ -236,9 +236,11 @@ fn extract_nested_iife_objects() { (function () {})({ children: (function () {})({ children: (function () {})({ - onSubmitError () { - throw new Error('wat') - } + children: (function () {})({ + onSubmitError () { + throw new Error('wat') + } + }) }) }) }) @@ -249,8 +251,9 @@ fn extract_nested_iife_objects() { let expected = [ None, Some(".children".into()), - Some(".children.children".into()), - Some(".children.children.onSubmitError".into()), + Some(".{children}".into()), + Some(".{children}".into()), + Some(".{children}.onSubmitError".into()), ]; assert_eq!(scopes, expected); }