fix(codegen): should print TSModuleDeclarationKind instead of just module (#3957)

This commit is contained in:
Dunqing 2024-06-29 05:24:36 +00:00
parent 2705df93b3
commit 51e54f902c
4 changed files with 26 additions and 7 deletions

View file

@ -3267,9 +3267,12 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {
if self.declare { if self.declare {
p.print_str(b"declare "); p.print_str(b"declare ");
} }
p.print_str(b"module"); self.kind.gen(p, ctx);
p.print_space_before_identifier(); // If the kind is global, then the id is also `global`, so we don't need to print it
self.id.gen(p, ctx); if !self.kind.is_global() {
p.print_space_before_identifier();
self.id.gen(p, ctx);
}
if let Some(body) = &self.body { if let Some(body) = &self.body {
let mut body = body; let mut body = body;
@ -3296,6 +3299,22 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {
} }
} }
impl<const MINIFY: bool> Gen<MINIFY> for TSModuleDeclarationKind {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _: Context) {
match self {
TSModuleDeclarationKind::Global => {
p.print_str(b"global");
}
TSModuleDeclarationKind::Module => {
p.print_str(b"module");
}
TSModuleDeclarationKind::Namespace => {
p.print_str(b"namespace");
}
}
}
}
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclarationName<'a> { impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclarationName<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
match self { match self {

View file

@ -4,5 +4,5 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/declare-global.ts
--- ---
==================== .D.TS ==================== ==================== .D.TS ====================
declare module global {} declare global {}
export {}; export {};

View file

@ -6,7 +6,7 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/expando-function.ts
export declare function foo(): void; export declare function foo(): void;
export declare const bar: () => void; export declare const bar: () => void;
export declare module NS { export declare namespace NS {
export const goo: () => void; export const goo: () => void;
} }

View file

@ -9,7 +9,7 @@ Mismatch: "declarationRestParameters.ts"
#### "declarationComputedPropertyNames.ts" #### #### "declarationComputedPropertyNames.ts" ####
//// [declarationComputedPropertyNames.ts] //// //// [declarationComputedPropertyNames.ts] ////
export module presentNs { export namespace presentNs {
export const a = Symbol(); export const a = Symbol();
} }
const aliasing = Symbol; const aliasing = Symbol;
@ -67,7 +67,7 @@ export const D = {
}; };
//// [declarationComputedPropertyNames.d.ts] //// //// [declarationComputedPropertyNames.d.ts] ////
export declare module presentNs { export declare namespace presentNs {
export const a: unknown; export const a: unknown;
} }
declare const aliasing: unknown; declare const aliasing: unknown;