feat(codegen): print TSClassImplements and TSThisParameter (#3786)

close: https://github.com/oxc-project/oxc/issues/3692#issuecomment-2178994839
This commit is contained in:
Dunqing 2024-06-20 06:15:23 +00:00
parent 497769cb60
commit 97575d8cab
3 changed files with 41 additions and 14 deletions

View file

@ -656,6 +656,13 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Function<'a> {
type_parameters.gen(p, ctx);
}
p.print(b'(');
if let Some(this_param) = &self.this_param {
this_param.gen(p, ctx);
if !self.params.is_empty() || self.params.rest.is_some() {
p.print_str(b",");
}
p.print_soft_space();
}
self.params.gen(p, ctx);
p.print(b')');
if let Some(return_type) = &self.return_type {
@ -2098,6 +2105,13 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Class<'a> {
if let Some(super_class) = self.super_class.as_ref() {
p.print_str(b" extends ");
super_class.gen_expr(p, Precedence::Call, Context::default());
if let Some(super_type_parameters) = &self.super_type_parameters {
super_type_parameters.gen(p, ctx);
}
}
if let Some(implements) = self.implements.as_ref() {
p.print_str(b" implements ");
p.print_list(implements, ctx);
}
p.print_soft_space();
self.body.gen(p, ctx);
@ -2628,6 +2642,15 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Decorator<'a> {
}
}
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSClassImplements<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
self.expression.gen(p, ctx);
if let Some(type_parameters) = self.type_parameters.as_ref() {
type_parameters.gen(p, ctx);
}
}
}
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTypeParameterDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.print_str(b"<");
@ -2971,13 +2994,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSFunctionType<'a> {
}
p.print_str(b"(");
if let Some(this_param) = &self.this_param {
this_param.this.gen(p, ctx);
p.print_str(b":");
if let Some(type_annotation) = &this_param.type_annotation {
type_annotation.gen(p, ctx);
} else {
p.print_str(b"unknown");
}
this_param.gen(p, ctx);
if !self.params.is_empty() || self.params.rest.is_some() {
p.print_str(b",");
}
@ -2992,6 +3009,16 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSFunctionType<'a> {
}
}
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSThisParameter<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
self.this.gen(p, ctx);
if let Some(type_annotation) = &self.type_annotation {
p.print_str(b": ");
type_annotation.gen(p, ctx);
}
}
}
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSSignature<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
match self {

View file

@ -1,7 +1,7 @@
import { AExtend, BExtend, Type, CImplements, CType, ThisType1, ThisType2, Unused } from 'mod';
import { AExtend, BExtend, Type, CImplements1, CImplements2, CType, ThisType1, ThisType2, Unused } from 'mod';
export interface A extends AExtend<Type> {}
export class B extends BExtend<Type> {}
export class C implements CImplements<CType> {}
export class C implements CImplements1<CType>, CImplements2<CType> {}
export function foo(this: ThisType1): void {}
export const bar: (this: ThisType2) => void = function() {}

View file

@ -4,9 +4,9 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/eliminate-imports.ts
---
==================== .D.TS ====================
import { AExtend, BExtend, Type, CImplements, CType, ThisType1, ThisType2 } from 'mod';
import { AExtend, BExtend, Type, CImplements1, CImplements2, CType, ThisType1, ThisType2 } from 'mod';
export interface A extends AExtend<Type> {}
export declare class B extends BExtend {}
export declare class C {}
export declare function foo(): void;
export declare const bar: (this:ThisType2 ) => void;
export declare class B extends BExtend<Type> {}
export declare class C implements CImplements1<CType>, CImplements2<CType> {}
export declare function foo(this: ThisType1 ): void;
export declare const bar: (this: ThisType2 ) => void;