Allow nullish extends in interfaceish (#12920)

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

View File

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

View File

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