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

166 行
4.6 KiB

  1. /**
  2. * @fileoverview Rule to disallow empty functions.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const ALLOW_OPTIONS = Object.freeze([
  14. "functions",
  15. "arrowFunctions",
  16. "generatorFunctions",
  17. "methods",
  18. "generatorMethods",
  19. "getters",
  20. "setters",
  21. "constructors"
  22. ]);
  23. /**
  24. * Gets the kind of a given function node.
  25. *
  26. * @param {ASTNode} node - A function node to get. This is one of
  27. * an ArrowFunctionExpression, a FunctionDeclaration, or a
  28. * FunctionExpression.
  29. * @returns {string} The kind of the function. This is one of "functions",
  30. * "arrowFunctions", "generatorFunctions", "asyncFunctions", "methods",
  31. * "generatorMethods", "asyncMethods", "getters", "setters", and
  32. * "constructors".
  33. */
  34. function getKind(node) {
  35. const parent = node.parent;
  36. let kind = "";
  37. if (node.type === "ArrowFunctionExpression") {
  38. return "arrowFunctions";
  39. }
  40. // Detects main kind.
  41. if (parent.type === "Property") {
  42. if (parent.kind === "get") {
  43. return "getters";
  44. }
  45. if (parent.kind === "set") {
  46. return "setters";
  47. }
  48. kind = parent.method ? "methods" : "functions";
  49. } else if (parent.type === "MethodDefinition") {
  50. if (parent.kind === "get") {
  51. return "getters";
  52. }
  53. if (parent.kind === "set") {
  54. return "setters";
  55. }
  56. if (parent.kind === "constructor") {
  57. return "constructors";
  58. }
  59. kind = "methods";
  60. } else {
  61. kind = "functions";
  62. }
  63. // Detects prefix.
  64. let prefix = "";
  65. if (node.generator) {
  66. prefix = "generator";
  67. } else if (node.async) {
  68. prefix = "async";
  69. } else {
  70. return kind;
  71. }
  72. return prefix + kind[0].toUpperCase() + kind.slice(1);
  73. }
  74. //------------------------------------------------------------------------------
  75. // Rule Definition
  76. //------------------------------------------------------------------------------
  77. module.exports = {
  78. meta: {
  79. docs: {
  80. description: "disallow empty functions",
  81. category: "Best Practices",
  82. recommended: false,
  83. url: "https://eslint.org/docs/rules/no-empty-function"
  84. },
  85. schema: [
  86. {
  87. type: "object",
  88. properties: {
  89. allow: {
  90. type: "array",
  91. items: { enum: ALLOW_OPTIONS },
  92. uniqueItems: true
  93. }
  94. },
  95. additionalProperties: false
  96. }
  97. ],
  98. messages: {
  99. unexpected: "Unexpected empty {{name}}."
  100. }
  101. },
  102. create(context) {
  103. const options = context.options[0] || {};
  104. const allowed = options.allow || [];
  105. const sourceCode = context.getSourceCode();
  106. /**
  107. * Reports a given function node if the node matches the following patterns.
  108. *
  109. * - Not allowed by options.
  110. * - The body is empty.
  111. * - The body doesn't have any comments.
  112. *
  113. * @param {ASTNode} node - A function node to report. This is one of
  114. * an ArrowFunctionExpression, a FunctionDeclaration, or a
  115. * FunctionExpression.
  116. * @returns {void}
  117. */
  118. function reportIfEmpty(node) {
  119. const kind = getKind(node);
  120. const name = astUtils.getFunctionNameWithKind(node);
  121. const innerComments = sourceCode.getTokens(node.body, {
  122. includeComments: true,
  123. filter: astUtils.isCommentToken
  124. });
  125. if (allowed.indexOf(kind) === -1 &&
  126. node.body.type === "BlockStatement" &&
  127. node.body.body.length === 0 &&
  128. innerComments.length === 0
  129. ) {
  130. context.report({
  131. node,
  132. loc: node.body.loc.start,
  133. messageId: "unexpected",
  134. data: { name }
  135. });
  136. }
  137. }
  138. return {
  139. ArrowFunctionExpression: reportIfEmpty,
  140. FunctionDeclaration: reportIfEmpty,
  141. FunctionExpression: reportIfEmpty
  142. };
  143. }
  144. };