项目原始demo,不改动
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。
 
 
 
 

91 行
2.8 KiB

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