2023-12-18 13:12:25 +08:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const getDocsUrl = require('./lib/get-docs-url')
|
|
|
|
const isPromise = require('./lib/is-promise')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
meta: {
|
2024-01-16 21:26:16 +08:00
|
|
|
type: 'problem',
|
2023-12-18 13:12:25 +08:00
|
|
|
docs: {
|
|
|
|
description:
|
|
|
|
'Ensures the proper number of arguments are passed to Promise functions',
|
2024-01-16 21:26:16 +08:00
|
|
|
url: getDocsUrl('valid-params'),
|
|
|
|
},
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
|
|
|
if (!isPromise(node)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = node.callee.property.name
|
|
|
|
const numArgs = node.arguments.length
|
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case 'resolve':
|
|
|
|
case 'reject':
|
|
|
|
if (numArgs > 1) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message:
|
|
|
|
'Promise.{{ name }}() requires 0 or 1 arguments, but received {{ numArgs }}',
|
2024-01-16 21:26:16 +08:00
|
|
|
data: { name, numArgs },
|
2023-12-18 13:12:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'then':
|
|
|
|
if (numArgs < 1 || numArgs > 2) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message:
|
|
|
|
'Promise.{{ name }}() requires 1 or 2 arguments, but received {{ numArgs }}',
|
2024-01-16 21:26:16 +08:00
|
|
|
data: { name, numArgs },
|
2023-12-18 13:12:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'race':
|
|
|
|
case 'all':
|
|
|
|
case 'catch':
|
|
|
|
case 'finally':
|
|
|
|
if (numArgs !== 1) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
message:
|
|
|
|
'Promise.{{ name }}() requires 1 argument, but received {{ numArgs }}',
|
2024-01-16 21:26:16 +08:00
|
|
|
data: { name, numArgs },
|
2023-12-18 13:12:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
},
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
},
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|