From a442fad3b7995dff53869a56eb5d6f3aa462223e Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 24 Oct 2023 17:25:12 +0800 Subject: [PATCH] feat(semantic): bind function expression name (#1049) closes #1018 We'll eventually recover the mangling tests, which should cover this change. --- crates/oxc_semantic/src/binder.rs | 10 ++++++++++ tasks/coverage/src/test262.rs | 26 +++++--------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index 4eba646cb..029380ab5 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -138,6 +138,16 @@ impl<'a> Binder for Function<'a> { excludes, ); ident.symbol_id.set(Some(symbol_id)); + } else if self.r#type == FunctionType::FunctionExpression { + // https://tc39.es/ecma262/#sec-runtime-semantics-instantiateordinaryfunctionexpression + // 5. Perform ! funcEnv.CreateImmutableBinding(name, false). + let symbol_id = builder.declare_symbol( + ident.span, + &ident.name, + SymbolFlags::empty(), + SymbolFlags::empty(), + ); + ident.symbol_id.set(Some(symbol_id)); } } diff --git a/tasks/coverage/src/test262.rs b/tasks/coverage/src/test262.rs index c899eef86..789d3a42a 100644 --- a/tasks/coverage/src/test262.rs +++ b/tasks/coverage/src/test262.rs @@ -203,7 +203,7 @@ impl Case for Test262Case { } fn are_all_identifiers_resolved(semantic: &oxc_semantic::Semantic<'_>) -> bool { - use oxc_ast::{ast, AstKind}; + use oxc_ast::AstKind; use oxc_semantic::AstNode; let ast_nodes = semantic.nodes(); @@ -212,27 +212,11 @@ fn are_all_identifiers_resolved(semantic: &oxc_semantic::Semantic<'_>) -> bool { AstKind::BindingIdentifier(id) => { let mut parents = ast_nodes.iter_parents(node.id()).map(AstNode::kind); parents.next(); // Exclude BindingIdentifier itself - match parents.next() { - Some(AstKind::Function(func)) - if func.r#type == ast::FunctionType::FunctionExpression => - { - // FIXME: Currently, the name of `FunctionExpression` won't be assigned a `SymbolId` - return false; - } - _ => {} + if let (Some(AstKind::Function(_)), Some(AstKind::IfStatement(_))) = + (parents.next(), parents.next()) + { + return false; } - let mut parents = ast_nodes.iter_parents(node.id()).map(AstNode::kind); - parents.next(); // Exclude BindingIdentifier itself - match (parents.next(), parents.next()) { - // FIXME: case like `if (xx) ; else function test() {}` - (Some(AstKind::Function(func)), Some(AstKind::IfStatement(_))) - if func.r#type == ast::FunctionType::FunctionDeclaration => - { - return false; - } - _ => {} - } - id.symbol_id.get().is_none() } AstKind::IdentifierReference(ref_id) => ref_id.reference_id.get().is_none(),