Skip to content

Commit a03cd69

Browse files
committed
fix: Correctly handle scope names for nested objects
1 parent bf74003 commit a03cd69

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/swc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ fn infer_name_from_ctx(path: &AstNodePath) -> ScopeName {
213213
// An object literal member:
214214
// `{ $name() ... }`
215215
Parent::MethodProp(method, _) => {
216+
push_sep(&mut scope_name);
216217
scope_name
217218
.components
218219
.push_front(prop_name_to_component(&method.key));
@@ -222,6 +223,7 @@ fn infer_name_from_ctx(path: &AstNodePath) -> ScopeName {
222223
// `{ $name: ... }`
223224
Parent::KeyValueProp(kv, _) => {
224225
if let Some(ident) = kv.key.as_ident() {
226+
push_sep(&mut scope_name);
225227
scope_name
226228
.components
227229
.push_front(NameComponent::ident(ident.clone()));

tests/extract.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,29 @@ fn extract_empty_function() {
228228
let expected = [None, None];
229229
assert_eq!(scopes, expected);
230230
}
231+
232+
#[test]
233+
fn extract_nested_iife_objects() {
234+
// NOTE: This mimicks what react-dom does to transpile JSX children into render tree.
235+
let src = r#"
236+
(function () {})({
237+
children: (function () {})({
238+
children: (function () {})({
239+
onSubmitError () {
240+
throw new Error('wat')
241+
}
242+
})
243+
})
244+
})
245+
"#;
246+
let scopes = extract_scope_names(src).unwrap();
247+
let scopes = scope_strs(scopes);
248+
249+
let expected = [
250+
None,
251+
Some("<object>.children".into()),
252+
Some("<object>.children.children".into()),
253+
Some("<object>.children.children.onSubmitError".into()),
254+
];
255+
assert_eq!(scopes, expected);
256+
}

0 commit comments

Comments
 (0)