mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer): use *_with_scope_id builder methods where possible (#7055)
In transformer, use `AstBuilder::function_with_scope_id` and `AstBuilder::arrow_function_expression_with_scope_id` function where possible, rather than creating a function and then setting the `scope_id` on it afterwards.
This commit is contained in:
parent
ee27b92465
commit
4688a061ae
6 changed files with 40 additions and 28 deletions
|
|
@ -10,7 +10,7 @@ use std::mem;
|
||||||
|
|
||||||
use oxc_allocator::{Allocator, Box, FromIn, String, Vec};
|
use oxc_allocator::{Allocator, Box, FromIn, String, Vec};
|
||||||
use oxc_span::{Atom, GetSpan, Span};
|
use oxc_span::{Atom, GetSpan, Span};
|
||||||
use oxc_syntax::{number::NumberBase, operator::UnaryOperator};
|
use oxc_syntax::{number::NumberBase, operator::UnaryOperator, scope::ScopeId};
|
||||||
|
|
||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
use crate::AstBuilder;
|
use crate::AstBuilder;
|
||||||
|
|
@ -234,18 +234,19 @@ impl<'a> AstBuilder<'a> {
|
||||||
self.formal_parameter(span, self.vec(), pattern, None, false, false)
|
self.formal_parameter(span, self.vec(), pattern, None, false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a [`Function`] with no "extras", i.e. decorators, type
|
/// Create a [`Function`] with no "extras".
|
||||||
/// annotations, accessibility modifiers, etc.
|
/// i.e. no decorators, type annotations, accessibility modifiers, etc.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn plain_function(
|
pub fn alloc_plain_function_with_scope_id(
|
||||||
self,
|
self,
|
||||||
r#type: FunctionType,
|
r#type: FunctionType,
|
||||||
span: Span,
|
span: Span,
|
||||||
id: Option<BindingIdentifier<'a>>,
|
id: Option<BindingIdentifier<'a>>,
|
||||||
params: FormalParameters<'a>,
|
params: FormalParameters<'a>,
|
||||||
body: FunctionBody<'a>,
|
body: FunctionBody<'a>,
|
||||||
|
scope_id: ScopeId,
|
||||||
) -> Box<'a, Function<'a>> {
|
) -> Box<'a, Function<'a>> {
|
||||||
self.alloc(self.function(
|
self.alloc_function_with_scope_id(
|
||||||
r#type,
|
r#type,
|
||||||
span,
|
span,
|
||||||
id,
|
id,
|
||||||
|
|
@ -257,7 +258,8 @@ impl<'a> AstBuilder<'a> {
|
||||||
params,
|
params,
|
||||||
NONE,
|
NONE,
|
||||||
Some(body),
|
Some(body),
|
||||||
))
|
scope_id,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------- Modules ---------- */
|
/* ---------- Modules ---------- */
|
||||||
|
|
|
||||||
|
|
@ -383,7 +383,7 @@ impl<'a> ArrowFunctions<'a> {
|
||||||
let flags = ctx.scopes_mut().get_flags_mut(scope_id);
|
let flags = ctx.scopes_mut().get_flags_mut(scope_id);
|
||||||
*flags &= !ScopeFlags::Arrow;
|
*flags &= !ScopeFlags::Arrow;
|
||||||
|
|
||||||
let new_function = ctx.ast.function(
|
Expression::FunctionExpression(ctx.ast.alloc_function_with_scope_id(
|
||||||
FunctionType::FunctionExpression,
|
FunctionType::FunctionExpression,
|
||||||
arrow_function_expr.span,
|
arrow_function_expr.span,
|
||||||
None,
|
None,
|
||||||
|
|
@ -395,10 +395,8 @@ impl<'a> ArrowFunctions<'a> {
|
||||||
arrow_function_expr.params,
|
arrow_function_expr.params,
|
||||||
arrow_function_expr.return_type,
|
arrow_function_expr.return_type,
|
||||||
Some(body),
|
Some(body),
|
||||||
);
|
scope_id,
|
||||||
new_function.scope_id.set(Some(scope_id));
|
))
|
||||||
|
|
||||||
Expression::FunctionExpression(ctx.ast.alloc(new_function))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert `var _this = this;` at the top of the statements.
|
/// Insert `var _this = this;` at the top of the statements.
|
||||||
|
|
|
||||||
|
|
@ -117,10 +117,18 @@ impl<'a, 'ctx> Traverse<'a> for NullishCoalescingOperator<'a, 'ctx> {
|
||||||
ctx.ast.vec(),
|
ctx.ast.vec(),
|
||||||
ctx.ast.vec1(ctx.ast.statement_expression(SPAN, new_expr)),
|
ctx.ast.vec1(ctx.ast.statement_expression(SPAN, new_expr)),
|
||||||
);
|
);
|
||||||
let arrow_function =
|
let arrow_function = Expression::ArrowFunctionExpression(
|
||||||
ctx.ast.arrow_function_expression(SPAN, true, false, NONE, params, NONE, body);
|
ctx.ast.alloc_arrow_function_expression_with_scope_id(
|
||||||
arrow_function.scope_id.set(Some(current_scope_id));
|
SPAN,
|
||||||
let arrow_function = ctx.ast.expression_from_arrow_function(arrow_function);
|
true,
|
||||||
|
false,
|
||||||
|
NONE,
|
||||||
|
params,
|
||||||
|
NONE,
|
||||||
|
body,
|
||||||
|
current_scope_id,
|
||||||
|
),
|
||||||
|
);
|
||||||
// `(x) => x;` -> `((x) => x)();`
|
// `(x) => x;` -> `((x) => x)();`
|
||||||
new_expr = ctx.ast.expression_call(SPAN, arrow_function, NONE, ctx.ast.vec(), false);
|
new_expr = ctx.ast.expression_call(SPAN, arrow_function, NONE, ctx.ast.vec(), false);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -567,7 +567,8 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
|
||||||
Some(ctx.ast.expression_array(SPAN, custom_hooks_in_scope, None)),
|
Some(ctx.ast.expression_array(SPAN, custom_hooks_in_scope, None)),
|
||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
let function = ctx.ast.function(
|
let scope_id = ctx.create_child_scope_of_current(ScopeFlags::Function);
|
||||||
|
let function = Argument::FunctionExpression(ctx.ast.alloc_function_with_scope_id(
|
||||||
FunctionType::FunctionExpression,
|
FunctionType::FunctionExpression,
|
||||||
SPAN,
|
SPAN,
|
||||||
None,
|
None,
|
||||||
|
|
@ -579,10 +580,9 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
|
||||||
formal_parameters,
|
formal_parameters,
|
||||||
NONE,
|
NONE,
|
||||||
Some(function_body),
|
Some(function_body),
|
||||||
);
|
scope_id,
|
||||||
let scope_id = ctx.create_child_scope_of_current(ScopeFlags::Function);
|
));
|
||||||
function.scope_id.set(Some(scope_id));
|
arguments.push(function);
|
||||||
arguments.push(ctx.ast.argument_expression(ctx.ast.expression_from_function(function)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Handle var hoisted in ctx API
|
// TODO: Handle var hoisted in ctx API
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ impl<'a> TypeScriptEnum<'a> {
|
||||||
|
|
||||||
let statements = self.transform_ts_enum_members(&mut decl.members, &ident, ctx);
|
let statements = self.transform_ts_enum_members(&mut decl.members, &ident, ctx);
|
||||||
let body = ast.alloc_function_body(decl.span, ast.vec(), statements);
|
let body = ast.alloc_function_body(decl.span, ast.vec(), statements);
|
||||||
let function = ctx.ast.function(
|
let callee = Expression::FunctionExpression(ctx.ast.alloc_function_with_scope_id(
|
||||||
FunctionType::FunctionExpression,
|
FunctionType::FunctionExpression,
|
||||||
SPAN,
|
SPAN,
|
||||||
None,
|
None,
|
||||||
|
|
@ -113,9 +113,8 @@ impl<'a> TypeScriptEnum<'a> {
|
||||||
params,
|
params,
|
||||||
None::<TSTypeAnnotation>,
|
None::<TSTypeAnnotation>,
|
||||||
Some(body),
|
Some(body),
|
||||||
);
|
func_scope_id,
|
||||||
function.scope_id.set(Some(func_scope_id));
|
));
|
||||||
let callee = ctx.ast.expression_from_function(function);
|
|
||||||
|
|
||||||
let var_symbol_id = decl.id.symbol_id.get().unwrap();
|
let var_symbol_id = decl.id.symbol_id.get().unwrap();
|
||||||
let arguments = if (is_export || is_not_top_scope) && !is_already_declared {
|
let arguments = if (is_export || is_not_top_scope) && !is_already_declared {
|
||||||
|
|
|
||||||
|
|
@ -336,12 +336,17 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
|
||||||
let items = ctx.ast.vec1(ctx.ast.plain_formal_parameter(SPAN, pattern));
|
let items = ctx.ast.vec1(ctx.ast.plain_formal_parameter(SPAN, pattern));
|
||||||
ctx.ast.formal_parameters(SPAN, FormalParameterKind::FormalParameter, items, NONE)
|
ctx.ast.formal_parameters(SPAN, FormalParameterKind::FormalParameter, items, NONE)
|
||||||
};
|
};
|
||||||
let function =
|
let function_expr =
|
||||||
ctx.ast.plain_function(FunctionType::FunctionExpression, SPAN, None, params, body);
|
Expression::FunctionExpression(ctx.ast.alloc_plain_function_with_scope_id(
|
||||||
function.scope_id.set(Some(scope_id));
|
FunctionType::FunctionExpression,
|
||||||
|
SPAN,
|
||||||
|
None,
|
||||||
|
params,
|
||||||
|
body,
|
||||||
|
scope_id,
|
||||||
|
));
|
||||||
*ctx.scopes_mut().get_flags_mut(scope_id) =
|
*ctx.scopes_mut().get_flags_mut(scope_id) =
|
||||||
ScopeFlags::Function | ScopeFlags::StrictMode;
|
ScopeFlags::Function | ScopeFlags::StrictMode;
|
||||||
let function_expr = ctx.ast.expression_from_function(function);
|
|
||||||
ctx.ast.expression_parenthesized(SPAN, function_expr)
|
ctx.ast.expression_parenthesized(SPAN, function_expr)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue