handle-callback-err.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. deprecated: true,
  14. replacedBy: [],
  15. type: "suggestion",
  16. docs: {
  17. description: "Require error handling in callbacks",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/handle-callback-err"
  20. },
  21. schema: [
  22. {
  23. type: "string"
  24. }
  25. ],
  26. messages: {
  27. expected: "Expected error to be handled."
  28. }
  29. },
  30. create(context) {
  31. const errorArgument = context.options[0] || "err";
  32. /**
  33. * Checks if the given argument should be interpreted as a regexp pattern.
  34. * @param {string} stringToCheck The string which should be checked.
  35. * @returns {boolean} Whether or not the string should be interpreted as a pattern.
  36. */
  37. function isPattern(stringToCheck) {
  38. const firstChar = stringToCheck[0];
  39. return firstChar === "^";
  40. }
  41. /**
  42. * Checks if the given name matches the configured error argument.
  43. * @param {string} name The name which should be compared.
  44. * @returns {boolean} Whether or not the given name matches the configured error variable name.
  45. */
  46. function matchesConfiguredErrorName(name) {
  47. if (isPattern(errorArgument)) {
  48. const regexp = new RegExp(errorArgument, "u");
  49. return regexp.test(name);
  50. }
  51. return name === errorArgument;
  52. }
  53. /**
  54. * Get the parameters of a given function scope.
  55. * @param {Object} scope The function scope.
  56. * @returns {Array} All parameters of the given scope.
  57. */
  58. function getParameters(scope) {
  59. return scope.variables.filter(variable => variable.defs[0] && variable.defs[0].type === "Parameter");
  60. }
  61. /**
  62. * Check to see if we're handling the error object properly.
  63. * @param {ASTNode} node The AST node to check.
  64. * @returns {void}
  65. */
  66. function checkForError(node) {
  67. const scope = context.getScope(),
  68. parameters = getParameters(scope),
  69. firstParameter = parameters[0];
  70. if (firstParameter && matchesConfiguredErrorName(firstParameter.name)) {
  71. if (firstParameter.references.length === 0) {
  72. context.report({ node, messageId: "expected" });
  73. }
  74. }
  75. }
  76. return {
  77. FunctionDeclaration: checkForError,
  78. FunctionExpression: checkForError,
  79. ArrowFunctionExpression: checkForError
  80. };
  81. }
  82. };