Skip to content

Commit

Permalink
Fix invalid print output when empty array is passed to t.tsInterfaceD…
Browse files Browse the repository at this point in the history
…eclaration (#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
  • Loading branch information
saitonakamura committed Mar 1, 2021
1 parent 4c343ac commit d05fdbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/typescript.ts
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit d05fdbc

Please sign in to comment.