From 21727ccf372d5ff960fa23bd273f1ecf7f9ece75 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 22 Mar 2023 08:47:18 +0800 Subject: [PATCH] Share logic between flow and ts (#14545) --- src/language-js/print/flow.js | 6 ++++-- src/language-js/print/type-annotation.js | 23 +++++++++++++++++++++++ src/language-js/print/typescript.js | 10 ++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/language-js/print/flow.js b/src/language-js/print/flow.js index 7adc8a876..e9ccc608f 100644 --- a/src/language-js/print/flow.js +++ b/src/language-js/print/flow.js @@ -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); diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 9da464d17..235da0f94 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -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, }; diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index 5aec0dfd6..407cd94dd 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -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":