From d05fdbc3c7fa0baae813e1394843ccf2874b4161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20=E3=82=B5=E3=82=A4=E3=83=88=E3=83=BC=20=E4=B8=AD?= =?UTF-8?q?=E6=9D=91=20Bashurov?= Date: Mon, 1 Mar 2021 17:43:24 +0200 Subject: [PATCH] Fix invalid print output when empty array is passed to t.tsInterfaceDeclaration (#12921) If you pass an empty array as `extends` in `t.tsInterfaceDeclaration` you'll get an invalid code printed ```ts t.tsInterfaceDeclaration( t.identifier('x'), undefined, [], t.tsInterfaceBody([]) ) ``` You will get ```ts interface A extends {} ``` Which is an invalid TS, this PR fixes that --- .../babel-generator/src/generators/typescript.ts | 2 +- packages/babel-generator/test/index.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/babel-generator/src/generators/typescript.ts b/packages/babel-generator/src/generators/typescript.ts index 14375b0beac5..0945de55ad5f 100644 --- a/packages/babel-generator/src/generators/typescript.ts +++ b/packages/babel-generator/src/generators/typescript.ts @@ -435,7 +435,7 @@ export function TSInterfaceDeclaration( this.space(); this.print(id, node); this.print(typeParameters, node); - if (extendz) { + if (extendz?.length) { this.space(); this.word("extends"); this.space(); diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index 7dcf37f6112f..8c597b818db7 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -735,6 +735,19 @@ describe("programmatic generation", function () { } }); }); + + describe("typescript interface declaration", () => { + it("empty extends array", () => { + const tsInterfaceDeclaration = t.tsInterfaceDeclaration( + t.identifier("A"), + undefined, + [], + t.tsInterfaceBody([]), + ); + const output = generate(tsInterfaceDeclaration).code; + expect(output).toBe("interface A {}"); + }); + }); }); describe("CodeGenerator", function () {