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
This commit is contained in:
Michael サイトー 中村 Bashurov 2021-03-01 17:43:24 +02:00 committed by GitHub
parent 4c343ac853
commit d05fdbc3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -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();

View File

@ -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 () {