From 775544ee58fb70750d2af50ba8926cbf2da443fb Mon Sep 17 00:00:00 2001 From: Michael Bashurov Date: Sat, 27 Feb 2021 12:12:55 +0200 Subject: [PATCH] Fix a bug with invalid print output when empty array is passed to t.tsInterfaceDeclaration 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 0993e5c352b8..c053162fa500 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -724,6 +724,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 () {