Files
NapCatQQ/.pnpm-store/v3/files/58/608e7deda0a5a79371b191f6b13d094cc4c70a49cf0586339735106aad6c3271a886b29f2d0bcb30014317c8e73bd6c83fa03822ec814547e498479f35cf65
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

49 lines
1.4 KiB
Plaintext

/**
* Library: isPromiseConstructor
* Makes sure that an Expression node is new Promise().
*/
'use strict'
/**
* @typedef {import('estree').Node} Node
* @typedef {import('estree').Expression} Expression
* @typedef {import('estree').NewExpression} NewExpression
* @typedef {import('estree').FunctionExpression} FunctionExpression
* @typedef {import('estree').ArrowFunctionExpression} ArrowFunctionExpression
*
* @typedef {NewExpression & { callee: { type: 'Identifier', name: 'Promise' } }} NewPromise
* @typedef {NewPromise & { arguments: [FunctionExpression | ArrowFunctionExpression] }} NewPromiseWithInlineExecutor
*
*/
/**
* Checks whether the given node is new Promise().
* @param {Node} node
* @returns {node is NewPromise}
*/
function isPromiseConstructor(node) {
return (
node.type === 'NewExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'Promise'
)
}
/**
* Checks whether the given node is new Promise(() => {}).
* @param {Node} node
* @returns {node is NewPromiseWithInlineExecutor}
*/
function isPromiseConstructorWithInlineExecutor(node) {
return (
isPromiseConstructor(node) &&
node.arguments.length === 1 &&
(node.arguments[0].type === 'FunctionExpression' ||
node.arguments[0].type === 'ArrowFunctionExpression')
)
}
module.exports = {
isPromiseConstructor,
isPromiseConstructorWithInlineExecutor,
}