handle-callback-err.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../types').Rule.RuleModule} */
  11. module.exports = {
  12. meta: {
  13. deprecated: {
  14. message: "Node.js rules were moved out of ESLint core.",
  15. url: "https://eslint.org/docs/latest/use/migrating-to-7.0.0#deprecate-node-rules",
  16. deprecatedSince: "7.0.0",
  17. availableUntil: "11.0.0",
  18. replacedBy: [
  19. {
  20. message:
  21. "eslint-plugin-n now maintains deprecated Node.js-related rules.",
  22. plugin: {
  23. name: "eslint-plugin-n",
  24. url: "https://github.com/eslint-community/eslint-plugin-n",
  25. },
  26. rule: {
  27. name: "handle-callback-err",
  28. url: "https://github.com/eslint-community/eslint-plugin-n/tree/master/docs/rules/handle-callback-err.md",
  29. },
  30. },
  31. ],
  32. },
  33. type: "suggestion",
  34. docs: {
  35. description: "Require error handling in callbacks",
  36. recommended: false,
  37. url: "https://eslint.org/docs/latest/rules/handle-callback-err",
  38. },
  39. schema: [
  40. {
  41. type: "string",
  42. },
  43. ],
  44. messages: {
  45. expected: "Expected error to be handled.",
  46. },
  47. },
  48. create(context) {
  49. const errorArgument = context.options[0] || "err";
  50. const sourceCode = context.sourceCode;
  51. /**
  52. * Checks if the given argument should be interpreted as a regexp pattern.
  53. * @param {string} stringToCheck The string which should be checked.
  54. * @returns {boolean} Whether or not the string should be interpreted as a pattern.
  55. */
  56. function isPattern(stringToCheck) {
  57. const firstChar = stringToCheck[0];
  58. return firstChar === "^";
  59. }
  60. /**
  61. * Checks if the given name matches the configured error argument.
  62. * @param {string} name The name which should be compared.
  63. * @returns {boolean} Whether or not the given name matches the configured error variable name.
  64. */
  65. function matchesConfiguredErrorName(name) {
  66. if (isPattern(errorArgument)) {
  67. const regexp = new RegExp(errorArgument, "u");
  68. return regexp.test(name);
  69. }
  70. return name === errorArgument;
  71. }
  72. /**
  73. * Get the parameters of a given function scope.
  74. * @param {Object} scope The function scope.
  75. * @returns {Array} All parameters of the given scope.
  76. */
  77. function getParameters(scope) {
  78. return scope.variables.filter(
  79. variable =>
  80. variable.defs[0] && variable.defs[0].type === "Parameter",
  81. );
  82. }
  83. /**
  84. * Check to see if we're handling the error object properly.
  85. * @param {ASTNode} node The AST node to check.
  86. * @returns {void}
  87. */
  88. function checkForError(node) {
  89. const scope = sourceCode.getScope(node),
  90. parameters = getParameters(scope),
  91. firstParameter = parameters[0];
  92. if (
  93. firstParameter &&
  94. matchesConfiguredErrorName(firstParameter.name)
  95. ) {
  96. if (firstParameter.references.length === 0) {
  97. context.report({ node, messageId: "expected" });
  98. }
  99. }
  100. }
  101. return {
  102. FunctionDeclaration: checkForError,
  103. FunctionExpression: checkForError,
  104. ArrowFunctionExpression: checkForError,
  105. };
  106. },
  107. };