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,33 @@
'use strict'
function getSourceCode(context) {
if (context.sourceCode != null) {
return context.sourceCode
}
return context.getSourceCode()
}
function getAncestors(context, node) {
const sourceCode = getSourceCode(context)
if (typeof sourceCode.getAncestors === 'function') {
return sourceCode.getAncestors(node)
}
return context.getAncestors(node)
}
function getScope(context, node) {
const sourceCode = getSourceCode(context)
if (typeof sourceCode.getScope === 'function') {
return sourceCode.getScope(node)
}
return context.getScope(node)
}
module.exports = {
getSourceCode,
getAncestors,
getScope,
}

View file

@ -0,0 +1,17 @@
'use strict'
const REPO_URL = 'https://github.com/eslint-community/eslint-plugin-promise'
/**
* Generates the URL to documentation for the given rule name. It uses the
* package version to build the link to a tagged version of the
* documentation file.
*
* @param {string} ruleName - Name of the eslint rule
* @returns {string} URL to the documentation for the given rule
*/
function getDocsUrl(ruleName) {
return `${REPO_URL}/blob/main/docs/rules/${ruleName}.md`
}
module.exports = getDocsUrl

View file

@ -0,0 +1,37 @@
/**
* Library: Has Promise Callback
* Makes sure that an Expression node is part of a promise
* with callback functions (like then() or catch())
*/
'use strict'
/**
* @typedef {import('estree').SimpleCallExpression} CallExpression
* @typedef {import('estree').MemberExpression} MemberExpression
* @typedef {import('estree').Identifier} Identifier
*
* @typedef {object} NameIsThenOrCatch
* @property {'then' | 'catch'} name
*
* @typedef {object} PropertyIsThenOrCatch
* @property {Identifier & NameIsThenOrCatch} property
*
* @typedef {object} CalleeIsPromiseCallback
* @property {MemberExpression & PropertyIsThenOrCatch} callee
*
* @typedef {CallExpression & CalleeIsPromiseCallback} HasPromiseCallback
*/
/**
* @param {import('estree').Node} node
* @returns {node is HasPromiseCallback}
*/
function hasPromiseCallback(node) {
// istanbul ignore if -- only being called within `CallExpression`
if (node.type !== 'CallExpression') return
if (node.callee.type !== 'MemberExpression') return
const propertyName = node.callee.property.name
return propertyName === 'then' || propertyName === 'catch'
}
module.exports = hasPromiseCallback

View file

@ -0,0 +1,14 @@
'use strict'
const isNamedCallback = require('./is-named-callback')
function isCallback(node, exceptions) {
const isCallExpression = node.type === 'CallExpression'
// istanbul ignore next -- always invoked on `CallExpression`
const callee = node.callee || {}
const nameIsCallback = isNamedCallback(callee.name, exceptions)
const isCB = isCallExpression && nameIsCallback
return isCB
}
module.exports = isCallback

View file

@ -0,0 +1,20 @@
'use strict'
const isInsidePromise = require('./is-inside-promise')
function isInsideCallback(node) {
const isCallExpression =
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionDeclaration' // this may be controversial
// it's totally fine to use promises inside promises
if (isInsidePromise(node)) return
const name = node.params && node.params[0] && node.params[0].name
const firstArgIsError = name === 'err' || name === 'error'
const isInACallback = isCallExpression && firstArgIsError
return isInACallback
}
module.exports = isInsideCallback

View file

@ -0,0 +1,15 @@
'use strict'
function isInsidePromise(node) {
const isFunctionExpression =
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
const parent = node.parent || {}
const callee = parent.callee || {}
const name = (callee.property && callee.property.name) || ''
const parentIsPromise = name === 'then' || name === 'catch'
const isInCB = isFunctionExpression && parentIsPromise
return isInCB
}
module.exports = isInsidePromise

View file

@ -0,0 +1,14 @@
'use strict'
let callbacks = ['done', 'cb', 'callback', 'next']
module.exports = function isNamedCallback(potentialCallbackName, exceptions) {
for (let i = 0; i < exceptions.length; i++) {
callbacks = callbacks.filter((item) => {
return item !== exceptions[i]
})
}
return callbacks.some((trueCallbackName) => {
return potentialCallbackName === trueCallbackName
})
}

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,
}

View file

@ -0,0 +1,36 @@
/**
* Library: isPromise
* Makes sure that an Expression node is part of a promise.
*/
'use strict'
const PROMISE_STATICS = require('./promise-statics')
function isPromise(expression) {
return (
// hello.then()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'then') ||
// hello.catch()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'catch') ||
// hello.finally()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'finally') ||
// somePromise.ANYTHING()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
isPromise(expression.callee.object)) ||
// Promise.STATIC_METHOD()
(expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.object.type === 'Identifier' &&
expression.callee.object.name === 'Promise' &&
PROMISE_STATICS[expression.callee.property.name])
)
}
module.exports = isPromise

View file

@ -0,0 +1,10 @@
'use strict'
module.exports = {
all: true,
allSettled: true,
any: true,
race: true,
reject: true,
resolve: true,
}