handle-callback-err.js 3.0 KB

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