Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

handle-callback-err.js 2.8KB

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