Share logic between flow and ts (#14545)
This commit is contained in:
1 parent
1c80e0104f
commit
21727ccf37
3 files changed
+33
-6
No files matched your search
@@ -22,6 +22,8 @@ import {
|
||||
printNamedTupleMember,
|
||||
printTypeAnnotation,
|
||||
printTypeAnnotationProperty,
|
||||
printArrayType,
|
||||
printTypeQuery,
|
||||
} from "./type-annotation.js";
|
||||
import { printInterface } from "./interface.js";
|
||||
import { printTypeParameter, printTypeParameters } from "./type-parameters.js";
|
||||
@@ -113,7 +115,7 @@ function printFlow(path, options, print) {
|
||||
case "TypeParameter":
|
||||
return printTypeParameter(path, options, print);
|
||||
case "TypeofTypeAnnotation":
|
||||
return ["typeof ", print("argument")];
|
||||
return printTypeQuery(path, print);
|
||||
case "ExistsTypeAnnotation":
|
||||
return "*";
|
||||
case "EmptyTypeAnnotation":
|
||||
@@ -121,7 +123,7 @@ function printFlow(path, options, print) {
|
||||
case "MixedTypeAnnotation":
|
||||
return "mixed";
|
||||
case "ArrayTypeAnnotation":
|
||||
return [print("elementType"), "[]"];
|
||||
return printArrayType(print);
|
||||
case "BooleanLiteralTypeAnnotation":
|
||||
return String(node.value);
|
||||
|
||||
|
||||
@@ -476,6 +476,27 @@ function printTypeAnnotation(path, options, print) {
|
||||
: print("typeAnnotation");
|
||||
}
|
||||
|
||||
/*
|
||||
- `TSArrayType`
|
||||
- `ArrayTypeAnnotation`
|
||||
*/
|
||||
function printArrayType(print) {
|
||||
return [print("elementType"), "[]"];
|
||||
}
|
||||
|
||||
/*
|
||||
- `TSTypeQuery`
|
||||
- `TypeofTypeAnnotation`
|
||||
*/
|
||||
function printTypeQuery({ node }, print) {
|
||||
return [
|
||||
"typeof ",
|
||||
...(node.type === "TSTypeQuery"
|
||||
? [print("exprName"), print("typeParameters")]
|
||||
: [print("argument")]),
|
||||
];
|
||||
}
|
||||
|
||||
export {
|
||||
printOpaqueType,
|
||||
printTypeAlias,
|
||||
@@ -489,4 +510,6 @@ export {
|
||||
printNamedTupleMember,
|
||||
printTypeAnnotationProperty,
|
||||
printTypeAnnotation,
|
||||
printArrayType,
|
||||
printTypeQuery,
|
||||
};
|
||||
@@ -52,6 +52,8 @@ import {
|
||||
printNamedTupleMember,
|
||||
printTypeAnnotation,
|
||||
printTypeAnnotationProperty,
|
||||
printArrayType,
|
||||
printTypeQuery,
|
||||
} from "./type-annotation.js";
|
||||
import { printEnumDeclaration, printEnumMember } from "./enum.js";
|
||||
|
||||
@@ -114,7 +116,7 @@ function printTypescript(path, options, print) {
|
||||
case "TSTypeAliasDeclaration":
|
||||
return printTypeAlias(path, options, print);
|
||||
case "TSQualifiedName":
|
||||
return join(".", [print("left"), print("right")]);
|
||||
return [print("left"), ".", print("right")];
|
||||
case "TSAbstractMethodDefinition":
|
||||
case "TSDeclareMethod":
|
||||
return printClassMethod(path, options, print);
|
||||
@@ -155,7 +157,7 @@ function printTypescript(path, options, print) {
|
||||
return parts;
|
||||
}
|
||||
case "TSArrayType":
|
||||
return [print("elementType"), "[]"];
|
||||
return printArrayType(print);
|
||||
case "TSPropertySignature":
|
||||
return [
|
||||
node.readonly ? "readonly " : "",
|
||||
@@ -174,7 +176,7 @@ function printTypescript(path, options, print) {
|
||||
];
|
||||
|
||||
case "TSTypeQuery":
|
||||
return ["typeof ", print("exprName"), print("typeParameters")];
|
||||
return printTypeQuery(path, print);
|
||||
case "TSIndexSignature": {
|
||||
// The typescript parser accepts multiple parameters here. If you're
|
||||
// using them, it makes sense to have a trailing comma. But if you
|
||||
@@ -382,7 +384,7 @@ function printTypescript(path, options, print) {
|
||||
return printTernary(path, options, print);
|
||||
|
||||
case "TSInferType":
|
||||
return ["infer", " ", print("typeParameter")];
|
||||
return ["infer ", print("typeParameter")];
|
||||
case "TSIntersectionType":
|
||||
return printIntersectionType(path, options, print);
|
||||
case "TSUnionType":
|
||||
|
||||
Reference in new issue
Block a user