Skip to content

Commit

Permalink
hack
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jul 21, 2024
1 parent 7f4c883 commit 28e1b5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
17 changes: 7 additions & 10 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_allocator::{Box, Vec};
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_span::{CompactStr, GetSpan};
use oxc_span::GetSpan;
use oxc_syntax::{
identifier::{LS, PS},
keyword::is_reserved_keyword_or_global_object,
Expand Down Expand Up @@ -1080,9 +1080,8 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ParenthesizedExpression<'a> {
impl<'a, const MINIFY: bool> Gen<MINIFY> for IdentifierReference<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
let name = p.get_identifier_reference_name(self);
let name = CompactStr::new(name);
p.add_source_mapping_for_name(self.span, &name);
p.print_str(&name);
p.add_source_mapping_for_name(self.span, name);
p.print_str(name);
}
}

Expand All @@ -1096,9 +1095,8 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for IdentifierName<'a> {
impl<'a, const MINIFY: bool> Gen<MINIFY> for BindingIdentifier<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
let name = p.get_binding_identifier_name(self);
let name = CompactStr::new(name);
p.add_source_mapping_for_name(self.span, &name);
p.print_str(&name);
p.add_source_mapping_for_name(self.span, name);
p.print_str(name);
}
}

Expand Down Expand Up @@ -1933,16 +1931,15 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for AssignmentTargetProperty<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for AssignmentTargetPropertyIdentifier<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
let ident_name = p.get_identifier_reference_name(&self.binding);
let ident_name = p.get_identifier_reference_name(&self.binding).to_owned();
if ident_name == self.binding.name.as_str() {
self.binding.gen(p, ctx);
} else {
// `({x: a} = y);`
let ident_name = CompactStr::new(ident_name);
p.print_str(self.binding.name.as_str());
p.print_colon();
p.print_soft_space();
p.print_str(ident_name.as_str());
p.print_str(&ident_name);
}
if let Some(expr) = &self.init {
p.print_soft_space();
Expand Down
10 changes: 6 additions & 4 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,22 +427,24 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
}
}

fn get_identifier_reference_name(&self, reference: &IdentifierReference<'a>) -> &str {
fn get_identifier_reference_name(&self, reference: &IdentifierReference<'a>) -> &'a str {
if let Some(mangler) = &self.mangler {
if let Some(reference_id) = reference.reference_id.get() {
if let Some(name) = mangler.get_reference_name(reference_id) {
return name;
// SAFETY: Hack the lifetime to be part of the allocator.
return unsafe { std::mem::transmute_copy(&name) };
}
}
}
reference.name.as_str()
}

fn get_binding_identifier_name(&self, ident: &BindingIdentifier<'a>) -> &str {
fn get_binding_identifier_name(&self, ident: &BindingIdentifier<'a>) -> &'a str {
if let Some(mangler) = &self.mangler {
if let Some(symbol_id) = ident.symbol_id.get() {
let name = mangler.get_symbol_name(symbol_id);
return name;
// SAFETY: Hack the lifetime to be part of the allocator.
return unsafe { std::mem::transmute_copy(&name) };
}
}
ident.name.as_str()
Expand Down

0 comments on commit 28e1b5d

Please sign in to comment.