Skip to content

Commit

Permalink
feat(codegen): print TSClassImplements and TSThisParameter (#3786)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 20, 2024
1 parent 497769c commit 97575d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
41 changes: 34 additions & 7 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"<");
Expand Down Expand Up @@ -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",");
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 97575d8

Please sign in to comment.