Skip to content

Commit

Permalink
refactor(ast)!: remove impl GetAddress for Function (#7343)
Browse files Browse the repository at this point in the history
`impl GetAddress for Function` was added as a hack, in the absence of another way to get the `Address` of a `&Function` (oxc-project/backlog#140).

Remove it, and use `Address:from_ptr` instead in JSX Refresh transform, which is only place using it.
  • Loading branch information
overlookmotel committed Nov 19, 2024
1 parent 4b8aecc commit 41a0e60
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
10 changes: 0 additions & 10 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://github.com/oxc-project/oxc/pull/6881#discussion_r1816560516>
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 {
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_transformer/src/jsx/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 41a0e60

Please sign in to comment.