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

139 行
4.4 KiB

  1. /**
  2. * @fileoverview A rule to control the style of variable initializations.
  3. * @author Colin Ihrig
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Checks whether or not a given node is a for loop.
  11. * @param {ASTNode} block - A node to check.
  12. * @returns {boolean} `true` when the node is a for loop.
  13. */
  14. function isForLoop(block) {
  15. return block.type === "ForInStatement" ||
  16. block.type === "ForOfStatement" ||
  17. block.type === "ForStatement";
  18. }
  19. /**
  20. * Checks whether or not a given declarator node has its initializer.
  21. * @param {ASTNode} node - A declarator node to check.
  22. * @returns {boolean} `true` when the node has its initializer.
  23. */
  24. function isInitialized(node) {
  25. const declaration = node.parent;
  26. const block = declaration.parent;
  27. if (isForLoop(block)) {
  28. if (block.type === "ForStatement") {
  29. return block.init === declaration;
  30. }
  31. return block.left === declaration;
  32. }
  33. return Boolean(node.init);
  34. }
  35. //------------------------------------------------------------------------------
  36. // Rule Definition
  37. //------------------------------------------------------------------------------
  38. module.exports = {
  39. meta: {
  40. docs: {
  41. description: "require or disallow initialization in variable declarations",
  42. category: "Variables",
  43. recommended: false,
  44. url: "https://eslint.org/docs/rules/init-declarations"
  45. },
  46. schema: {
  47. anyOf: [
  48. {
  49. type: "array",
  50. items: [
  51. {
  52. enum: ["always"]
  53. }
  54. ],
  55. minItems: 0,
  56. maxItems: 1
  57. },
  58. {
  59. type: "array",
  60. items: [
  61. {
  62. enum: ["never"]
  63. },
  64. {
  65. type: "object",
  66. properties: {
  67. ignoreForLoopInit: {
  68. type: "boolean"
  69. }
  70. },
  71. additionalProperties: false
  72. }
  73. ],
  74. minItems: 0,
  75. maxItems: 2
  76. }
  77. ]
  78. }
  79. },
  80. create(context) {
  81. const MODE_ALWAYS = "always",
  82. MODE_NEVER = "never";
  83. const mode = context.options[0] || MODE_ALWAYS;
  84. const params = context.options[1] || {};
  85. //--------------------------------------------------------------------------
  86. // Public API
  87. //--------------------------------------------------------------------------
  88. return {
  89. "VariableDeclaration:exit"(node) {
  90. const kind = node.kind,
  91. declarations = node.declarations;
  92. for (let i = 0; i < declarations.length; ++i) {
  93. const declaration = declarations[i],
  94. id = declaration.id,
  95. initialized = isInitialized(declaration),
  96. isIgnoredForLoop = params.ignoreForLoopInit && isForLoop(node.parent);
  97. if (id.type !== "Identifier") {
  98. continue;
  99. }
  100. if (mode === MODE_ALWAYS && !initialized) {
  101. context.report({
  102. node: declaration,
  103. message: "Variable '{{idName}}' should be initialized on declaration.",
  104. data: {
  105. idName: id.name
  106. }
  107. });
  108. } else if (mode === MODE_NEVER && kind !== "const" && initialized && !isIgnoredForLoop) {
  109. context.report({
  110. node: declaration,
  111. message: "Variable '{{idName}}' should not be initialized on declaration.",
  112. data: {
  113. idName: id.name
  114. }
  115. });
  116. }
  117. }
  118. }
  119. };
  120. }
  121. };