This commit is contained in:
Lilith 2025-02-27 02:26:55 +01:00
parent a71a3b5593
commit cb52890889
Signed by: lilith
GPG key ID: 8712A0F317C37175
16657 changed files with 1483086 additions and 1 deletions

View file

@ -0,0 +1,48 @@
/**
* 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,
}