Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions crates/oxc_transformer/src/decorator/legacy/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
/// * TypeScript's [emitDecoratorMetadata](https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata)
use oxc_allocator::{Box as ArenaBox, TakeIn};
use oxc_ast::ast::*;
use oxc_semantic::ReferenceFlags;
use oxc_semantic::{Reference, ReferenceFlags};
use oxc_span::{ContentEq, SPAN};
use oxc_traverse::{MaybeBoundIdentifier, Traverse, TraverseCtx};

Expand Down Expand Up @@ -362,16 +362,18 @@ impl<'a> LegacyDecoratorMetadata<'a, '_> {
// `A` -> `typeof A !== "undefined" && A`
TSTypeName::IdentifierReference(ident) => {
let binding = MaybeBoundIdentifier::from_identifier_reference(ident, ctx);
let ident1 = binding.create_read_expression(ctx);
let ident2 = binding.create_read_expression(ctx);
let flags = Self::get_reference_flags(&binding, ctx);
let ident1 = binding.create_expression(flags, ctx);
let ident2 = binding.create_expression(flags, ctx);
Self::create_checked_value(ident1, ident2, ctx)
}
TSTypeName::QualifiedName(qualified) => {
if let TSTypeName::IdentifierReference(ident) = &qualified.left {
// `A.B` -> `typeof A !== "undefined" && A.B`
let binding = MaybeBoundIdentifier::from_identifier_reference(ident, ctx);
let ident1 = binding.create_read_expression(ctx);
let ident2 = binding.create_read_expression(ctx);
let flags = Self::get_reference_flags(&binding, ctx);
let ident1 = binding.create_expression(flags, ctx);
let ident2 = binding.create_expression(flags, ctx);
let member = create_property_access(SPAN, ident1, &qualified.right.name, ctx);
Self::create_checked_value(ident2, member, ctx)
} else {
Expand Down Expand Up @@ -494,6 +496,29 @@ impl<'a> LegacyDecoratorMetadata<'a, '_> {
a.content_eq(b)
}

fn get_reference_flags(
binding: &MaybeBoundIdentifier<'a>,
ctx: &TraverseCtx<'a>,
) -> ReferenceFlags {
if let Some(symbol_id) = binding.symbol_id {
if ctx.scoping().get_resolved_references(symbol_id).any(Reference::is_value) {
// If the symbol has a value reference, we can use it as a value reference
ReferenceFlags::Read
} else {
// Otherwise, we can only use it as a type reference because the TypeScript plugin will
// remove imports by checking references of `Symbol` whether it contains a
// `ReferenceFlags::Read`. If it doesn't, that means the symbol can be removed. That's
// why we need to use `Type` flag here, which hints the TypeScript plugin this Reference
// is only used as a type reference. If no `ReferenceFlags::Read` is found, we can
// safely remove the import.
ReferenceFlags::Type
}
} else {
// Unresolved reference
ReferenceFlags::Type | ReferenceFlags::Read
}
}

#[inline]
fn create_global_identifier(ident: &'static str, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
ctx.create_unbound_ident_expr(SPAN, Atom::new_const(ident), ReferenceFlags::Read)
Expand Down
Loading
Loading