From 41a0e60c3ec7a04fd31600f957ef70a3fbc49849 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 19 Nov 2024 01:05:30 +0000 Subject: [PATCH] refactor(ast)!: remove `impl GetAddress for Function` (#7343) `impl GetAddress for Function` was added as a hack, in the absence of another way to get the `Address` of a `&Function` (https://github.com/oxc-project/backlog/issues/140). Remove it, and use `Address:from_ptr` instead in JSX Refresh transform, which is only place using it. --- crates/oxc_ast/src/ast_impl/js.rs | 10 ---------- crates/oxc_transformer/src/jsx/refresh.rs | 7 ++++--- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index 60d4f5ee9a522..c0d6a549452a3 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -1011,16 +1011,6 @@ impl<'a> Function<'a> { } } -// FIXME: This is a workaround for we can't get current address by `TraverseCtx`, -// we will remove this once we support `TraverseCtx::current_address`. -// See: -impl GetAddress for Function<'_> { - #[inline] - fn address(&self) -> Address { - Address::from_ptr(self) - } -} - impl<'a> FormalParameters<'a> { /// Number of parameters bound in this parameter list. pub fn parameters_count(&self) -> usize { diff --git a/crates/oxc_transformer/src/jsx/refresh.rs b/crates/oxc_transformer/src/jsx/refresh.rs index f55dc2b4b90b0..78962ebaf40e7 100644 --- a/crates/oxc_transformer/src/jsx/refresh.rs +++ b/crates/oxc_transformer/src/jsx/refresh.rs @@ -2,7 +2,7 @@ use base64::prelude::{Engine, BASE64_STANDARD}; use rustc_hash::FxHashMap; use sha1::{Digest, Sha1}; -use oxc_allocator::{CloneIn, GetAddress, Vec as ArenaVec}; +use oxc_allocator::{Address, CloneIn, GetAddress, Vec as ArenaVec}; use oxc_ast::{ast::*, match_expression, AstBuilder, NONE}; use oxc_semantic::{Reference, ReferenceFlags, ScopeFlags, ScopeId, SymbolFlags}; use oxc_span::{Atom, GetSpan, SPAN}; @@ -289,8 +289,9 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> { // which is a `Statement::ExportDefaultDeclaration` Ancestor::ExportDefaultDeclarationDeclaration(decl) => decl.address(), // Otherwise just a `function Foo() {}` - // which is a `Statement::FunctionDeclaration` - _ => func.address(), + // which is a `Statement::FunctionDeclaration`. + // `Function` is always stored in a `Box`, so has a stable memory address. + _ => Address::from_ptr(func), }; self.ctx.statement_injector.insert_after(&address, statement); }