fix: do not throw when creating type annotation based on bigint (#12971)

This commit is contained in:
Huáng Jùnliàng 2021-03-19 11:44:11 -04:00 committed by GitHub
parent e2244c92d9
commit 85963167b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import { import {
anyTypeAnnotation,
stringTypeAnnotation, stringTypeAnnotation,
numberTypeAnnotation, numberTypeAnnotation,
voidTypeAnnotation, voidTypeAnnotation,
@ -25,7 +26,8 @@ export default function createTypeAnnotationBasedOnTypeof(
| t.VoidTypeAnnotation | t.VoidTypeAnnotation
| t.NumberTypeAnnotation | t.NumberTypeAnnotation
| t.BooleanTypeAnnotation | t.BooleanTypeAnnotation
| t.GenericTypeAnnotation { | t.GenericTypeAnnotation
| t.AnyTypeAnnotation {
if (type === "string") { if (type === "string") {
return stringTypeAnnotation(); return stringTypeAnnotation();
} else if (type === "number") { } else if (type === "number") {
@ -40,7 +42,11 @@ export default function createTypeAnnotationBasedOnTypeof(
return genericTypeAnnotation(identifier("Object")); return genericTypeAnnotation(identifier("Object"));
} else if (type === "symbol") { } else if (type === "symbol") {
return genericTypeAnnotation(identifier("Symbol")); return genericTypeAnnotation(identifier("Symbol"));
} else if (type === "bigint") {
// todo: use BigInt annotation when Flow supports BigInt
// https://github.com/facebook/flow/issues/6639
return anyTypeAnnotation();
} else { } else {
throw new Error("Invalid typeof value"); throw new Error("Invalid typeof value: " + type);
} }
} }

View File

@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`builders flow createTypeAnnotationBasedOnTypeof bigint 1`] = `
Object {
"type": "AnyTypeAnnotation",
}
`;
exports[`builders flow createTypeAnnotationBasedOnTypeof function 1`] = ` exports[`builders flow createTypeAnnotationBasedOnTypeof function 1`] = `
Object { Object {
"id": Object { "id": Object {

View File

@ -11,6 +11,13 @@ describe("builders", function () {
undefined: typeof undefined, undefined: typeof undefined,
function: typeof function () {}, function: typeof function () {},
symbol: typeof Symbol(), symbol: typeof Symbol(),
bigint: (() => {
try {
return eval("typeof 0n");
} catch (e) {
return "bigint";
}
})(),
}; };
for (const name in values) { for (const name in values) {