refactor(isolated-declarations): remove TransformDtsCtx (#3719)

This commit is contained in:
Boshen 2024-06-17 13:16:25 +00:00
parent 0b8098a442
commit 3c597356e4
11 changed files with 214 additions and 239 deletions

View file

@ -57,7 +57,7 @@ impl<'a> IsolatedDeclarations<'a> {
property property
.type_annotation .type_annotation
.as_ref() .as_ref()
.map(|type_annotation| self.ctx.ast.copy(type_annotation)) .map(|type_annotation| self.ast.copy(type_annotation))
.or_else(|| { .or_else(|| {
let new_type = property let new_type = property
.value .value
@ -65,17 +65,17 @@ impl<'a> IsolatedDeclarations<'a> {
.and_then(|expr| self.infer_type_from_expression(expr)) .and_then(|expr| self.infer_type_from_expression(expr))
.unwrap_or_else(|| { .unwrap_or_else(|| {
// report error for has no type annotation // report error for has no type annotation
self.ctx.ast.ts_unknown_keyword(property.span) self.ast.ts_unknown_keyword(property.span)
}); });
Some(self.ctx.ast.ts_type_annotation(SPAN, new_type)) Some(self.ast.ts_type_annotation(SPAN, new_type))
}) })
}; };
self.ctx.ast.class_property( self.ast.class_property(
property.r#type, property.r#type,
property.span, property.span,
self.ctx.ast.copy(&property.key), self.ast.copy(&property.key),
None, None,
property.computed, property.computed,
property.r#static, property.r#static,
@ -86,7 +86,7 @@ impl<'a> IsolatedDeclarations<'a> {
property.readonly, property.readonly,
type_annotations, type_annotations,
self.transform_accessibility(property.accessibility), self.transform_accessibility(property.accessibility),
self.ctx.ast.new_vec(), self.ast.new_vec(),
) )
} }
@ -107,7 +107,7 @@ impl<'a> IsolatedDeclarations<'a> {
}; };
return self.create_class_property( return self.create_class_property(
r#type, r#type,
self.ctx.ast.copy(&definition.key), self.ast.copy(&definition.key),
definition.r#override, definition.r#override,
self.transform_accessibility(definition.accessibility), self.transform_accessibility(definition.accessibility),
); );
@ -115,23 +115,23 @@ impl<'a> IsolatedDeclarations<'a> {
let type_annotation = self.infer_function_return_type(function); let type_annotation = self.infer_function_return_type(function);
let value = self.ctx.ast.function( let value = self.ast.function(
FunctionType::TSEmptyBodyFunctionExpression, FunctionType::TSEmptyBodyFunctionExpression,
function.span, function.span,
self.ctx.ast.copy(&function.id), self.ast.copy(&function.id),
function.generator, function.generator,
function.r#async, function.r#async,
self.ctx.ast.copy(&function.this_param), self.ast.copy(&function.this_param),
params, params,
None, None,
self.ctx.ast.copy(&function.type_parameters), self.ast.copy(&function.type_parameters),
type_annotation, type_annotation,
Modifiers::empty(), Modifiers::empty(),
); );
self.ctx.ast.class_method( self.ast.class_method(
definition.r#type, definition.r#type,
definition.span, definition.span,
self.ctx.ast.copy(&definition.key), self.ast.copy(&definition.key),
definition.kind, definition.kind,
value, value,
definition.computed, definition.computed,
@ -139,7 +139,7 @@ impl<'a> IsolatedDeclarations<'a> {
definition.r#override, definition.r#override,
definition.optional, definition.optional,
self.transform_accessibility(definition.accessibility), self.transform_accessibility(definition.accessibility),
self.ctx.ast.new_vec(), self.ast.new_vec(),
) )
} }
@ -150,7 +150,7 @@ impl<'a> IsolatedDeclarations<'a> {
r#override: bool, r#override: bool,
accessibility: Option<TSAccessibility>, accessibility: Option<TSAccessibility>,
) -> ClassElement<'a> { ) -> ClassElement<'a> {
self.ctx.ast.class_property( self.ast.class_property(
r#type, r#type,
SPAN, SPAN,
key, key,
@ -164,7 +164,7 @@ impl<'a> IsolatedDeclarations<'a> {
false, false,
None, None,
accessibility, accessibility,
self.ctx.ast.new_vec(), self.ast.new_vec(),
) )
} }
@ -177,9 +177,8 @@ impl<'a> IsolatedDeclarations<'a> {
// A parameter property may not be declared using a binding pattern.(1187) // A parameter property may not be declared using a binding pattern.(1187)
return None; return None;
}; };
let key = let key = self.ast.property_key_identifier(IdentifierName::new(SPAN, ident_name.clone()));
self.ctx.ast.property_key_identifier(IdentifierName::new(SPAN, ident_name.clone())); Some(self.ast.class_property(
Some(self.ctx.ast.class_property(
PropertyDefinitionType::PropertyDefinition, PropertyDefinitionType::PropertyDefinition,
param.span, param.span,
key, key,
@ -193,7 +192,7 @@ impl<'a> IsolatedDeclarations<'a> {
param.readonly, param.readonly,
type_annotation, type_annotation,
self.transform_accessibility(param.accessibility), self.transform_accessibility(param.accessibility),
self.ctx.ast.new_vec(), self.ast.new_vec(),
)) ))
} }
@ -211,11 +210,11 @@ impl<'a> IsolatedDeclarations<'a> {
_ => true, _ => true,
}; };
if is_not_allowed { if is_not_allowed {
self.ctx.error(extends_clause_expression(super_class.span())); self.error(extends_clause_expression(super_class.span()));
} }
} }
let mut elements = self.ctx.ast.new_vec(); let mut elements = self.ast.new_vec();
let mut has_private_key = false; let mut has_private_key = false;
for element in &decl.body.body { for element in &decl.body.body {
match element { match element {
@ -237,7 +236,7 @@ impl<'a> IsolatedDeclarations<'a> {
if param.accessibility.is_some() { if param.accessibility.is_some() {
// transformed params will definitely have type annotation // transformed params will definitely have type annotation
let type_annotation = let type_annotation =
self.ctx.ast.copy(&params.items[index].pattern.type_annotation); self.ast.copy(&params.items[index].pattern.type_annotation);
if let Some(new_element) = self if let Some(new_element) = self
.transform_formal_parameter_to_class_property( .transform_formal_parameter_to_class_property(
param, param,
@ -275,18 +274,18 @@ impl<'a> IsolatedDeclarations<'a> {
} }
// FIXME: missing many fields // FIXME: missing many fields
let new_element = self.ctx.ast.accessor_property( let new_element = self.ast.accessor_property(
property.r#type, property.r#type,
property.span, property.span,
self.ctx.ast.copy(&property.key), self.ast.copy(&property.key),
None, None,
property.computed, property.computed,
property.r#static, property.r#static,
self.ctx.ast.new_vec(), self.ast.new_vec(),
); );
elements.push(new_element); elements.push(new_element);
} }
ClassElement::TSIndexSignature(_) => elements.push(self.ctx.ast.copy(element)), ClassElement::TSIndexSignature(_) => elements.push(self.ast.copy(element)),
} }
} }
@ -295,12 +294,11 @@ impl<'a> IsolatedDeclarations<'a> {
// When the class has at least one private identifier, create a unique constant identifier to retain the nominal typing behavior // When the class has at least one private identifier, create a unique constant identifier to retain the nominal typing behavior
// Prevents other classes with the same public members from being used in place of the current class // Prevents other classes with the same public members from being used in place of the current class
let ident = self let ident = self
.ctx
.ast .ast
.property_key_private_identifier(PrivateIdentifier::new(SPAN, "private".into())); .property_key_private_identifier(PrivateIdentifier::new(SPAN, "private".into()));
let r#type = PropertyDefinitionType::PropertyDefinition; let r#type = PropertyDefinitionType::PropertyDefinition;
let decorators = self.ctx.ast.new_vec(); let decorators = self.ast.new_vec();
let new_element = self.ctx.ast.class_property( let new_element = self.ast.class_property(
r#type, SPAN, ident, None, false, false, false, false, false, false, false, None, r#type, SPAN, ident, None, false, false, false, false, false, false, false, None,
None, decorators, None, decorators,
); );
@ -308,23 +306,23 @@ impl<'a> IsolatedDeclarations<'a> {
elements.insert(0, new_element); elements.insert(0, new_element);
} }
let body = self.ctx.ast.class_body(decl.body.span, elements); let body = self.ast.class_body(decl.body.span, elements);
let mut modifiers = self.modifiers_declare(); let mut modifiers = self.modifiers_declare();
if decl.modifiers.is_contains_abstract() { if decl.modifiers.is_contains_abstract() {
modifiers.add_modifier(Modifier { span: SPAN, kind: ModifierKind::Abstract }); modifiers.add_modifier(Modifier { span: SPAN, kind: ModifierKind::Abstract });
}; };
Some(self.ctx.ast.class( Some(self.ast.class(
decl.r#type, decl.r#type,
decl.span, decl.span,
self.ctx.ast.copy(&decl.id), self.ast.copy(&decl.id),
self.ctx.ast.copy(&decl.super_class), self.ast.copy(&decl.super_class),
body, body,
self.ctx.ast.copy(&decl.type_parameters), self.ast.copy(&decl.type_parameters),
self.ctx.ast.copy(&decl.super_type_parameters), self.ast.copy(&decl.super_type_parameters),
self.ctx.ast.copy(&decl.implements), self.ast.copy(&decl.implements),
self.ctx.ast.new_vec(), self.ast.new_vec(),
modifiers, modifiers,
)) ))
} }

View file

@ -1,27 +0,0 @@
use std::{cell::RefCell, mem, rc::Rc};
use oxc_allocator::Allocator;
use oxc_ast::AstBuilder;
use oxc_diagnostics::OxcDiagnostic;
pub type Ctx<'a> = Rc<TransformDtsCtx<'a>>;
pub struct TransformDtsCtx<'a> {
errors: RefCell<Vec<OxcDiagnostic>>,
pub ast: AstBuilder<'a>,
}
impl<'a> TransformDtsCtx<'a> {
pub fn new(allocator: &'a Allocator) -> Self {
Self { errors: RefCell::new(vec![]), ast: AstBuilder::new(allocator) }
}
pub fn take_errors(&self) -> Vec<OxcDiagnostic> {
mem::take(&mut self.errors.borrow_mut())
}
/// Add an Error
pub fn error(&self, error: OxcDiagnostic) {
self.errors.borrow_mut().push(error);
}
}

View file

@ -19,7 +19,7 @@ impl<'a> IsolatedDeclarations<'a> {
None None
} else { } else {
let declarations = let declarations =
self.ctx.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| { self.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
self.transform_variable_declarator(declarator, check_binding) self.transform_variable_declarator(declarator, check_binding)
})); }));
Some(self.transform_variable_declaration_with_new_declarations(decl, declarations)) Some(self.transform_variable_declaration_with_new_declarations(decl, declarations))
@ -31,10 +31,10 @@ impl<'a> IsolatedDeclarations<'a> {
decl: &VariableDeclaration<'a>, decl: &VariableDeclaration<'a>,
declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>, declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>,
) -> Box<'a, VariableDeclaration<'a>> { ) -> Box<'a, VariableDeclaration<'a>> {
self.ctx.ast.variable_declaration( self.ast.variable_declaration(
decl.span, decl.span,
decl.kind, decl.kind,
self.ctx.ast.new_vec_from_iter(declarations), self.ast.new_vec_from_iter(declarations),
self.modifiers_declare(), self.modifiers_declare(),
) )
} }
@ -45,7 +45,7 @@ impl<'a> IsolatedDeclarations<'a> {
check_binding: bool, check_binding: bool,
) -> Option<VariableDeclarator<'a>> { ) -> Option<VariableDeclarator<'a>> {
if decl.id.kind.is_destructuring_pattern() { if decl.id.kind.is_destructuring_pattern() {
self.ctx.error(OxcDiagnostic::error( self.error(OxcDiagnostic::error(
"Binding elements can't be exported directly with --isolatedDeclarations.", "Binding elements can't be exported directly with --isolatedDeclarations.",
)); ));
return None; return None;
@ -65,32 +65,32 @@ impl<'a> IsolatedDeclarations<'a> {
if let Some(init_expr) = &decl.init { if let Some(init_expr) = &decl.init {
// if kind is const and it doesn't need to infer type from expression // if kind is const and it doesn't need to infer type from expression
if decl.kind.is_const() && !Self::is_need_to_infer_type_from_expression(init_expr) { if decl.kind.is_const() && !Self::is_need_to_infer_type_from_expression(init_expr) {
init = Some(self.ctx.ast.copy(init_expr)); init = Some(self.ast.copy(init_expr));
} else { } else {
// otherwise, we need to infer type from expression // otherwise, we need to infer type from expression
binding_type = self.infer_type_from_expression(init_expr); binding_type = self.infer_type_from_expression(init_expr);
} }
} }
if init.is_none() && binding_type.is_none() { if init.is_none() && binding_type.is_none() {
binding_type = Some(self.ctx.ast.ts_unknown_keyword(SPAN)); binding_type = Some(self.ast.ts_unknown_keyword(SPAN));
self.ctx.error( self.error(
OxcDiagnostic::error("Variable must have an explicit type annotation with --isolatedDeclarations.") OxcDiagnostic::error("Variable must have an explicit type annotation with --isolatedDeclarations.")
.with_label(decl.id.span()), .with_label(decl.id.span()),
); );
} }
} }
let id = binding_type.map_or_else( let id = binding_type.map_or_else(
|| self.ctx.ast.copy(&decl.id), || self.ast.copy(&decl.id),
|ts_type| { |ts_type| {
self.ctx.ast.binding_pattern( self.ast.binding_pattern(
self.ctx.ast.copy(&decl.id.kind), self.ast.copy(&decl.id.kind),
Some(self.ctx.ast.ts_type_annotation(SPAN, ts_type)), Some(self.ast.ts_type_annotation(SPAN, ts_type)),
decl.id.optional, decl.id.optional,
) )
}, },
); );
Some(self.ctx.ast.variable_declarator(decl.span, decl.kind, id, init, decl.definite)) Some(self.ast.variable_declarator(decl.span, decl.kind, id, init, decl.definite))
} }
pub fn transform_using_declaration( pub fn transform_using_declaration(
@ -99,7 +99,7 @@ impl<'a> IsolatedDeclarations<'a> {
check_binding: bool, check_binding: bool,
) -> Box<'a, VariableDeclaration<'a>> { ) -> Box<'a, VariableDeclaration<'a>> {
let declarations = let declarations =
self.ctx.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| { self.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
self.transform_variable_declarator(declarator, check_binding) self.transform_variable_declarator(declarator, check_binding)
})); }));
self.transform_using_declaration_with_new_declarations(decl, declarations) self.transform_using_declaration_with_new_declarations(decl, declarations)
@ -110,7 +110,7 @@ impl<'a> IsolatedDeclarations<'a> {
decl: &UsingDeclaration<'a>, decl: &UsingDeclaration<'a>,
declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>, declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>,
) -> Box<'a, VariableDeclaration<'a>> { ) -> Box<'a, VariableDeclaration<'a>> {
self.ctx.ast.variable_declaration( self.ast.variable_declaration(
decl.span, decl.span,
VariableDeclarationKind::Const, VariableDeclarationKind::Const,
declarations, declarations,
@ -126,7 +126,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.scope.enter_scope(ScopeFlags::TsModuleBlock); self.scope.enter_scope(ScopeFlags::TsModuleBlock);
let stmts = self.transform_statements_on_demand(&block.body); let stmts = self.transform_statements_on_demand(&block.body);
self.scope.leave_scope(); self.scope.leave_scope();
self.ctx.ast.ts_module_block(SPAN, stmts) self.ast.ts_module_block(SPAN, stmts)
} }
pub fn transform_ts_module_declaration( pub fn transform_ts_module_declaration(
@ -134,19 +134,19 @@ impl<'a> IsolatedDeclarations<'a> {
decl: &Box<'a, TSModuleDeclaration<'a>>, decl: &Box<'a, TSModuleDeclaration<'a>>,
) -> Box<'a, TSModuleDeclaration<'a>> { ) -> Box<'a, TSModuleDeclaration<'a>> {
if decl.modifiers.is_contains_declare() { if decl.modifiers.is_contains_declare() {
return self.ctx.ast.copy(decl); return self.ast.copy(decl);
} }
let Some(body) = &decl.body else { let Some(body) = &decl.body else {
return self.ctx.ast.copy(decl); return self.ast.copy(decl);
}; };
match body { match body {
TSModuleDeclarationBody::TSModuleDeclaration(decl) => { TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
let inner = self.transform_ts_module_declaration(decl); let inner = self.transform_ts_module_declaration(decl);
return self.ctx.ast.ts_module_declaration( return self.ast.ts_module_declaration(
decl.span, decl.span,
self.ctx.ast.copy(&decl.id), self.ast.copy(&decl.id),
Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)), Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)),
decl.kind, decl.kind,
self.modifiers_declare(), self.modifiers_declare(),
@ -154,9 +154,9 @@ impl<'a> IsolatedDeclarations<'a> {
} }
TSModuleDeclarationBody::TSModuleBlock(block) => { TSModuleDeclarationBody::TSModuleBlock(block) => {
let body = self.transform_ts_module_block(block); let body = self.transform_ts_module_block(block);
return self.ctx.ast.ts_module_declaration( return self.ast.ts_module_declaration(
decl.span, decl.span,
self.ctx.ast.copy(&decl.id), self.ast.copy(&decl.id),
Some(TSModuleDeclarationBody::TSModuleBlock(body)), Some(TSModuleDeclarationBody::TSModuleBlock(body)),
decl.kind, decl.kind,
self.modifiers_declare(), self.modifiers_declare(),
@ -198,7 +198,7 @@ impl<'a> IsolatedDeclarations<'a> {
Declaration::TSTypeAliasDeclaration(alias_decl) => { Declaration::TSTypeAliasDeclaration(alias_decl) => {
self.visit_ts_type_alias_declaration(alias_decl); self.visit_ts_type_alias_declaration(alias_decl);
if !check_binding || self.scope.has_reference(&alias_decl.id.name) { if !check_binding || self.scope.has_reference(&alias_decl.id.name) {
Some(self.ctx.ast.copy(decl)) Some(self.ast.copy(decl))
} else { } else {
None None
} }
@ -206,7 +206,7 @@ impl<'a> IsolatedDeclarations<'a> {
Declaration::TSInterfaceDeclaration(interface_decl) => { Declaration::TSInterfaceDeclaration(interface_decl) => {
self.visit_ts_interface_declaration(interface_decl); self.visit_ts_interface_declaration(interface_decl);
if !check_binding || self.scope.has_reference(&interface_decl.id.name) { if !check_binding || self.scope.has_reference(&interface_decl.id.name) {
Some(self.ctx.ast.copy(decl)) Some(self.ast.copy(decl))
} else { } else {
None None
} }
@ -235,7 +235,7 @@ impl<'a> IsolatedDeclarations<'a> {
} }
Declaration::TSImportEqualsDeclaration(decl) => { Declaration::TSImportEqualsDeclaration(decl) => {
if !check_binding || self.scope.has_reference(&decl.id.name) { if !check_binding || self.scope.has_reference(&decl.id.name) {
Some(Declaration::TSImportEqualsDeclaration(self.ctx.ast.copy(decl))) Some(Declaration::TSImportEqualsDeclaration(self.ast.copy(decl)))
} else { } else {
None None
} }
@ -257,7 +257,7 @@ impl<'a> IsolatedDeclarations<'a> {
}; };
if is_not_allowed { if is_not_allowed {
self.ctx.error(signature_computed_property_name(key.span())); self.error(signature_computed_property_name(key.span()));
} }
} }
} }

View file

@ -21,7 +21,7 @@ impl<'a> IsolatedDeclarations<'a> {
&mut self, &mut self,
decl: &TSEnumDeclaration<'a>, decl: &TSEnumDeclaration<'a>,
) -> Option<Declaration<'a>> { ) -> Option<Declaration<'a>> {
let mut members = self.ctx.ast.new_vec(); let mut members = self.ast.new_vec();
let mut prev_initializer_value = Some(ConstantValue::Number(0.0)); let mut prev_initializer_value = Some(ConstantValue::Number(0.0));
let mut prev_members = FxHashMap::default(); let mut prev_members = FxHashMap::default();
for member in &decl.members { for member in &decl.members {
@ -30,7 +30,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.computed_constant_value(initializer, &decl.id.name, &prev_members); self.computed_constant_value(initializer, &decl.id.name, &prev_members);
if computed_value.is_none() { if computed_value.is_none() {
self.ctx.error(enum_member_initializers(member.id.span())); self.error(enum_member_initializers(member.id.span()));
} }
computed_value computed_value
@ -55,9 +55,9 @@ impl<'a> IsolatedDeclarations<'a> {
prev_members.insert(member_name.clone(), value.clone()); prev_members.insert(member_name.clone(), value.clone());
} }
let member = self.ctx.ast.ts_enum_member( let member = self.ast.ts_enum_member(
member.span, member.span,
self.ctx.ast.copy(&member.id), self.ast.copy(&member.id),
value.map(|v| match v { value.map(|v| match v {
ConstantValue::Number(v) => { ConstantValue::Number(v) => {
let is_negative = v < 0.0; let is_negative = v < 0.0;
@ -65,36 +65,35 @@ impl<'a> IsolatedDeclarations<'a> {
// Infinity // Infinity
let expr = if v.is_infinite() { let expr = if v.is_infinite() {
let ident = let ident =
IdentifierReference::new(SPAN, self.ctx.ast.new_atom("Infinity")); IdentifierReference::new(SPAN, self.ast.new_atom("Infinity"));
self.ctx.ast.identifier_reference_expression(ident) self.ast.identifier_reference_expression(ident)
} else { } else {
let value = if is_negative { -v } else { v }; let value = if is_negative { -v } else { v };
self.ctx.ast.literal_number_expression(NumericLiteral { self.ast.literal_number_expression(NumericLiteral {
span: SPAN, span: SPAN,
value, value,
raw: self.ctx.ast.new_str(&value.to_string()), raw: self.ast.new_str(&value.to_string()),
base: NumberBase::Decimal, base: NumberBase::Decimal,
}) })
}; };
if is_negative { if is_negative {
self.ctx.ast.unary_expression(SPAN, UnaryOperator::UnaryNegation, expr) self.ast.unary_expression(SPAN, UnaryOperator::UnaryNegation, expr)
} else { } else {
expr expr
} }
} }
ConstantValue::String(v) => self ConstantValue::String(v) => {
.ctx self.ast.literal_string_expression(self.ast.string_literal(SPAN, &v))
.ast }
.literal_string_expression(self.ctx.ast.string_literal(SPAN, &v)),
}), }),
); );
members.push(member); members.push(member);
} }
Some(self.ctx.ast.ts_enum_declaration( Some(self.ast.ts_enum_declaration(
decl.span, decl.span,
self.ctx.ast.copy(&decl.id), self.ast.copy(&decl.id),
members, members,
self.modifiers_declare(), self.modifiers_declare(),
)) ))

View file

@ -15,16 +15,16 @@ impl<'a> IsolatedDeclarations<'a> {
} else { } else {
let return_type = self.infer_function_return_type(func); let return_type = self.infer_function_return_type(func);
let params = self.transform_formal_parameters(&func.params); let params = self.transform_formal_parameters(&func.params);
Some(self.ctx.ast.function( Some(self.ast.function(
func.r#type, func.r#type,
func.span, func.span,
self.ctx.ast.copy(&func.id), self.ast.copy(&func.id),
func.generator, func.generator,
func.r#async, func.r#async,
self.ctx.ast.copy(&func.this_param), self.ast.copy(&func.this_param),
params, params,
None, None,
self.ctx.ast.copy(&func.type_parameters), self.ast.copy(&func.type_parameters),
return_type, return_type,
self.modifiers_declare(), self.modifiers_declare(),
)) ))
@ -39,9 +39,9 @@ impl<'a> IsolatedDeclarations<'a> {
let is_assignment_pattern = param.pattern.kind.is_assignment_pattern(); let is_assignment_pattern = param.pattern.kind.is_assignment_pattern();
let mut pattern = let mut pattern =
if let BindingPatternKind::AssignmentPattern(pattern) = &param.pattern.kind { if let BindingPatternKind::AssignmentPattern(pattern) = &param.pattern.kind {
self.ctx.ast.copy(&pattern.left) self.ast.copy(&pattern.left)
} else { } else {
self.ctx.ast.copy(&param.pattern) self.ast.copy(&param.pattern)
}; };
if is_assignment_pattern || pattern.type_annotation.is_none() { if is_assignment_pattern || pattern.type_annotation.is_none() {
@ -51,12 +51,12 @@ impl<'a> IsolatedDeclarations<'a> {
let type_annotation = pattern let type_annotation = pattern
.type_annotation .type_annotation
.as_ref() .as_ref()
.map(|type_annotation| self.ctx.ast.copy(&type_annotation.type_annotation)) .map(|type_annotation| self.ast.copy(&type_annotation.type_annotation))
.or_else(|| { .or_else(|| {
// report error for has no type annotation // report error for has no type annotation
let new_type = self let new_type = self
.infer_type_from_formal_parameter(param) .infer_type_from_formal_parameter(param)
.unwrap_or_else(|| self.ctx.ast.ts_unknown_keyword(param.span)); .unwrap_or_else(|| self.ast.ts_unknown_keyword(param.span));
Some(new_type) Some(new_type)
}) })
.map(|ts_type| { .map(|ts_type| {
@ -64,36 +64,36 @@ impl<'a> IsolatedDeclarations<'a> {
// we need to add undefined to it's type // we need to add undefined to it's type
if !is_next_param_optional { if !is_next_param_optional {
if matches!(ts_type, TSType::TSTypeReference(_)) { if matches!(ts_type, TSType::TSTypeReference(_)) {
self.ctx.error( self.error(
OxcDiagnostic::error("Declaration emit for this parameter requires implicitly adding undefined to it's type. This is not supported with --isolatedDeclarations.") OxcDiagnostic::error("Declaration emit for this parameter requires implicitly adding undefined to it's type. This is not supported with --isolatedDeclarations.")
.with_label(param.span), .with_label(param.span),
); );
} else if !ts_type.is_maybe_undefined() { } else if !ts_type.is_maybe_undefined() {
// union with undefined // union with undefined
return self.ctx.ast.ts_type_annotation(SPAN, return self.ast.ts_type_annotation(SPAN,
self.ctx.ast.ts_union_type(SPAN, self.ctx.ast.new_vec_from_iter([ts_type, self.ctx.ast.ts_undefined_keyword(SPAN)])) self.ast.ts_union_type(SPAN, self.ast.new_vec_from_iter([ts_type, self.ast.ts_undefined_keyword(SPAN)]))
); );
} }
} }
self.ctx.ast.ts_type_annotation(SPAN, ts_type) self.ast.ts_type_annotation(SPAN, ts_type)
}); });
pattern = self.ctx.ast.binding_pattern( pattern = self.ast.binding_pattern(
self.ctx.ast.copy(&pattern.kind), self.ast.copy(&pattern.kind),
type_annotation, type_annotation,
// if it's assignment pattern, it's optional // if it's assignment pattern, it's optional
pattern.optional || (is_next_param_optional && is_assignment_pattern), pattern.optional || (is_next_param_optional && is_assignment_pattern),
); );
} }
self.ctx.ast.formal_parameter( self.ast.formal_parameter(
param.span, param.span,
pattern, pattern,
None, None,
param.readonly, param.readonly,
false, false,
self.ctx.ast.new_vec(), self.ast.new_vec(),
) )
} }
@ -102,27 +102,27 @@ impl<'a> IsolatedDeclarations<'a> {
params: &FormalParameters<'a>, params: &FormalParameters<'a>,
) -> Box<'a, FormalParameters<'a>> { ) -> Box<'a, FormalParameters<'a>> {
if params.kind.is_signature() || (params.rest.is_none() && params.items.is_empty()) { if params.kind.is_signature() || (params.rest.is_none() && params.items.is_empty()) {
return self.ctx.ast.alloc(self.ctx.ast.copy(params)); return self.ast.alloc(self.ast.copy(params));
} }
let items = let items =
self.ctx.ast.new_vec_from_iter(params.items.iter().enumerate().map(|(index, item)| { self.ast.new_vec_from_iter(params.items.iter().enumerate().map(|(index, item)| {
self.transform_formal_parameter(item, params.items.get(index + 1)) self.transform_formal_parameter(item, params.items.get(index + 1))
})); }));
if let Some(rest) = &params.rest { if let Some(rest) = &params.rest {
if rest.argument.type_annotation.is_none() { if rest.argument.type_annotation.is_none() {
self.ctx.error(OxcDiagnostic::error( self.error(OxcDiagnostic::error(
"Parameter must have an explicit type annotation with --isolatedDeclarations.", "Parameter must have an explicit type annotation with --isolatedDeclarations.",
).with_label(rest.span)); ).with_label(rest.span));
} }
} }
self.ctx.ast.formal_parameters( self.ast.formal_parameters(
params.span, params.span,
FormalParameterKind::Signature, FormalParameterKind::Signature,
items, items,
self.ctx.ast.copy(&params.rest), self.ast.copy(&params.rest),
) )
} }
} }

View file

@ -14,23 +14,23 @@ use crate::{
impl<'a> IsolatedDeclarations<'a> { impl<'a> IsolatedDeclarations<'a> {
pub fn infer_type_from_expression(&self, expr: &Expression<'a>) -> Option<TSType<'a>> { pub fn infer_type_from_expression(&self, expr: &Expression<'a>) -> Option<TSType<'a>> {
match expr { match expr {
Expression::BooleanLiteral(_) => Some(self.ctx.ast.ts_boolean_keyword(SPAN)), Expression::BooleanLiteral(_) => Some(self.ast.ts_boolean_keyword(SPAN)),
Expression::NullLiteral(_) => Some(self.ctx.ast.ts_null_keyword(SPAN)), Expression::NullLiteral(_) => Some(self.ast.ts_null_keyword(SPAN)),
Expression::NumericLiteral(_) | Expression::BigintLiteral(_) => { Expression::NumericLiteral(_) | Expression::BigintLiteral(_) => {
Some(self.ctx.ast.ts_number_keyword(SPAN)) Some(self.ast.ts_number_keyword(SPAN))
} }
Expression::StringLiteral(_) | Expression::TemplateLiteral(_) => { Expression::StringLiteral(_) | Expression::TemplateLiteral(_) => {
Some(self.ctx.ast.ts_string_keyword(SPAN)) Some(self.ast.ts_string_keyword(SPAN))
} }
Expression::Identifier(ident) => match ident.name.as_str() { Expression::Identifier(ident) => match ident.name.as_str() {
"undefined" => Some(self.ctx.ast.ts_undefined_keyword(SPAN)), "undefined" => Some(self.ast.ts_undefined_keyword(SPAN)),
_ => None, _ => None,
}, },
Expression::FunctionExpression(func) => { Expression::FunctionExpression(func) => {
self.transform_function_to_ts_type(func).map(|x| self.ctx.ast.copy(&x)) self.transform_function_to_ts_type(func).map(|x| self.ast.copy(&x))
} }
Expression::ArrowFunctionExpression(func) => { Expression::ArrowFunctionExpression(func) => {
self.transform_arrow_function_to_ts_type(func).map(|x| self.ctx.ast.copy(&x)) self.transform_arrow_function_to_ts_type(func).map(|x| self.ast.copy(&x))
} }
Expression::ObjectExpression(expr) => { Expression::ObjectExpression(expr) => {
Some(self.transform_object_expression_to_ts_type(expr, false)) Some(self.transform_object_expression_to_ts_type(expr, false))
@ -39,11 +39,11 @@ impl<'a> IsolatedDeclarations<'a> {
if expr.type_annotation.is_const_type_reference() { if expr.type_annotation.is_const_type_reference() {
Some(self.transform_expression_to_ts_type(&expr.expression)) Some(self.transform_expression_to_ts_type(&expr.expression))
} else { } else {
Some(self.ctx.ast.copy(&expr.type_annotation)) Some(self.ast.copy(&expr.type_annotation))
} }
} }
Expression::ClassExpression(expr) => { Expression::ClassExpression(expr) => {
self.ctx.error( self.error(
OxcDiagnostic::error( OxcDiagnostic::error(
" "
Inference from class expressions is not supported with --isolatedDeclarations. Inference from class expressions is not supported with --isolatedDeclarations.
@ -51,7 +51,7 @@ impl<'a> IsolatedDeclarations<'a> {
) )
.with_label(expr.span), .with_label(expr.span),
); );
Some(self.ctx.ast.ts_unknown_keyword(SPAN)) Some(self.ast.ts_unknown_keyword(SPAN))
} }
Expression::TSNonNullExpression(expr) => { Expression::TSNonNullExpression(expr) => {
self.infer_type_from_expression(&expr.expression) self.infer_type_from_expression(&expr.expression)
@ -63,7 +63,7 @@ impl<'a> IsolatedDeclarations<'a> {
unreachable!(); unreachable!();
// infer_type_from_expression(ctx, &expr.expression) // infer_type_from_expression(ctx, &expr.expression)
} }
Expression::TSTypeAssertion(expr) => Some(self.ctx.ast.copy(&expr.type_annotation)), Expression::TSTypeAssertion(expr) => Some(self.ast.copy(&expr.type_annotation)),
_ => None, _ => None,
} }
} }
@ -73,15 +73,15 @@ impl<'a> IsolatedDeclarations<'a> {
param: &FormalParameter<'a>, param: &FormalParameter<'a>,
) -> Option<TSType<'a>> { ) -> Option<TSType<'a>> {
if param.pattern.type_annotation.is_some() { if param.pattern.type_annotation.is_some() {
param.pattern.type_annotation.as_ref().map(|x| self.ctx.ast.copy(&x.type_annotation)); param.pattern.type_annotation.as_ref().map(|x| self.ast.copy(&x.type_annotation));
} }
if let BindingPatternKind::AssignmentPattern(pattern) = &param.pattern.kind { if let BindingPatternKind::AssignmentPattern(pattern) = &param.pattern.kind {
if let Some(annotation) = pattern.left.type_annotation.as_ref() { if let Some(annotation) = pattern.left.type_annotation.as_ref() {
Some(self.ctx.ast.copy(&annotation.type_annotation)) Some(self.ast.copy(&annotation.type_annotation))
} else { } else {
if let Expression::TSAsExpression(expr) = &pattern.right { if let Expression::TSAsExpression(expr) = &pattern.right {
if !expr.type_annotation.is_keyword_or_literal() { if !expr.type_annotation.is_keyword_or_literal() {
self.ctx.error( self.error(
OxcDiagnostic::error("Parameter must have an explicit type annotation with --isolatedDeclarations.") OxcDiagnostic::error("Parameter must have an explicit type annotation with --isolatedDeclarations.")
.with_label(expr.type_annotation.span()) .with_label(expr.type_annotation.span())
); );
@ -100,11 +100,11 @@ impl<'a> IsolatedDeclarations<'a> {
function: &Function<'a>, function: &Function<'a>,
) -> Option<Box<'a, TSTypeAnnotation<'a>>> { ) -> Option<Box<'a, TSTypeAnnotation<'a>>> {
if function.return_type.is_some() { if function.return_type.is_some() {
return self.ctx.ast.copy(&function.return_type); return self.ast.copy(&function.return_type);
} }
if function.r#async || function.generator { if function.r#async || function.generator {
self.ctx.error(function_must_have_explicit_return_type(function)); self.error(function_must_have_explicit_return_type(function));
} }
let return_type = FunctionReturnType::infer( let return_type = FunctionReturnType::infer(
@ -114,12 +114,12 @@ impl<'a> IsolatedDeclarations<'a> {
.as_ref() .as_ref()
.unwrap_or_else(|| unreachable!("Only declare function can have no body")), .unwrap_or_else(|| unreachable!("Only declare function can have no body")),
) )
.map(|type_annotation| self.ctx.ast.ts_type_annotation(SPAN, type_annotation)); .map(|type_annotation| self.ast.ts_type_annotation(SPAN, type_annotation));
if return_type.is_none() { if return_type.is_none() {
self.ctx.error(function_must_have_explicit_return_type(function)); self.error(function_must_have_explicit_return_type(function));
Some(self.ctx.ast.ts_type_annotation(SPAN, self.ctx.ast.ts_unknown_keyword(SPAN))) Some(self.ast.ts_type_annotation(SPAN, self.ast.ts_unknown_keyword(SPAN)))
} else { } else {
return_type return_type
} }
@ -130,18 +130,18 @@ impl<'a> IsolatedDeclarations<'a> {
function: &ArrowFunctionExpression<'a>, function: &ArrowFunctionExpression<'a>,
) -> Option<Box<'a, TSTypeAnnotation<'a>>> { ) -> Option<Box<'a, TSTypeAnnotation<'a>>> {
if function.return_type.is_some() { if function.return_type.is_some() {
return self.ctx.ast.copy(&function.return_type); return self.ast.copy(&function.return_type);
} }
if function.expression { if function.expression {
if let Some(Statement::ExpressionStatement(stmt)) = function.body.statements.first() { if let Some(Statement::ExpressionStatement(stmt)) = function.body.statements.first() {
return self return self
.infer_type_from_expression(&stmt.expression) .infer_type_from_expression(&stmt.expression)
.map(|type_annotation| self.ctx.ast.ts_type_annotation(SPAN, type_annotation)); .map(|type_annotation| self.ast.ts_type_annotation(SPAN, type_annotation));
} }
} }
FunctionReturnType::infer(self, &function.body) FunctionReturnType::infer(self, &function.body)
.map(|type_annotation| self.ctx.ast.ts_type_annotation(SPAN, type_annotation)) .map(|type_annotation| self.ast.ts_type_annotation(SPAN, type_annotation))
} }
pub fn is_need_to_infer_type_from_expression(expr: &Expression) -> bool { pub fn is_need_to_infer_type_from_expression(expr: &Expression) -> bool {

View file

@ -6,7 +6,6 @@
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformers/declarations.ts> //! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformers/declarations.ts>
mod class; mod class;
mod context;
mod declaration; mod declaration;
mod diagnostics; mod diagnostics;
mod r#enum; mod r#enum;
@ -17,15 +16,15 @@ mod return_type;
mod scope; mod scope;
mod types; mod types;
use std::{collections::VecDeque, rc::Rc}; use std::{cell::RefCell, collections::VecDeque, mem};
use context::{Ctx, TransformDtsCtx};
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
#[allow(clippy::wildcard_imports)] #[allow(clippy::wildcard_imports)]
use oxc_ast::{ast::*, Visit}; use oxc_ast::{ast::*, AstBuilder, Visit};
use oxc_diagnostics::OxcDiagnostic; use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{SourceType, SPAN}; use oxc_span::{SourceType, SPAN};
use scope::ScopeTree;
use crate::scope::ScopeTree;
pub struct IsolatedDeclarationsReturn<'a> { pub struct IsolatedDeclarationsReturn<'a> {
pub program: Program<'a>, pub program: Program<'a>,
@ -33,14 +32,19 @@ pub struct IsolatedDeclarationsReturn<'a> {
} }
pub struct IsolatedDeclarations<'a> { pub struct IsolatedDeclarations<'a> {
ctx: Ctx<'a>, ast: AstBuilder<'a>,
// state
scope: ScopeTree<'a>, scope: ScopeTree<'a>,
errors: RefCell<Vec<OxcDiagnostic>>,
} }
impl<'a> IsolatedDeclarations<'a> { impl<'a> IsolatedDeclarations<'a> {
pub fn new(allocator: &'a Allocator) -> Self { pub fn new(allocator: &'a Allocator) -> Self {
let ctx = Rc::new(TransformDtsCtx::new(allocator)); Self {
Self { ctx, scope: ScopeTree::new(allocator) } ast: AstBuilder::new(allocator),
scope: ScopeTree::new(allocator),
errors: RefCell::new(vec![]),
}
} }
/// # Errors /// # Errors
@ -48,10 +52,19 @@ impl<'a> IsolatedDeclarations<'a> {
/// Returns `Vec<Error>` if any errors were collected during the transformation. /// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> { pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
let source_type = SourceType::default().with_module(true).with_typescript_definition(true); let source_type = SourceType::default().with_module(true).with_typescript_definition(true);
let directives = self.ctx.ast.new_vec(); let directives = self.ast.new_vec();
let stmts = self.transform_program(program); let stmts = self.transform_program(program);
let program = self.ctx.ast.program(SPAN, source_type, directives, None, stmts); let program = self.ast.program(SPAN, source_type, directives, None, stmts);
IsolatedDeclarationsReturn { program, errors: self.ctx.take_errors() } IsolatedDeclarationsReturn { program, errors: self.take_errors() }
}
fn take_errors(&self) -> Vec<OxcDiagnostic> {
mem::take(&mut self.errors.borrow_mut())
}
/// Add an Error
fn error(&self, error: OxcDiagnostic) {
self.errors.borrow_mut().push(error);
} }
} }
@ -83,7 +96,7 @@ impl<'a> IsolatedDeclarations<'a> {
Modifiers::empty() Modifiers::empty()
} else { } else {
Modifiers::new( Modifiers::new(
self.ctx.ast.new_vec_single(Modifier { span: SPAN, kind: ModifierKind::Declare }), self.ast.new_vec_single(Modifier { span: SPAN, kind: ModifierKind::Declare }),
) )
} }
} }
@ -92,13 +105,13 @@ impl<'a> IsolatedDeclarations<'a> {
&mut self, &mut self,
program: &Program<'a>, program: &Program<'a>,
) -> oxc_allocator::Vec<'a, Statement<'a>> { ) -> oxc_allocator::Vec<'a, Statement<'a>> {
let mut new_ast_stmts = self.ctx.ast.new_vec::<Statement<'a>>(); let mut new_ast_stmts = self.ast.new_vec::<Statement<'a>>();
for stmt in &program.body { for stmt in &program.body {
if let Some(decl) = stmt.as_declaration() { if let Some(decl) = stmt.as_declaration() {
if let Some(decl) = self.transform_declaration(decl, false) { if let Some(decl) = self.transform_declaration(decl, false) {
new_ast_stmts.push(Statement::from(decl)); new_ast_stmts.push(Statement::from(decl));
} else { } else {
new_ast_stmts.push(Statement::from(self.ctx.ast.copy(decl))); new_ast_stmts.push(Statement::from(self.ast.copy(decl)));
} }
} }
} }
@ -122,19 +135,19 @@ impl<'a> IsolatedDeclarations<'a> {
match stmt.to_declaration() { match stmt.to_declaration() {
Declaration::VariableDeclaration(decl) => { Declaration::VariableDeclaration(decl) => {
variables_declarations.push_back( variables_declarations.push_back(
self.ctx.ast.copy(&decl.declarations).into_iter().collect::<Vec<_>>(), self.ast.copy(&decl.declarations).into_iter().collect::<Vec<_>>(),
); );
variable_transformed_indexes.push_back(Vec::default()); variable_transformed_indexes.push_back(Vec::default());
} }
Declaration::UsingDeclaration(decl) => { Declaration::UsingDeclaration(decl) => {
variables_declarations.push_back( variables_declarations.push_back(
self.ctx.ast.copy(&decl.declarations).into_iter().collect::<Vec<_>>(), self.ast.copy(&decl.declarations).into_iter().collect::<Vec<_>>(),
); );
variable_transformed_indexes.push_back(Vec::default()); variable_transformed_indexes.push_back(Vec::default());
} }
_ => {} _ => {}
} }
new_stmts.push(self.ctx.ast.copy(stmt)); new_stmts.push(self.ast.copy(stmt));
} }
match_module_declaration!(Statement) => { match_module_declaration!(Statement) => {
transformed_indexes.push(new_stmts.len()); transformed_indexes.push(new_stmts.len());
@ -145,15 +158,14 @@ impl<'a> IsolatedDeclarations<'a> {
{ {
if let Some(var_decl) = var_decl { if let Some(var_decl) = var_decl {
self.scope.visit_variable_declaration(&var_decl); self.scope.visit_variable_declaration(&var_decl);
new_stmts.push(Statement::VariableDeclaration( new_stmts
self.ctx.ast.alloc(var_decl), .push(Statement::VariableDeclaration(self.ast.alloc(var_decl)));
));
transformed_indexes.push(new_stmts.len()); transformed_indexes.push(new_stmts.len());
} }
self.scope.visit_export_default_declaration(&new_decl); self.scope.visit_export_default_declaration(&new_decl);
new_stmts.push(Statement::ExportDefaultDeclaration( new_stmts.push(Statement::ExportDefaultDeclaration(
self.ctx.ast.alloc(new_decl), self.ast.alloc(new_decl),
)); ));
return; return;
} }
@ -166,9 +178,8 @@ impl<'a> IsolatedDeclarations<'a> {
new_decl.declaration.as_ref().unwrap_or_else(|| unreachable!()), new_decl.declaration.as_ref().unwrap_or_else(|| unreachable!()),
); );
new_stmts.push(Statement::ExportNamedDeclaration( new_stmts
self.ctx.ast.alloc(new_decl), .push(Statement::ExportNamedDeclaration(self.ast.alloc(new_decl)));
));
return; return;
} }
@ -177,7 +188,7 @@ impl<'a> IsolatedDeclarations<'a> {
module_declaration => self.scope.visit_module_declaration(module_declaration), module_declaration => self.scope.visit_module_declaration(module_declaration),
} }
new_stmts.push(self.ctx.ast.copy(stmt)); new_stmts.push(self.ast.copy(stmt));
} }
_ => {} _ => {}
}); });
@ -231,7 +242,7 @@ impl<'a> IsolatedDeclarations<'a> {
// 6. Transform variable/using declarations, import statements, remove unused imports // 6. Transform variable/using declarations, import statements, remove unused imports
// 7. Return transformed statements // 7. Return transformed statements
let mut new_ast_stmts = self.ctx.ast.new_vec_with_capacity(transformed_indexes.len()); let mut new_ast_stmts = self.ast.new_vec_with_capacity(transformed_indexes.len());
for (index, stmt) in new_stmts.into_iter().enumerate() { for (index, stmt) in new_stmts.into_iter().enumerate() {
match stmt { match stmt {
_ if transformed_indexes.contains(&index) => { _ if transformed_indexes.contains(&index) => {
@ -247,7 +258,7 @@ impl<'a> IsolatedDeclarations<'a> {
let variables_declaration = self let variables_declaration = self
.transform_variable_declaration_with_new_declarations( .transform_variable_declaration_with_new_declarations(
&decl, &decl,
self.ctx.ast.new_vec_from_iter( self.ast.new_vec_from_iter(
declarations declarations
.into_iter() .into_iter()
.enumerate() .enumerate()
@ -268,7 +279,7 @@ impl<'a> IsolatedDeclarations<'a> {
let variable_declaration = self let variable_declaration = self
.transform_using_declaration_with_new_declarations( .transform_using_declaration_with_new_declarations(
&decl, &decl,
self.ctx.ast.new_vec_from_iter( self.ast.new_vec_from_iter(
declarations declarations
.into_iter() .into_iter()
.enumerate() .enumerate()

View file

@ -17,7 +17,7 @@ impl<'a> IsolatedDeclarations<'a> {
Some(ExportNamedDeclaration { Some(ExportNamedDeclaration {
span: decl.span(), span: decl.span(),
declaration: Some(decl), declaration: Some(decl),
specifiers: self.ctx.ast.new_vec(), specifiers: self.ast.new_vec(),
source: None, source: None,
export_kind: ImportOrExportKind::Value, export_kind: ImportOrExportKind::Value,
with_clause: None, with_clause: None,
@ -37,7 +37,7 @@ impl<'a> IsolatedDeclarations<'a> {
.map(|d| (None, ExportDefaultDeclarationKind::ClassDeclaration(d))), .map(|d| (None, ExportDefaultDeclarationKind::ClassDeclaration(d))),
ExportDefaultDeclarationKind::TSInterfaceDeclaration(interface_decl) => { ExportDefaultDeclarationKind::TSInterfaceDeclaration(interface_decl) => {
self.visit_ts_interface_declaration(interface_decl); self.visit_ts_interface_declaration(interface_decl);
Some((None, self.ctx.ast.copy(&decl.declaration))) Some((None, self.ast.copy(&decl.declaration)))
} }
expr @ match_expression!(ExportDefaultDeclarationKind) => { expr @ match_expression!(ExportDefaultDeclarationKind) => {
let expr = expr.to_expression(); let expr = expr.to_expression();
@ -46,19 +46,18 @@ impl<'a> IsolatedDeclarations<'a> {
} else { } else {
// declare const _default: Type // declare const _default: Type
let kind = VariableDeclarationKind::Const; let kind = VariableDeclarationKind::Const;
let name = self.ctx.ast.new_atom("_default"); let name = self.ast.new_atom("_default");
let id = self let id = self
.ctx
.ast .ast
.binding_pattern_identifier(BindingIdentifier::new(SPAN, name.clone())); .binding_pattern_identifier(BindingIdentifier::new(SPAN, name.clone()));
let type_annotation = self let type_annotation = self
.infer_type_from_expression(expr) .infer_type_from_expression(expr)
.map(|ts_type| self.ctx.ast.ts_type_annotation(SPAN, ts_type)); .map(|ts_type| self.ast.ts_type_annotation(SPAN, ts_type));
let id = BindingPattern { kind: id, type_annotation, optional: false }; let id = BindingPattern { kind: id, type_annotation, optional: false };
let declarations = self.ctx.ast.new_vec_single( let declarations = self
self.ctx.ast.variable_declarator(SPAN, kind, id, None, true), .ast
); .new_vec_single(self.ast.variable_declarator(SPAN, kind, id, None, true));
Some(( Some((
Some(VariableDeclaration { Some(VariableDeclaration {
@ -68,8 +67,8 @@ impl<'a> IsolatedDeclarations<'a> {
modifiers: self.modifiers_declare(), modifiers: self.modifiers_declare(),
}), }),
ExportDefaultDeclarationKind::from( ExportDefaultDeclarationKind::from(
self.ctx.ast.identifier_reference_expression( self.ast.identifier_reference_expression(
self.ctx.ast.identifier_reference(SPAN, &name), self.ast.identifier_reference(SPAN, &name),
), ),
), ),
)) ))
@ -80,7 +79,7 @@ impl<'a> IsolatedDeclarations<'a> {
declaration.map(|(var_decl, declaration)| { declaration.map(|(var_decl, declaration)| {
let exported = ModuleExportName::Identifier(IdentifierName::new( let exported = ModuleExportName::Identifier(IdentifierName::new(
SPAN, SPAN,
self.ctx.ast.new_atom("default"), self.ast.new_atom("default"),
)); ));
(var_decl, ExportDefaultDeclaration { span: decl.span, declaration, exported }) (var_decl, ExportDefaultDeclaration { span: decl.span, declaration, exported })
}) })
@ -92,7 +91,7 @@ impl<'a> IsolatedDeclarations<'a> {
) -> Option<Box<'a, ImportDeclaration<'a>>> { ) -> Option<Box<'a, ImportDeclaration<'a>>> {
let specifiers = decl.specifiers.as_ref()?; let specifiers = decl.specifiers.as_ref()?;
let mut specifiers = self.ctx.ast.copy(specifiers); let mut specifiers = self.ast.copy(specifiers);
specifiers.retain(|specifier| match specifier { specifiers.retain(|specifier| match specifier {
ImportDeclarationSpecifier::ImportSpecifier(specifier) => { ImportDeclarationSpecifier::ImportSpecifier(specifier) => {
self.scope.has_reference(&specifier.local.name) self.scope.has_reference(&specifier.local.name)
@ -101,18 +100,18 @@ impl<'a> IsolatedDeclarations<'a> {
self.scope.has_reference(&specifier.local.name) self.scope.has_reference(&specifier.local.name)
} }
ImportDeclarationSpecifier::ImportNamespaceSpecifier(_) => { ImportDeclarationSpecifier::ImportNamespaceSpecifier(_) => {
self.scope.has_reference(&self.ctx.ast.new_atom(&specifier.name())) self.scope.has_reference(&self.ast.new_atom(&specifier.name()))
} }
}); });
if specifiers.is_empty() { if specifiers.is_empty() {
// We don't need to print this import statement // We don't need to print this import statement
None None
} else { } else {
Some(self.ctx.ast.import_declaration( Some(self.ast.import_declaration(
decl.span, decl.span,
Some(specifiers), Some(specifiers),
self.ctx.ast.copy(&decl.source), self.ast.copy(&decl.source),
self.ctx.ast.copy(&decl.with_clause), self.ast.copy(&decl.with_clause),
decl.import_kind, decl.import_kind,
)) ))
} }

View file

@ -1,20 +1,18 @@
use std::rc::Rc;
use oxc_ast::{ use oxc_ast::{
ast::{ ast::{
BindingIdentifier, Expression, Function, FunctionBody, ReturnStatement, TSType, BindingIdentifier, Expression, Function, FunctionBody, ReturnStatement, TSType,
TSTypeAliasDeclaration, TSTypeName, TSTypeQueryExprName, TSTypeAliasDeclaration, TSTypeName, TSTypeQueryExprName,
}, },
Visit, AstBuilder, Visit,
}; };
use oxc_span::{Atom, GetSpan}; use oxc_span::{Atom, GetSpan};
use oxc_syntax::scope::ScopeFlags; use oxc_syntax::scope::ScopeFlags;
use crate::{context::Ctx, diagnostics::type_containing_private_name, IsolatedDeclarations}; use crate::{diagnostics::type_containing_private_name, IsolatedDeclarations};
/// Infer return type from return statement. Does not support multiple return statements. /// Infer return type from return statement. Does not support multiple return statements.
pub struct FunctionReturnType<'a> { pub struct FunctionReturnType<'a> {
ctx: Ctx<'a>, ast: AstBuilder<'a>,
return_expression: Option<Expression<'a>>, return_expression: Option<Expression<'a>>,
value_bindings: Vec<Atom<'a>>, value_bindings: Vec<Atom<'a>>,
type_bindings: Vec<Atom<'a>>, type_bindings: Vec<Atom<'a>>,
@ -28,7 +26,7 @@ impl<'a> FunctionReturnType<'a> {
body: &FunctionBody<'a>, body: &FunctionBody<'a>,
) -> Option<TSType<'a>> { ) -> Option<TSType<'a>> {
let mut visitor = FunctionReturnType { let mut visitor = FunctionReturnType {
ctx: Rc::clone(&transformer.ctx), ast: transformer.ast,
return_expression: None, return_expression: None,
return_statement_count: 0, return_statement_count: 0,
scope_depth: 0, scope_depth: 0,
@ -69,7 +67,7 @@ impl<'a> FunctionReturnType<'a> {
}; };
if is_defined_in_current_scope { if is_defined_in_current_scope {
transformer.ctx.error(type_containing_private_name( transformer.error(type_containing_private_name(
&reference_name, &reference_name,
expr_type expr_type
.get_identifier_reference() .get_identifier_reference()
@ -108,6 +106,6 @@ impl<'a> Visit<'a> for FunctionReturnType<'a> {
if self.return_statement_count > 1 { if self.return_statement_count > 1 {
return; return;
} }
self.return_expression = self.ctx.ast.copy(&stmt.argument); self.return_expression = self.ast.copy(&stmt.argument);
} }
} }

View file

@ -1,3 +1,5 @@
use rustc_hash::FxHashSet;
use oxc_allocator::{Allocator, Vec}; use oxc_allocator::{Allocator, Vec};
#[allow(clippy::wildcard_imports)] #[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*; use oxc_ast::ast::*;
@ -6,7 +8,6 @@ use oxc_ast::AstBuilder;
use oxc_ast::{visit::walk::*, Visit}; use oxc_ast::{visit::walk::*, Visit};
use oxc_span::Atom; use oxc_span::Atom;
use oxc_syntax::scope::ScopeFlags; use oxc_syntax::scope::ScopeFlags;
use rustc_hash::FxHashSet;
pub struct ScopeTree<'a> { pub struct ScopeTree<'a> {
type_bindings: Vec<'a, FxHashSet<Atom<'a>>>, type_bindings: Vec<'a, FxHashSet<Atom<'a>>>,

View file

@ -14,12 +14,12 @@ impl<'a> IsolatedDeclarations<'a> {
let params = self.transform_formal_parameters(&func.params); let params = self.transform_formal_parameters(&func.params);
return_type.map(|return_type| { return_type.map(|return_type| {
self.ctx.ast.ts_function_type( self.ast.ts_function_type(
func.span, func.span,
self.ctx.ast.copy(&func.this_param), self.ast.copy(&func.this_param),
params, params,
return_type, return_type,
self.ctx.ast.copy(&func.type_parameters), self.ast.copy(&func.type_parameters),
) )
}) })
} }
@ -32,12 +32,12 @@ impl<'a> IsolatedDeclarations<'a> {
let params = self.transform_formal_parameters(&func.params); let params = self.transform_formal_parameters(&func.params);
return_type.map(|return_type| { return_type.map(|return_type| {
self.ctx.ast.ts_function_type( self.ast.ts_function_type(
func.span, func.span,
None, None,
params, params,
return_type, return_type,
self.ctx.ast.copy(&func.type_parameters), self.ast.copy(&func.type_parameters),
) )
}) })
} }
@ -60,7 +60,7 @@ impl<'a> IsolatedDeclarations<'a> {
is_const: bool, is_const: bool,
) -> TSType<'a> { ) -> TSType<'a> {
let members = let members =
self.ctx.ast.new_vec_from_iter(expr.properties.iter().filter_map(|property| match property { self.ast.new_vec_from_iter(expr.properties.iter().filter_map(|property| match property {
ObjectPropertyKind::ObjectProperty(object) => { ObjectPropertyKind::ObjectProperty(object) => {
if self.report_property_key(&object.key, object.computed) { if self.report_property_key(&object.key, object.computed) {
return None; return None;
@ -70,41 +70,41 @@ impl<'a> IsolatedDeclarations<'a> {
if !is_const && object.method { if !is_const && object.method {
let return_type = self.infer_function_return_type(function); let return_type = self.infer_function_return_type(function);
let params = self.transform_formal_parameters(&function.params); let params = self.transform_formal_parameters(&function.params);
return Some(self.ctx.ast.ts_method_signature( return Some(self.ast.ts_method_signature(
object.span, object.span,
self.ctx.ast.copy(&object.key), self.ast.copy(&object.key),
object.computed, object.computed,
false, false,
TSMethodSignatureKind::Method, TSMethodSignatureKind::Method,
self.ctx.ast.copy(&function.this_param), self.ast.copy(&function.this_param),
params, params,
return_type, return_type,
self.ctx.ast.copy(&function.type_parameters), self.ast.copy(&function.type_parameters),
)); ));
} }
} }
let type_annotation = self.infer_type_from_expression(&object.value); let type_annotation = self.infer_type_from_expression(&object.value);
let property_signature = self.ctx.ast.ts_property_signature( let property_signature = self.ast.ts_property_signature(
object.span, object.span,
false, false,
false, false,
is_const, is_const,
self.ctx.ast.copy(&object.key), self.ast.copy(&object.key),
type_annotation type_annotation
.map(|type_annotation| self.ctx.ast.ts_type_annotation(SPAN, type_annotation)), .map(|type_annotation| self.ast.ts_type_annotation(SPAN, type_annotation)),
); );
Some(property_signature) Some(property_signature)
}, },
ObjectPropertyKind::SpreadProperty(spread) => { ObjectPropertyKind::SpreadProperty(spread) => {
self.ctx.error(OxcDiagnostic::error( self.error(OxcDiagnostic::error(
"Objects that contain spread assignments can't be inferred with --isolatedDeclarations.", "Objects that contain spread assignments can't be inferred with --isolatedDeclarations.",
).with_label(spread.span)); ).with_label(spread.span));
None None
} }
})); }));
self.ctx.ast.ts_type_literal(SPAN, members) self.ast.ts_type_literal(SPAN, members)
} }
pub fn transform_array_expression_to_ts_type( pub fn transform_array_expression_to_ts_type(
@ -113,16 +113,16 @@ impl<'a> IsolatedDeclarations<'a> {
is_const: bool, is_const: bool,
) -> TSType<'a> { ) -> TSType<'a> {
let element_types = let element_types =
self.ctx.ast.new_vec_from_iter(expr.elements.iter().filter_map(|element| { self.ast.new_vec_from_iter(expr.elements.iter().filter_map(|element| {
match element { match element {
ArrayExpressionElement::SpreadElement(spread) => { ArrayExpressionElement::SpreadElement(spread) => {
self.ctx.error(OxcDiagnostic::error( self.error(OxcDiagnostic::error(
"Arrays with spread elements can't inferred with --isolatedDeclarations.", "Arrays with spread elements can't inferred with --isolatedDeclarations.",
).with_label(spread.span)); ).with_label(spread.span));
None None
}, },
ArrayExpressionElement::Elision(elision) => { ArrayExpressionElement::Elision(elision) => {
Some(TSTupleElement::from(self.ctx.ast.ts_undefined_keyword(elision.span))) Some(TSTupleElement::from(self.ast.ts_undefined_keyword(elision.span)))
}, },
_ => { _ => {
Some(TSTupleElement::from(self.transform_expression_to_ts_type(element.to_expression()))) Some(TSTupleElement::from(self.transform_expression_to_ts_type(element.to_expression())))
@ -130,9 +130,9 @@ impl<'a> IsolatedDeclarations<'a> {
} }
})); }));
let ts_type = self.ctx.ast.ts_tuple_type(SPAN, element_types); let ts_type = self.ast.ts_tuple_type(SPAN, element_types);
if is_const { if is_const {
self.ctx.ast.ts_type_operator_type(SPAN, TSTypeOperatorOperator::Readonly, ts_type) self.ast.ts_type_operator_type(SPAN, TSTypeOperatorOperator::Readonly, ts_type)
} else { } else {
ts_type ts_type
} }
@ -141,28 +141,24 @@ impl<'a> IsolatedDeclarations<'a> {
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions
pub fn transform_expression_to_ts_type(&self, expr: &Expression<'a>) -> TSType<'a> { pub fn transform_expression_to_ts_type(&self, expr: &Expression<'a>) -> TSType<'a> {
match expr { match expr {
Expression::BooleanLiteral(lit) => self Expression::BooleanLiteral(lit) => {
.ctx self.ast.ts_literal_type(SPAN, TSLiteral::BooleanLiteral(self.ast.copy(lit)))
.ast }
.ts_literal_type(SPAN, TSLiteral::BooleanLiteral(self.ctx.ast.copy(lit))), Expression::NumericLiteral(lit) => {
Expression::NumericLiteral(lit) => self self.ast.ts_literal_type(SPAN, TSLiteral::NumericLiteral(self.ast.copy(lit)))
.ctx }
.ast
.ts_literal_type(SPAN, TSLiteral::NumericLiteral(self.ctx.ast.copy(lit))),
Expression::BigintLiteral(lit) => { Expression::BigintLiteral(lit) => {
self.ctx.ast.ts_literal_type(SPAN, TSLiteral::BigintLiteral(self.ctx.ast.copy(lit))) self.ast.ts_literal_type(SPAN, TSLiteral::BigintLiteral(self.ast.copy(lit)))
} }
Expression::StringLiteral(lit) => { Expression::StringLiteral(lit) => {
self.ctx.ast.ts_literal_type(SPAN, TSLiteral::StringLiteral(self.ctx.ast.copy(lit))) self.ast.ts_literal_type(SPAN, TSLiteral::StringLiteral(self.ast.copy(lit)))
}
Expression::TemplateLiteral(lit) => {
self.ast.ts_literal_type(SPAN, TSLiteral::TemplateLiteral(self.ast.copy(lit)))
}
Expression::UnaryExpression(expr) => {
self.ast.ts_literal_type(SPAN, TSLiteral::UnaryExpression(self.ast.copy(expr)))
} }
Expression::TemplateLiteral(lit) => self
.ctx
.ast
.ts_literal_type(SPAN, TSLiteral::TemplateLiteral(self.ctx.ast.copy(lit))),
Expression::UnaryExpression(expr) => self
.ctx
.ast
.ts_literal_type(SPAN, TSLiteral::UnaryExpression(self.ctx.ast.copy(expr))),
Expression::ArrayExpression(expr) => { Expression::ArrayExpression(expr) => {
self.transform_array_expression_to_ts_type(expr, true) self.transform_array_expression_to_ts_type(expr, true)
} }