From d3eb1c3318a1ff0263d8ea772d3a323f14fd5d20 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Tue, 2 Apr 2024 22:42:11 -0400 Subject: [PATCH] fix(semantic): flag function expressions with `SymbolFlags::Function` (#2891) # What This PR Does Consider the following code snippet ```js const x = function y () {{ ``` `y` will now be flagged with `SymbolFlags::Function`. This follow's tsc's behavior. - [Example in OXC Playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAICagICAgICAgICxG0qZRraXZOpcCHVsSRwDq2kRR0HprsTfRRT5WMw%2Ff2epoIA%3D) - [Example in TypeScript AST Viewer](https://ts-ast-viewer.com/#code/MYewdgzgLgBAHjAvDAZgVzMKBLcMCeAFAJQwDeAvgFBA) --- crates/oxc_semantic/src/binder.rs | 2 +- crates/oxc_semantic/tests/symbols.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index 9bd0010b6..17b972466 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -146,7 +146,7 @@ impl<'a> Binder for Function<'a> { let symbol_id = builder.declare_symbol( ident.span, &ident.name, - SymbolFlags::empty(), + SymbolFlags::Function, SymbolFlags::empty(), ); ident.symbol_id.set(Some(symbol_id)); diff --git a/crates/oxc_semantic/tests/symbols.rs b/crates/oxc_semantic/tests/symbols.rs index d2054d2e7..1dbaa81d0 100644 --- a/crates/oxc_semantic/tests/symbols.rs +++ b/crates/oxc_semantic/tests/symbols.rs @@ -26,6 +26,18 @@ fn test_function_simple() { .test(); } +#[test] +fn test_function_expressions() { + SemanticTester::js("const x = function y() {}") + .has_some_symbol("y") + .contains_flags(SymbolFlags::Function) + .test(); + SemanticTester::js("const x = () => {}") + .has_some_symbol("x") + .contains_flags(SymbolFlags::BlockScopedVariable | SymbolFlags::ConstVariable) + .test(); +} + #[test] fn test_var_simple() { SemanticTester::js("let x; { let y; }")