Skip to content

Commit

Permalink
refactor(syntax)!: remove SymbolFlags::ArrowFunction (#5857)
Browse files Browse the repository at this point in the history
`SymbolFlags::ArrowFunction` is an oddity, as whether a symbol is an arrow function is not statically knowable. In the following cases, `f` symbol did not have `ArrowFunction` flag set:

```js
const {f} = {f: () => {}};
```

```js
let f = 123;
f = () => {};
```

`SymbolFlags::ArrowFunction` is therefore not particularly useful, and possibly misleading. Having it complicates the transformer, and it's not used anywhere in Oxc.

This PR removes it.
  • Loading branch information
overlookmotel committed Sep 18, 2024
1 parent 7f05eed commit c96b712
Show file tree
Hide file tree
Showing 32 changed files with 49 additions and 102 deletions.
8 changes: 1 addition & 7 deletions crates/oxc_semantic/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) trait Binder<'a> {

impl<'a> Binder<'a> for VariableDeclarator<'a> {
fn bind(&self, builder: &mut SemanticBuilder<'a>) {
let (mut includes, excludes) = match self.kind {
let (includes, excludes) = match self.kind {
VariableDeclarationKind::Const => (
SymbolFlags::BlockScopedVariable | SymbolFlags::ConstVariable,
SymbolFlags::BlockScopedVariableExcludes,
Expand All @@ -38,12 +38,6 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/jsx/element-name.jsx
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 0,
"name": "Component",
"node": "VariableDeclarator(Component)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/call-expression
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"references": []
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "arrow",
"node": "VariableDeclarator(arrow)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/arrow
]
},
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 1,
"name": "foo",
"node": "VariableDeclarator(foo)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "top",
"node": "VariableDeclarator(top)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 0,
"name": "top",
"node": "VariableDeclarator(top)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(FunctionScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(FunctionScopedVariable)",
"id": 0,
"name": "top",
"node": "VariableDeclarator(top)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable | ConstVariable)",
"id": 0,
"name": "top",
"node": "VariableDeclarator(top)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(BlockScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(BlockScopedVariable)",
"id": 0,
"name": "top",
"node": "VariableDeclarator(top)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/global-resoluti
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(FunctionScopedVariable | ArrowFunction)",
"flags": "SymbolFlags(FunctionScopedVariable)",
"id": 0,
"name": "top",
"node": "VariableDeclarator(top)",
Expand Down
39 changes: 12 additions & 27 deletions crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,22 @@ bitflags! {
const CatchVariable = 1 << 6;
/// A function declaration or expression
const Function = 1 << 7;
/// A function or block-scoped variable initialized to an arrow function
const ArrowFunction = 1 << 8;
/// Imported ESM binding
const Import = 1 << 9;
const Import = 1 << 8;
/// Imported ESM type-only binding
const TypeImport = 1 << 10;
const TypeImport = 1 << 9;
// Type specific symbol flags
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;
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;
// In a dts file or there is a declare flag
const Ambient = 1 << 20;
const Ambient = 1 << 19;

const Enum = Self::ConstEnum.bits() | Self::RegularEnum.bits();

Expand Down Expand Up @@ -158,24 +156,11 @@ impl SymbolFlags {
}

/// 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)
Expand Down
14 changes: 0 additions & 14 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,6 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
*expr = self.transform_arrow_function_expression(arrow_function_expr.unbox(), ctx);
}
}

fn enter_variable_declarator(
&mut self,
node: &mut VariableDeclarator<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if !matches!(node.init, Some(Expression::ArrowFunctionExpression(_))) {
return;
}

let Some(id) = node.id.get_binding_identifier() else { return };
*ctx.symbols_mut().get_flags_mut(id.symbol_id.get().unwrap()) &=
!SymbolFlags::ArrowFunction;
}
}

impl<'a> ArrowFunctions<'a> {
Expand Down
10 changes: 0 additions & 10 deletions crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ impl<'a> Traverse<'a> for ES2015<'a> {
}
}

fn enter_variable_declarator(
&mut self,
node: &mut VariableDeclarator<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.arrow_function.is_some() {
self.arrow_functions.enter_variable_declarator(node, ctx);
}
}

fn enter_jsx_element_name(&mut self, node: &mut JSXElementName<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.arrow_function.is_some() {
self.arrow_functions.enter_jsx_element_name(node, ctx);
Expand Down
8 changes: 0 additions & 8 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,6 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x2_es2019.enter_catch_clause(clause, ctx);
}

fn enter_variable_declarator(
&mut self,
node: &mut VariableDeclarator<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x3_es2015.enter_variable_declarator(node, ctx);
}

fn enter_import_declaration(
&mut self,
node: &mut ImportDeclaration<'a>,
Expand Down
Loading

0 comments on commit c96b712

Please sign in to comment.