From a03cd699d6b7a69d986c381fa2bbc3655695b6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 22 Nov 2022 21:35:16 +0100 Subject: [PATCH] fix: Correctly handle scope names for nested objects --- src/swc.rs | 2 ++ tests/extract.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/swc.rs b/src/swc.rs index c9dfdde..fdae5b3 100644 --- a/src/swc.rs +++ b/src/swc.rs @@ -213,6 +213,7 @@ fn infer_name_from_ctx(path: &AstNodePath) -> ScopeName { // An object literal member: // `{ $name() ... }` Parent::MethodProp(method, _) => { + push_sep(&mut scope_name); scope_name .components .push_front(prop_name_to_component(&method.key)); @@ -222,6 +223,7 @@ fn infer_name_from_ctx(path: &AstNodePath) -> ScopeName { // `{ $name: ... }` Parent::KeyValueProp(kv, _) => { if let Some(ident) = kv.key.as_ident() { + push_sep(&mut scope_name); scope_name .components .push_front(NameComponent::ident(ident.clone())); diff --git a/tests/extract.rs b/tests/extract.rs index 297a23f..d8c1ad2 100644 --- a/tests/extract.rs +++ b/tests/extract.rs @@ -228,3 +228,29 @@ fn extract_empty_function() { let expected = [None, None]; assert_eq!(scopes, expected); } + +#[test] +fn extract_nested_iife_objects() { + // NOTE: This mimicks what react-dom does to transpile JSX children into render tree. + let src = r#" + (function () {})({ + children: (function () {})({ + children: (function () {})({ + onSubmitError () { + throw new Error('wat') + } + }) + }) + }) + "#; + let scopes = extract_scope_names(src).unwrap(); + let scopes = scope_strs(scopes); + + let expected = [ + None, + Some(".children".into()), + Some(".children.children".into()), + Some(".children.children.onSubmitError".into()), + ]; + assert_eq!(scopes, expected); +}