From 11017975fd48e2f9bf6951ac827001ecb9458184 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Sat, 17 Aug 2024 12:26:29 -0400 Subject: [PATCH] feat(semantic,syntax): add SymbolFlags::ArrowFunction There are many cases in lint rules where we want to see if a symbol is a function by checking its SymbolFlags. This is currently not fully possible, since variables assigned to arrow functions are not distinguished from any other kind of variable. This PR adds `SymbolFlags::ArrowFunction` for variables that are initialized to arrow functions. Symbols that are re-assigned to arrow functions will not have this flag, but this is acceptable for lint rules. --- crates/oxc_semantic/src/binder.rs | 8 +++- .../call-expression/call-expression.snap | 2 +- .../readable-ref-body-shadow.snap | 2 +- .../default-params/readable-ref-const.snap | 2 +- .../default-params/readable-ref-let.snap | 2 +- .../readable-ref-nested-body-shadow.snap | 2 +- .../default-params/readable-ref-nested.snap | 2 +- .../readable-ref-param-shadow.snap | 2 +- .../default-params/readable-ref-partial.snap | 2 +- .../arrow/default-params/writable-ref.snap | 2 +- .../functions/arrow/scope.snap | 2 +- .../arrow/type-parameters/body-reference.snap | 2 +- .../type-parameters/param-reference.snap | 2 +- .../return-value-reference.snap | 2 +- .../type-parameters/type-param-reference.snap | 2 +- .../type-parameter-declaration.snap | 2 +- .../arrow/type-predicate-asserts1.snap | 2 +- .../arrow/type-predicate-asserts2.snap | 2 +- .../functions/arrow/type-predicate1.snap | 2 +- .../functions/arrow/type-predicate2.snap | 2 +- .../module/variable-decl-const.snap | 2 +- .../module/variable-decl-let.snap | 2 +- .../module/variable-decl-var.snap | 2 +- .../script/variable-decl-const.snap | 2 +- .../script/variable-decl-let.snap | 2 +- .../script/variable-decl-var.snap | 2 +- crates/oxc_syntax/src/symbol.rs | 46 +++++++++++++------ 27 files changed, 65 insertions(+), 39 deletions(-) diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index f58c8eca0d4737..05349007142a1e 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -19,7 +19,7 @@ pub(crate) trait Binder<'a> { impl<'a> Binder<'a> for VariableDeclarator<'a> { fn bind(&self, builder: &mut SemanticBuilder<'a>) { - let (includes, excludes) = match self.kind { + let (mut includes, excludes) = match self.kind { VariableDeclarationKind::Const => ( SymbolFlags::BlockScopedVariable | SymbolFlags::ConstVariable, SymbolFlags::BlockScopedVariableExcludes, @@ -32,6 +32,12 @@ impl<'a> Binder<'a> for VariableDeclarator<'a> { } }; + if self.init.as_ref().is_some_and(|init| { + matches!(init.get_inner_expression(), Expression::ArrowFunctionExpression(_)) + }) { + includes |= SymbolFlags::ArrowFunction; + } + if self.kind.is_lexical() { self.id.bound_names(&mut |ident| { let symbol_id = builder.declare_symbol(ident.span, &ident.name, includes, excludes); diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/call-expression/call-expression.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/call-expression/call-expression.snap index 4591db113131d0..31f89fdfff3b9c 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/call-expression/call-expression.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/call-expression/call-expression.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/call-expression "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-body-shadow.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-body-shadow.snap index 9d38826a7499af..96240e8c92807d 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-body-shadow.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-body-shadow.snap @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-const.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-const.snap index 6305e258448ed6..4c680fe23c04a2 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-const.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-const.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-let.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-let.snap index 28e503905275ce..4d87f3c55e5464 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-let.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-let.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested-body-shadow.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested-body-shadow.snap index fe9320ffe91a08..2d3a18a6766b8b 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested-body-shadow.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested-body-shadow.snap @@ -55,7 +55,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested.snap index 19d48c3efe310e..ef5748cab72e09 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-nested.snap @@ -48,7 +48,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-param-shadow.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-param-shadow.snap index 81b71e353bc53e..5c229cc7d33107 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-param-shadow.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-param-shadow.snap @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "references": [] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-partial.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-partial.snap index 64144dc6747040..66c54910b62892 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-partial.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/readable-ref-partial.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/writable-ref.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/writable-ref.snap index e1b7773db2044f..ec06ee23532afc 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/writable-ref.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/default-params/writable-ref.snap @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/scope.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/scope.snap index b0e85e3e0ce343..e823a40bd31cec 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/scope.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/scope.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "arrow", "node": "VariableDeclarator(arrow)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/body-reference.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/body-reference.snap index d57ed25f45bf91..77d2c7419e76c4 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/body-reference.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/body-reference.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/param-reference.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/param-reference.snap index 2a471d81e5d53c..8be3e0847beed6 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/param-reference.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/param-reference.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/return-value-reference.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/return-value-reference.snap index b281fa060e032b..5136fbf0a3d99c 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/return-value-reference.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/return-value-reference.snap @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-param-reference.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-param-reference.snap index 1c922cd9ca7491..f68560c0c74c70 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-param-reference.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-param-reference.snap @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-parameter-declaration.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-parameter-declaration.snap index ee6047b26f15c8..7c4906fe2602b8 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-parameter-declaration.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-parameters/type-parameter-declaration.snap @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts1.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts1.snap index 760c2e908a827e..011e5884426fce 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts1.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts1.snap @@ -26,7 +26,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts2.snap index da8e49256cc116..804c449d2cafeb 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate-asserts2.snap @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate1.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate1.snap index 6cc1792a728f2a..08036c353837b4 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate1.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate1.snap @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate2.snap index 8d0a425debf8e5..1ee6b5fa70195c 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow/type-predicate2.snap @@ -54,7 +54,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow ] }, { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 1, "name": "foo", "node": "VariableDeclarator(foo)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-const.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-const.snap index 2a3e292a303585..3fe613b1a11aa8 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-const.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-const.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "top", "node": "VariableDeclarator(top)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-let.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-let.snap index 0999057c67c4ea..0cf716dd9c824c 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-let.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-let.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 0, "name": "top", "node": "VariableDeclarator(top)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-var.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-var.snap index afe358e19ba68a..458d3616de15c3 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-var.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/module/variable-decl-var.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti "node": "Program", "symbols": [ { - "flag": "SymbolFlags(FunctionScopedVariable)", + "flag": "SymbolFlags(FunctionScopedVariable | ArrowFunction)", "id": 0, "name": "top", "node": "VariableDeclarator(top)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-const.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-const.snap index 033633a5fbb7e0..ead5b246970340 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-const.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-const.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable | ConstVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)", "id": 0, "name": "top", "node": "VariableDeclarator(top)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-let.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-let.snap index 64191a83fc249a..f7c460c6d881af 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-let.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-let.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti "node": "Program", "symbols": [ { - "flag": "SymbolFlags(BlockScopedVariable)", + "flag": "SymbolFlags(BlockScopedVariable | ArrowFunction)", "id": 0, "name": "top", "node": "VariableDeclarator(top)", diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-var.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-var.snap index a6f06bb6b9bba7..a3b709649ab5e5 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-var.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resolution/script/variable-decl-var.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti "node": "Program", "symbols": [ { - "flag": "SymbolFlags(FunctionScopedVariable)", + "flag": "SymbolFlags(FunctionScopedVariable | ArrowFunction)", "id": 0, "name": "top", "node": "VariableDeclarator(top)", diff --git a/crates/oxc_syntax/src/symbol.rs b/crates/oxc_syntax/src/symbol.rs index 671891f14f3747..1ac6e953127eff 100644 --- a/crates/oxc_syntax/src/symbol.rs +++ b/crates/oxc_syntax/src/symbol.rs @@ -77,22 +77,28 @@ bitflags! { /// Is this symbol inside an export declaration const Export = 1 << 4; const Class = 1 << 5; - const CatchVariable = 1 << 6; // try {} catch(catch_variable) {} + /// `try {} catch(catch_variable) {}` + const CatchVariable = 1 << 6; + /// A function declaration or expression const Function = 1 << 7; - const Import = 1 << 8; // Imported ESM binding - const TypeImport = 1 << 9; // Imported ESM type-only binding + /// A function or block-scoped variable initialized to an arrow function + const ArrowFunction = 1 << 8; + /// Imported ESM binding + const Import = 1 << 9; + /// Imported ESM type-only binding + const TypeImport = 1 << 10; // Type specific symbol flags - const TypeAlias = 1 << 10; - const Interface = 1 << 11; - const RegularEnum = 1 << 12; - const ConstEnum = 1 << 13; - const EnumMember = 1 << 14; - const TypeLiteral = 1 << 15; - const TypeParameter = 1 << 16; - const NameSpaceModule = 1 << 17; - const ValueModule = 1 << 18; + const TypeAlias = 1 << 11; + const Interface = 1 << 12; + const RegularEnum = 1 << 13; + const ConstEnum = 1 << 14; + const EnumMember = 1 << 15; + const TypeLiteral = 1 << 16; + const TypeParameter = 1 << 17; + const NameSpaceModule = 1 << 18; + const ValueModule = 1 << 19; // In a dts file or there is a declare flag - const Ambient = 1 << 19; + const Ambient = 1 << 20; const Enum = Self::ConstEnum.bits() | Self::RegularEnum.bits(); @@ -150,11 +156,25 @@ impl SymbolFlags { self.contains(Self::ConstVariable) } + /// Returns `true` if this symbol is a function declaration or expression. + /// + /// Use [`SymbolFlags::is_function_like`] to check if this symbol is a function or an arrow function. #[inline] pub fn is_function(&self) -> bool { self.contains(Self::Function) } + #[inline] + pub fn is_arrow_function(&self) -> bool { + self.contains(Self::ArrowFunction) + } + + /// Returns `true` if this symbol is an arrow function or a function declaration/expression. + #[inline] + pub fn is_function_like(&self) -> bool { + self.intersects(Self::Function | Self::ArrowFunction) + } + #[inline] pub fn is_class(&self) -> bool { self.contains(Self::Class)