Skip to content

Commit

Permalink
refactor(isolated_declarations): use aliases ArenaBox / ArenaVec
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Feb 26, 2025
1 parent a925498 commit 032edce
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 44 deletions.
16 changes: 8 additions & 8 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use oxc_allocator::{Box, CloneIn};
use oxc_allocator::{Box as ArenaBox, CloneIn, Vec as ArenaVec};
use oxc_ast::{NONE, ast::*};
use oxc_span::{GetSpan, SPAN};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -133,8 +133,8 @@ impl<'a> IsolatedDeclarations<'a> {
fn transform_class_method_definition(
&self,
definition: &MethodDefinition<'a>,
params: Box<'a, FormalParameters<'a>>,
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
params: ArenaBox<'a, FormalParameters<'a>>,
return_type: Option<ArenaBox<'a, TSTypeAnnotation<'a>>>,
) -> ClassElement<'a> {
let function = &definition.value;

Expand Down Expand Up @@ -197,7 +197,7 @@ impl<'a> IsolatedDeclarations<'a> {
fn transform_formal_parameter_to_class_property(
&self,
param: &FormalParameter<'a>,
type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
type_annotation: Option<ArenaBox<'a, TSTypeAnnotation<'a>>>,
) -> Option<ClassElement<'a>> {
let Some(ident_name) = param.pattern.get_identifier_name() else {
// A parameter property may not be declared using a binding pattern.(1187)
Expand Down Expand Up @@ -264,7 +264,7 @@ impl<'a> IsolatedDeclarations<'a> {
&self,
function: &Function<'a>,
params: &FormalParameters<'a>,
) -> oxc_allocator::Vec<'a, ClassElement<'a>> {
) -> ArenaVec<'a, ClassElement<'a>> {
let mut elements = self.ast.vec();
for (index, param) in function.params.items.iter().enumerate() {
if param.accessibility.is_some() || param.readonly {
Expand Down Expand Up @@ -299,7 +299,7 @@ impl<'a> IsolatedDeclarations<'a> {
fn collect_getter_or_setter_annotations(
&self,
decl: &Class<'a>,
) -> FxHashMap<Cow<str>, Box<'a, TSTypeAnnotation<'a>>> {
) -> FxHashMap<Cow<str>, ArenaBox<'a, TSTypeAnnotation<'a>>> {
let mut method_annotations = FxHashMap::default();
for element in &decl.body.body {
if let ClassElement::MethodDefinition(method) = element {
Expand Down Expand Up @@ -347,7 +347,7 @@ impl<'a> IsolatedDeclarations<'a> {
&self,
decl: &Class<'a>,
declare: Option<bool>,
) -> Box<'a, Class<'a>> {
) -> ArenaBox<'a, Class<'a>> {
if let Some(super_class) = &decl.super_class {
let is_not_allowed = match super_class {
Expression::Identifier(_) => false,
Expand Down Expand Up @@ -569,7 +569,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn create_formal_parameters(
&self,
kind: BindingPatternKind<'a>,
) -> Box<'a, FormalParameters<'a>> {
) -> ArenaBox<'a, FormalParameters<'a>> {
let pattern = self.ast.binding_pattern(kind, NONE, false);
let parameter =
self.ast.formal_parameter(SPAN, self.ast.vec(), pattern, None, false, false);
Expand Down
18 changes: 9 additions & 9 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::Cell;

use oxc_allocator::{Box, CloneIn, Vec};
use oxc_allocator::{Box as ArenaBox, CloneIn, Vec as ArenaVec};
use oxc_ast::{Visit, VisitMut, ast::*, visit::walk_mut::walk_ts_signatures};
use oxc_ecmascript::BoundNames;
use oxc_span::{GetSpan, SPAN};
Expand All @@ -20,7 +20,7 @@ impl<'a> IsolatedDeclarations<'a> {
&self,
decl: &VariableDeclaration<'a>,
check_binding: bool,
) -> Option<Box<'a, VariableDeclaration<'a>>> {
) -> Option<ArenaBox<'a, VariableDeclaration<'a>>> {
if decl.declare {
None
} else {
Expand All @@ -35,8 +35,8 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn transform_variable_declaration_with_new_declarations(
&self,
decl: &VariableDeclaration<'a>,
declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>,
) -> Box<'a, VariableDeclaration<'a>> {
declarations: ArenaVec<'a, VariableDeclarator<'a>>,
) -> ArenaBox<'a, VariableDeclaration<'a>> {
self.ast.alloc_variable_declaration(decl.span, decl.kind, declarations, self.is_declare())
}

Expand Down Expand Up @@ -104,8 +104,8 @@ impl<'a> IsolatedDeclarations<'a> {

fn transform_ts_module_block(
&mut self,
block: &Box<'a, TSModuleBlock<'a>>,
) -> Box<'a, TSModuleBlock<'a>> {
block: &ArenaBox<'a, TSModuleBlock<'a>>,
) -> ArenaBox<'a, TSModuleBlock<'a>> {
// We need to enter a new scope for the module block, avoid add binding to the parent scope
// TODO: doesn't have a scope_id!
self.scope.enter_scope(ScopeFlags::TsModuleBlock, &Cell::default());
Expand All @@ -116,8 +116,8 @@ impl<'a> IsolatedDeclarations<'a> {

pub(crate) fn transform_ts_module_declaration(
&mut self,
decl: &Box<'a, TSModuleDeclaration<'a>>,
) -> Box<'a, TSModuleDeclaration<'a>> {
decl: &ArenaBox<'a, TSModuleDeclaration<'a>>,
) -> ArenaBox<'a, TSModuleDeclaration<'a>> {
if decl.declare {
return decl.clone_in(self.ast.allocator);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

impl<'a> VisitMut<'a> for IsolatedDeclarations<'a> {
fn visit_ts_signatures(&mut self, signatures: &mut Vec<'a, TSSignature<'a>>) {
fn visit_ts_signatures(&mut self, signatures: &mut ArenaVec<'a, TSSignature<'a>>) {
self.transform_ts_signatures(signatures);
walk_ts_signatures(self, signatures);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::{Box, CloneIn};
use oxc_allocator::{Box as ArenaBox, CloneIn};
use oxc_ast::{NONE, ast::*};
use oxc_span::{SPAN, Span};

Expand All @@ -16,7 +16,7 @@ impl<'a> IsolatedDeclarations<'a> {
&self,
func: &Function<'a>,
declare: Option<bool>,
) -> Box<'a, Function<'a>> {
) -> ArenaBox<'a, Function<'a>> {
let return_type = self.infer_function_return_type(func);
if return_type.is_none() {
self.error(function_must_have_explicit_return_type(get_function_span(func)));
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn transform_formal_parameters(
&self,
params: &FormalParameters<'a>,
) -> Box<'a, FormalParameters<'a>> {
) -> ArenaBox<'a, FormalParameters<'a>> {
if params.kind.is_signature() || (params.rest.is_none() && params.items.is_empty()) {
return self.ast.alloc(params.clone_in(self.ast.allocator));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_isolated_declarations/src/inferrer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::{Box, CloneIn};
use oxc_allocator::{Box as ArenaBox, CloneIn};
use oxc_ast::ast::{
ArrowFunctionExpression, BindingPatternKind, Expression, FormalParameter, Function, Statement,
TSType, TSTypeAnnotation, UnaryExpression,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn infer_function_return_type(
&self,
function: &Function<'a>,
) -> Option<Box<'a, TSTypeAnnotation<'a>>> {
) -> Option<ArenaBox<'a, TSTypeAnnotation<'a>>> {
if function.return_type.is_some() {
return function.return_type.clone_in(self.ast.allocator);
}
Expand All @@ -119,7 +119,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn infer_arrow_function_return_type(
&self,
function: &ArrowFunctionExpression<'a>,
) -> Option<Box<'a, TSTypeAnnotation<'a>>> {
) -> Option<ArenaBox<'a, TSTypeAnnotation<'a>>> {
if function.return_type.is_some() {
return function.return_type.clone_in(self.ast.allocator);
}
Expand Down
23 changes: 9 additions & 14 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{cell::RefCell, mem};

use rustc_hash::{FxHashMap, FxHashSet};

use oxc_allocator::{Allocator, CloneIn};
use oxc_allocator::{Allocator, CloneIn, Vec as ArenaVec};
use oxc_ast::{AstBuilder, NONE, Visit, ast::*};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Atom, GetSpan, SPAN, SourceType};
Expand Down Expand Up @@ -131,10 +131,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

impl<'a> IsolatedDeclarations<'a> {
fn transform_program(
&mut self,
program: &Program<'a>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
fn transform_program(&mut self, program: &Program<'a>) -> ArenaVec<'a, Statement<'a>> {
let has_import_or_export = program.body.iter().any(Statement::is_module_declaration);

if has_import_or_export {
Expand All @@ -146,8 +143,8 @@ impl<'a> IsolatedDeclarations<'a> {

fn transform_program_without_module_declaration(
&mut self,
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
stmts: &ArenaVec<'a, Statement<'a>>,
) -> ArenaVec<'a, Statement<'a>> {
self.report_error_for_expando_function(stmts);

let mut stmts =
Expand All @@ -168,8 +165,8 @@ impl<'a> IsolatedDeclarations<'a> {

fn transform_statements_on_demand(
&mut self,
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
stmts: &ArenaVec<'a, Statement<'a>>,
) -> ArenaVec<'a, Statement<'a>> {
self.report_error_for_expando_function(stmts);

let mut stmts = self.ast.vec_from_iter(stmts.iter().filter(|stmt| {
Expand Down Expand Up @@ -426,9 +423,7 @@ impl<'a> IsolatedDeclarations<'a> {
new_stm
}

fn remove_function_overloads_implementation(
stmts: &mut oxc_allocator::Vec<'a, &Statement<'a>>,
) {
fn remove_function_overloads_implementation(stmts: &mut ArenaVec<'a, &Statement<'a>>) {
let mut last_function_name: Option<Atom<'a>> = None;
let mut is_export_default_function_overloads = false;

Expand Down Expand Up @@ -494,7 +489,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

fn get_assignable_properties_for_namespaces(
stmts: &'a oxc_allocator::Vec<'a, Statement<'a>>,
stmts: &'a ArenaVec<'a, Statement<'a>>,
) -> FxHashMap<&'a str, FxHashSet<Atom<'a>>> {
let mut assignable_properties_for_namespace = FxHashMap::<&str, FxHashSet<Atom>>::default();
for stmt in stmts {
Expand Down Expand Up @@ -559,7 +554,7 @@ impl<'a> IsolatedDeclarations<'a> {
assignable_properties_for_namespace
}

fn report_error_for_expando_function(&self, stmts: &oxc_allocator::Vec<'a, Statement<'a>>) {
fn report_error_for_expando_function(&self, stmts: &ArenaVec<'a, Statement<'a>>) {
let assignable_properties_for_namespace =
IsolatedDeclarations::get_assignable_properties_for_namespaces(stmts);

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/literal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::Box;
use oxc_allocator::Box as ArenaBox;
use oxc_ast::ast::{StringLiteral, TemplateLiteral};

use crate::IsolatedDeclarations;
Expand All @@ -7,7 +7,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn transform_template_to_string(
&self,
lit: &TemplateLiteral<'a>,
) -> Option<Box<'a, StringLiteral<'a>>> {
) -> Option<ArenaBox<'a, StringLiteral<'a>>> {
if lit.expressions.is_empty() {
lit.quasis.first().map(|item| {
self.ast.alloc(self.ast.string_literal(
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::{Box, CloneIn, Vec};
use oxc_allocator::{Box as ArenaBox, CloneIn, Vec as ArenaVec};
use oxc_ast::{NONE, ast::*};
use oxc_span::{Atom, GetSpan, SPAN};

Expand Down Expand Up @@ -116,7 +116,7 @@ impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn transform_import_declaration(
&self,
decl: &ImportDeclaration<'a>,
) -> Option<Box<'a, ImportDeclaration<'a>>> {
) -> Option<ArenaBox<'a, ImportDeclaration<'a>>> {
let specifiers = decl.specifiers.as_ref()?;

let mut new_specifiers = self.ast.vec_with_capacity(specifiers.len());
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<'a> IsolatedDeclarations<'a> {
/// const a = 1;
/// function b() {}
/// ```
pub(crate) fn strip_export_keyword(&self, stmts: &mut Vec<'a, Statement<'a>>) {
pub(crate) fn strip_export_keyword(&self, stmts: &mut ArenaVec<'a, Statement<'a>>) {
stmts.iter_mut().for_each(|stmt| {
if let Statement::ExportNamedDeclaration(decl) = stmt {
if let Some(declaration) = &mut decl.declaration {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/signatures.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashMap;

use oxc_allocator::{CloneIn, Vec};
use oxc_allocator::{CloneIn, Vec as ArenaVec};
use oxc_ast::ast::{TSMethodSignatureKind, TSSignature};
use oxc_span::GetSpan;

Expand All @@ -11,7 +11,7 @@ impl<'a> IsolatedDeclarations<'a> {
///
/// Infer get accessor return type from set accessor's param type
/// Infer set accessor parameter type from get accessor return type
pub fn transform_ts_signatures(&mut self, signatures: &mut Vec<'a, TSSignature<'a>>) {
pub fn transform_ts_signatures(&mut self, signatures: &mut ArenaVec<'a, TSSignature<'a>>) {
// <name, (requires_inference, first_param_annotation, return_type)>
let mut method_annotations: FxHashMap<_, (bool, _, _)> = FxHashMap::default();

Expand Down

0 comments on commit 032edce

Please sign in to comment.