Add isFlowKeywordType (#14549)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-03-22 09:18:03 +08:00
1 parent a3b352ce90
commit 6718d466b8
4 files changed
+45 -87

No files matched your search

+13 -30
View File
@@ -10,6 +10,7 @@ import {
isGetterOrSetter,
rawText,
} from "../utils/index.js";
import isFlowKeywordType from "../utils/is-flow-keyword-type.js";
import { printClass } from "./class.js";
import {
printOpaqueType,
@@ -45,6 +46,12 @@ import {
function printFlow(path, options, print) {
const { node } = path;
if (isFlowKeywordType(node)) {
// Flow keyword types ends with `TypeAnnotation`
return node.type.slice(0, -14).toLowerCase();
}
const semi = options.semi ? ";" : "";
switch (node.type) {
@@ -117,14 +124,8 @@ function printFlow(path, options, print) {
return printTypeQuery(path, print);
case "ExistsTypeAnnotation":
return "*";
case "EmptyTypeAnnotation":
return "empty";
case "MixedTypeAnnotation":
return "mixed";
case "ArrayTypeAnnotation":
return printArrayType(print);
case "BooleanLiteralTypeAnnotation":
return String(node.value);
case "DeclareEnum":
case "EnumDeclaration":
@@ -224,6 +225,11 @@ function printFlow(path, options, print) {
case "QualifiedTypeofIdentifier":
case "QualifiedTypeIdentifier":
return [print("qualification"), ".", print("id")];
case "NullLiteralTypeAnnotation":
return "null";
case "BooleanLiteralTypeAnnotation":
return String(node.value);
case "StringLiteralTypeAnnotation":
return replaceEndOfLine(printString(rawText(node), options));
case "NumberLiteralTypeAnnotation":
@@ -246,30 +252,7 @@ function printFlow(path, options, print) {
return "%checks";
case "DeclaredPredicate":
return ["%checks(", print("value"), ")"];
case "AnyTypeAnnotation":
return "any";
case "BooleanTypeAnnotation":
return "boolean";
case "BigIntTypeAnnotation":
return "bigint";
case "NullLiteralTypeAnnotation":
return "null";
case "NumberTypeAnnotation":
return "number";
case "SymbolTypeAnnotation":
return "symbol";
case "StringTypeAnnotation":
return "string";
case "VoidTypeAnnotation":
return "void";
case "ThisTypeAnnotation":
return "this";
case "NeverTypeAnnotation":
return "never";
case "UndefinedTypeAnnotation":
return "undefined";
case "UnknownTypeAnnotation":
return "unknown";
// These types are unprintable because they serve as abstract
// supertypes for other (printable) types.
case "Node":
+1
View File
@@ -67,6 +67,7 @@ function printTypescript(path, options, print) {
}
if (isTsKeywordType(node)) {
// TS keyword types stars with `TS`, ends with `Keyword`
return node.type.slice(2, -7).toLowerCase();
}
+11 -57
View File
@@ -9,6 +9,8 @@ import getVisitorKeys from "../traverse/get-visitor-keys.js";
import createTypeCheckFunction from "./create-type-check-function.js";
import isBlockComment from "./is-block-comment.js";
import isNodeMatches from "./is-node-matches.js";
import isFlowKeywordType from "./is-flow-keyword-type.js";
import isTsKeywordType from "./is-ts-keyword-type.js";
/**
* @typedef {import("../types/estree.js").Node} Node
@@ -318,78 +320,30 @@ function isMemberish(node) {
);
}
const simpleTypeAnnotations = new Set([
// `any`
"AnyTypeAnnotation",
"TSAnyKeyword",
// `null`
"NullLiteralTypeAnnotation",
"TSNullKeyword",
// `this`
"ThisTypeAnnotation",
const isSimpleTypeAnnotation = createTypeCheckFunction([
"TSThisType",
// `number`
"NumberTypeAnnotation",
"TSNumberKeyword",
// `void`
"VoidTypeAnnotation",
"TSVoidKeyword",
// `boolean`
"BooleanTypeAnnotation",
"TSBooleanKeyword",
// `bigint`
"BigIntTypeAnnotation",
"TSBigIntKeyword",
// `symbol`
"SymbolTypeAnnotation",
"TSSymbolKeyword",
// `string`
"StringTypeAnnotation",
"TSStringKeyword",
// `never`
"NeverTypeAnnotation",
"TSNeverKeyword",
// `undefined`
"UndefinedTypeAnnotation",
"TSUndefinedKeyword",
// `unknown`
"UnknownTypeAnnotation",
"TSUnknownKeyword",
// literals
"NullLiteralTypeAnnotation",
"BooleanLiteralTypeAnnotation",
"StringLiteralTypeAnnotation",
"BigIntLiteralTypeAnnotation",
"NumberLiteralTypeAnnotation",
"TSLiteralType",
"TSTemplateLiteralType",
// flow only, `empty`, `mixed`
"EmptyTypeAnnotation",
"MixedTypeAnnotation",
// typescript only `object`
"TSObjectKeyword",
]);
/**
* @param {Node} node
* @returns {boolean}
*/
function isSimpleType(node) {
if (!node) {
return false;
}
if (
(node.type === "GenericTypeAnnotation" ||
return (
isTsKeywordType(node) ||
isFlowKeywordType(node) ||
isSimpleTypeAnnotation(node) ||
((node.type === "GenericTypeAnnotation" ||
node.type === "TSTypeReference") &&
!node.typeParameters
) {
return true;
}
if (simpleTypeAnnotations.has(node.type)) {
return true;
}
return false;
!node.typeParameters)
);
}
/**
@@ -0,0 +1,20 @@
import createTypeCheckFunction from "./create-type-check-function.js";
const isFlowKeywordType = createTypeCheckFunction([
"AnyTypeAnnotation",
"ThisTypeAnnotation",
"NumberTypeAnnotation",
"VoidTypeAnnotation",
"BooleanTypeAnnotation",
"BigIntTypeAnnotation",
"SymbolTypeAnnotation",
"StringTypeAnnotation",
"NeverTypeAnnotation",
"UndefinedTypeAnnotation",
"UnknownTypeAnnotation",
// FLow only
"EmptyTypeAnnotation",
"MixedTypeAnnotation",
]);
export default isFlowKeywordType;