source: imaps-frontend/node_modules/eslint/lib/rules/handle-callback-err.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/**
2 * @fileoverview Ensure handling of errors when we know they exist.
3 * @author Jamund Ferguson
4 * @deprecated in ESLint v7.0.0
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Rule Definition
11//------------------------------------------------------------------------------
12
13/** @type {import('../shared/types').Rule} */
14module.exports = {
15 meta: {
16 deprecated: true,
17
18 replacedBy: [],
19
20 type: "suggestion",
21
22 docs: {
23 description: "Require error handling in callbacks",
24 recommended: false,
25 url: "https://eslint.org/docs/latest/rules/handle-callback-err"
26 },
27
28 schema: [
29 {
30 type: "string"
31 }
32 ],
33 messages: {
34 expected: "Expected error to be handled."
35 }
36 },
37
38 create(context) {
39
40 const errorArgument = context.options[0] || "err";
41 const sourceCode = context.sourceCode;
42
43 /**
44 * Checks if the given argument should be interpreted as a regexp pattern.
45 * @param {string} stringToCheck The string which should be checked.
46 * @returns {boolean} Whether or not the string should be interpreted as a pattern.
47 */
48 function isPattern(stringToCheck) {
49 const firstChar = stringToCheck[0];
50
51 return firstChar === "^";
52 }
53
54 /**
55 * Checks if the given name matches the configured error argument.
56 * @param {string} name The name which should be compared.
57 * @returns {boolean} Whether or not the given name matches the configured error variable name.
58 */
59 function matchesConfiguredErrorName(name) {
60 if (isPattern(errorArgument)) {
61 const regexp = new RegExp(errorArgument, "u");
62
63 return regexp.test(name);
64 }
65 return name === errorArgument;
66 }
67
68 /**
69 * Get the parameters of a given function scope.
70 * @param {Object} scope The function scope.
71 * @returns {Array} All parameters of the given scope.
72 */
73 function getParameters(scope) {
74 return scope.variables.filter(variable => variable.defs[0] && variable.defs[0].type === "Parameter");
75 }
76
77 /**
78 * Check to see if we're handling the error object properly.
79 * @param {ASTNode} node The AST node to check.
80 * @returns {void}
81 */
82 function checkForError(node) {
83 const scope = sourceCode.getScope(node),
84 parameters = getParameters(scope),
85 firstParameter = parameters[0];
86
87 if (firstParameter && matchesConfiguredErrorName(firstParameter.name)) {
88 if (firstParameter.references.length === 0) {
89 context.report({ node, messageId: "expected" });
90 }
91 }
92 }
93
94 return {
95 FunctionDeclaration: checkForError,
96 FunctionExpression: checkForError,
97 ArrowFunctionExpression: checkForError
98 };
99
100 }
101};
Note: See TracBrowser for help on using the repository browser.