Skip to content

Commit

Permalink
Allow nullish extends in interfaceish (#12920)
Browse files Browse the repository at this point in the history
* Allow nullish extends in interfaceish

Otherwise code such as

```ts
t.interfaceDeclaration(
  t.identifier('id'),
  undefined,
  undefined,
  t.objectTypeAnnotation([])
)
```

Will fail when priting with ` TypeError: unknown: Cannot read property 'length' of null`
Despite nullish values being allowed in `t.interfaceDeclaration` definitions

* Update packages/babel-generator/src/generators/flow.ts

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
saitonakamura and nicolo-ribaudo committed Mar 1, 2021
1 parent b62fc3d commit 4c343ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/flow.ts
Expand Up @@ -360,7 +360,7 @@ export function _interfaceish(
) {
this.print(node.id, node);
this.print(node.typeParameters, node);
if (node.extends.length) {
if (node.extends?.length) {
this.space();
this.word("extends");
this.space();
Expand Down
11 changes: 11 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -462,6 +462,17 @@ describe("programmatic generation", function () {
}`);
});

it("flow interface with nullish extends", () => {
const interfaceDeclaration = t.interfaceDeclaration(
t.identifier("A"),
undefined,
undefined,
t.objectTypeAnnotation([]),
);
const output = generate(interfaceDeclaration).code;
expect(output).toBe("interface A {}");
});

describe("directives", function () {
it("preserves escapes", function () {
const directive = t.directive(
Expand Down

0 comments on commit 4c343ac

Please sign in to comment.