]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/handle-callback-err.js
2 * @fileoverview Ensure handling of errors when we know they exist.
3 * @author Jamund Ferguson
4 * @deprecated in ESLint v7.0.0
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 /** @type {import('../shared/types').Rule} */
23 description
: "Require error handling in callbacks",
25 url
: "https://eslint.org/docs/rules/handle-callback-err"
34 expected
: "Expected error to be handled."
40 const errorArgument
= context
.options
[0] || "err";
43 * Checks if the given argument should be interpreted as a regexp pattern.
44 * @param {string} stringToCheck The string which should be checked.
45 * @returns {boolean} Whether or not the string should be interpreted as a pattern.
47 function isPattern(stringToCheck
) {
48 const firstChar
= stringToCheck
[0];
50 return firstChar
=== "^";
54 * Checks if the given name matches the configured error argument.
55 * @param {string} name The name which should be compared.
56 * @returns {boolean} Whether or not the given name matches the configured error variable name.
58 function matchesConfiguredErrorName(name
) {
59 if (isPattern(errorArgument
)) {
60 const regexp
= new RegExp(errorArgument
, "u");
62 return regexp
.test(name
);
64 return name
=== errorArgument
;
68 * Get the parameters of a given function scope.
69 * @param {Object} scope The function scope.
70 * @returns {Array} All parameters of the given scope.
72 function getParameters(scope
) {
73 return scope
.variables
.filter(variable
=> variable
.defs
[0] && variable
.defs
[0].type
=== "Parameter");
77 * Check to see if we're handling the error object properly.
78 * @param {ASTNode} node The AST node to check.
81 function checkForError(node
) {
82 const scope
= context
.getScope(),
83 parameters
= getParameters(scope
),
84 firstParameter
= parameters
[0];
86 if (firstParameter
&& matchesConfiguredErrorName(firstParameter
.name
)) {
87 if (firstParameter
.references
.length
=== 0) {
88 context
.report({ node
, messageId
: "expected" });
94 FunctionDeclaration
: checkForError
,
95 FunctionExpression
: checkForError
,
96 ArrowFunctionExpression
: checkForError