Extract type guard functions to node-types.js (#18439)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-12-07 18:12:31 +08:00
1 parent 1fa465679c
commit 2f2faa92c4
3 files changed
+99 -83

No files matched your search

@@ -1193,10 +1193,6 @@ function handleCommentAfterArrowExpression({
return false;
}
/**
* @param {Node} node
* @returns {boolean}
*/
const isRealFunctionLikeNode = createTypeCheckFunction([
"ArrowFunctionExpression",
"FunctionExpression",
+16 -79
View File
@@ -14,6 +14,22 @@ import isFlowKeywordType from "./is-flow-keyword-type.js";
import isLineComment from "./is-line-comment.js";
import isNodeMatches from "./is-node-matches.js";
import isTsKeywordType from "./is-ts-keyword-type.js";
import {
isArrayExpression,
isBinaryCastExpression,
isBinaryish,
isConditionalType,
isExportDeclaration,
isFunctionOrArrowExpression,
isIntersectionType,
isJsxElement,
isLiteral,
isObjectExpression,
isObjectType,
isReturnOrThrowStatement,
isTypeAlias,
isUnionType,
} from "./node-types.js";
/**
@import {Node, NodeMap, Comment} from "../types/estree.js";
@@ -95,20 +111,6 @@ function getLeftSidePathName(node) {
throw new Error("Unexpected node has no left side.");
}
const isExportDeclaration = createTypeCheckFunction([
"ExportDefaultDeclaration",
"DeclareExportDeclaration",
"ExportNamedDeclaration",
"ExportAllDeclaration",
"DeclareExportAllDeclaration",
]);
// These two functions exists because we used support `recordAndTuple`
// Feel free to check `node.type` instead
// https://github.com/prettier/prettier/pull/17363
const isArrayExpression = createTypeCheckFunction(["ArrayExpression"]);
const isObjectExpression = createTypeCheckFunction(["ObjectExpression"]);
/**
* @param {Node} node
* @returns {boolean}
@@ -166,17 +168,6 @@ function isRegExpLiteral(node) {
);
}
const isLiteral = createTypeCheckFunction([
"Literal",
"BooleanLiteral",
"BigIntLiteral", // Babel, flow use `BigIntLiteral` too
"DirectiveLiteral",
"NullLiteral",
"NumericLiteral",
"RegExpLiteral",
"StringLiteral",
]);
const isSingleWordType = createTypeCheckFunction([
"Identifier",
"ThisExpression",
@@ -185,17 +176,6 @@ const isSingleWordType = createTypeCheckFunction([
"PrivateIdentifier",
]);
const isObjectType = createTypeCheckFunction([
"ObjectTypeAnnotation",
"TSTypeLiteral",
"TSMappedType",
]);
const isFunctionOrArrowExpression = createTypeCheckFunction([
"FunctionExpression",
"ArrowFunctionExpression",
]);
/**
* @param {Node} node
* @returns {boolean}
@@ -226,8 +206,6 @@ function isAngularTestWrapper(node) {
);
}
const isJsxElement = createTypeCheckFunction(["JSXElement", "JSXFragment"]);
function isMethod(node) {
return (
(node.method && node.kind === "init") ||
@@ -266,12 +244,6 @@ function isTypeAnnotationAFunction(node) {
);
}
const isBinaryish = createTypeCheckFunction([
"BinaryExpression",
"LogicalExpression",
"NGPipeExpression",
]);
/**
* @param {Node} node
* @returns {boolean}
@@ -1061,42 +1033,12 @@ function isObjectProperty(node) {
);
}
const isBinaryCastExpression = createTypeCheckFunction([
// TS
"TSAsExpression",
"TSSatisfiesExpression",
// Flow
"AsExpression",
"AsConstExpression",
"SatisfiesExpression",
]);
const isUnionType = createTypeCheckFunction([
"TSUnionType",
"UnionTypeAnnotation",
]);
const isIntersectionType = createTypeCheckFunction([
"TSIntersectionType",
"IntersectionTypeAnnotation",
]);
const isConditionalType = createTypeCheckFunction([
"TSConditionalType",
"ConditionalTypeAnnotation",
]);
const isTsAsConstExpression = (node) =>
node?.type === "TSAsExpression" &&
node.typeAnnotation.type === "TSTypeReference" &&
node.typeAnnotation.typeName.type === "Identifier" &&
node.typeAnnotation.typeName.name === "const";
const isTypeAlias = createTypeCheckFunction([
"TSTypeAliasDeclaration",
"TypeAlias",
]);
function shouldUnionTypePrintOwnComments({ key, parent }) {
if (
// If it's `types` of union type, parent will print comment for it
@@ -1123,11 +1065,6 @@ function isBooleanTypeCoercion(node) {
);
}
const isReturnOrThrowStatement = createTypeCheckFunction([
"ReturnStatement",
"ThrowStatement",
]);
export {
CommentCheckFlags,
createTypeCheckFunction,
+83
View File
@@ -0,0 +1,83 @@
import createTypeCheckFunction from "./create-type-check-function.js";
export const isBinaryCastExpression = createTypeCheckFunction([
// TS
"TSAsExpression",
"TSSatisfiesExpression",
// Flow
"AsExpression",
"AsConstExpression",
"SatisfiesExpression",
]);
export const isUnionType = createTypeCheckFunction([
"TSUnionType",
"UnionTypeAnnotation",
]);
export const isIntersectionType = createTypeCheckFunction([
"TSIntersectionType",
"IntersectionTypeAnnotation",
]);
export const isConditionalType = createTypeCheckFunction([
"TSConditionalType",
"ConditionalTypeAnnotation",
]);
export const isTypeAlias = createTypeCheckFunction([
"TSTypeAliasDeclaration",
"TypeAlias",
]);
export const isReturnOrThrowStatement = createTypeCheckFunction([
"ReturnStatement",
"ThrowStatement",
]);
export const isExportDeclaration = createTypeCheckFunction([
"ExportDefaultDeclaration",
"DeclareExportDeclaration",
"ExportNamedDeclaration",
"ExportAllDeclaration",
"DeclareExportAllDeclaration",
]);
// These two functions exists because we used support `recordAndTuple`
// Feel free to check `node.type` instead
// https://github.com/prettier/prettier/pull/17363
export const isArrayExpression = createTypeCheckFunction(["ArrayExpression"]);
export const isObjectExpression = createTypeCheckFunction(["ObjectExpression"]);
export const isLiteral = createTypeCheckFunction([
"Literal",
"BooleanLiteral",
"BigIntLiteral", // Babel, flow use `BigIntLiteral` too
"DirectiveLiteral",
"NullLiteral",
"NumericLiteral",
"RegExpLiteral",
"StringLiteral",
]);
export const isObjectType = createTypeCheckFunction([
"ObjectTypeAnnotation",
"TSTypeLiteral",
"TSMappedType",
]);
export const isFunctionOrArrowExpression = createTypeCheckFunction([
"FunctionExpression",
"ArrowFunctionExpression",
]);
export const isJsxElement = createTypeCheckFunction([
"JSXElement",
"JSXFragment",
]);
export const isBinaryish = createTypeCheckFunction([
"BinaryExpression",
"LogicalExpression",
"NGPipeExpression",
]);