Skip to content

Commit

Permalink
fix(transformer): fix TS annotation transform scopes (#3816)
Browse files Browse the repository at this point in the history
Create symbol references for `IdentifierReferences` created in TS annotations transform.
  • Loading branch information
overlookmotel committed Jun 22, 2024
1 parent 2cdb34f commit 6dcc3f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ impl<'a> Traverse<'a> for Transformer<'a> {
fn enter_method_definition(
&mut self,
def: &mut MethodDefinition<'a>,
_ctx: &mut TraverseCtx<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x0_typescript.transform_method_definition(def);
self.x0_typescript.transform_method_definition(def, ctx);
}

fn exit_method_definition(
Expand Down
25 changes: 19 additions & 6 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::rc::Rc;
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_span::{Atom, GetSpan, SPAN};
use oxc_syntax::operator::AssignmentOperator;
use oxc_syntax::{operator::AssignmentOperator, reference::ReferenceFlag};
use oxc_traverse::TraverseCtx;
use rustc_hash::FxHashSet;

Expand Down Expand Up @@ -61,9 +61,18 @@ impl<'a> TypeScriptAnnotations<'a> {
}

// Creates `this.name = name`
fn create_this_property_assignment(&self, id: &BindingIdentifier<'a>) -> Statement<'a> {
fn create_this_property_assignment(
&self,
id: &BindingIdentifier<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let ast = self.ctx.ast;

let symbol_id = id.symbol_id.get().unwrap();
let reference_id =
ctx.create_bound_reference(id.name.to_compact_str(), symbol_id, ReferenceFlag::Read);
let id = IdentifierReference::new_read(id.span, id.name.clone(), Some(reference_id));

ast.expression_statement(
SPAN,
ast.assignment_expression(
Expand All @@ -72,10 +81,10 @@ impl<'a> TypeScriptAnnotations<'a> {
ast.simple_assignment_target_member_expression(ast.static_member(
SPAN,
ast.this_expression(SPAN),
ast.identifier_name(id.span, &id.name),
IdentifierName::new(id.span, id.name.clone()),
false,
)),
ast.identifier_reference_expression(ast.identifier_reference(id.span, &id.name)),
ast.identifier_reference_expression(id),
),
)
}
Expand Down Expand Up @@ -297,14 +306,18 @@ impl<'a> TypeScriptAnnotations<'a> {
elem.type_parameters = None;
}

pub fn transform_method_definition(&mut self, def: &mut MethodDefinition<'a>) {
pub fn transform_method_definition(
&mut self,
def: &mut MethodDefinition<'a>,
ctx: &mut TraverseCtx<'a>,
) {
// Collects parameter properties so that we can add an assignment
// for each of them in the constructor body.
if def.kind == MethodDefinitionKind::Constructor {
for param in def.value.params.items.as_mut_slice() {
if param.is_public() {
if let Some(id) = param.pattern.get_binding_identifier() {
let assignment = self.create_this_property_assignment(id);
let assignment = self.create_this_property_assignment(id, ctx);
self.assignments.push(assignment);
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ impl<'a> TypeScript<'a> {
self.annotations.transform_jsx_opening_element(elem);
}

pub fn transform_method_definition(&mut self, def: &mut MethodDefinition<'a>) {
self.annotations.transform_method_definition(def);
pub fn transform_method_definition(
&mut self,
def: &mut MethodDefinition<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.annotations.transform_method_definition(def, ctx);
}

pub fn transform_method_definition_on_exit(&mut self, def: &mut MethodDefinition<'a>) {
Expand Down

0 comments on commit 6dcc3f4

Please sign in to comment.