Skip to content

Commit

Permalink
Fix a bug with invalid print output when empty array is passed to t.t…
Browse files Browse the repository at this point in the history
…sInterfaceDeclaration

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 Feb 28, 2021
1 parent 265424d commit 775544e
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 @@ -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 () {
Expand Down

0 comments on commit 775544e

Please sign in to comment.