mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
fix(transformer): fix TS annotation transform scopes (#3816)
Create symbol references for `IdentifierReferences` created in TS annotations transform.
This commit is contained in:
parent
2cdb34f6df
commit
6dcc3f4002
3 changed files with 27 additions and 10 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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(
|
||||
|
|
@ -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),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue