Skip to content

Commit

Permalink
fix(es/utils): Mark ident of default fn/class as binding (#8764)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Mar 22, 2024
1 parent ad932f0 commit f62097c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions crates/swc_ecma_utils/src/lib.rs
Expand Up @@ -2797,6 +2797,24 @@ where
self.is_pat_decl = old;
}

fn visit_export_default_decl(&mut self, e: &ExportDefaultDecl) {
match &e.decl {
DefaultDecl::Class(ClassExpr {
ident: Some(ident), ..
}) => {
self.add(ident);
}
DefaultDecl::Fn(FnExpr {
ident: Some(ident),
function: f,
}) if f.body.is_some() => {
self.add(ident);
}
_ => {}
}
e.visit_children_with(self);
}

fn visit_fn_decl(&mut self, node: &FnDecl) {
node.visit_children_with(self);

Expand Down Expand Up @@ -3253,6 +3271,12 @@ mod test {
run_collect_decls("const [ a, b = 1, [c], ...d ] = [];", &["a", "b", "c", "d"]);
}

#[test]
fn test_collect_export_default_expr() {
run_collect_decls("export default function foo(){}", &["foo"]);
run_collect_decls("export default class Foo{}", &["Foo"]);
}

fn run_collect_decls(text: &str, expected_names: &[&str]) {
let module = parse_module(text);
let decls: AHashSet<Id> = collect_decls(&module);
Expand Down

0 comments on commit f62097c

Please sign in to comment.