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.

init-declarations.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. type: "suggestion",
  41. docs: {
  42. description: "require or disallow initialization in variable declarations",
  43. category: "Variables",
  44. recommended: false,
  45. url: "https://eslint.org/docs/rules/init-declarations"
  46. },
  47. schema: {
  48. anyOf: [
  49. {
  50. type: "array",
  51. items: [
  52. {
  53. enum: ["always"]
  54. }
  55. ],
  56. minItems: 0,
  57. maxItems: 1
  58. },
  59. {
  60. type: "array",
  61. items: [
  62. {
  63. enum: ["never"]
  64. },
  65. {
  66. type: "object",
  67. properties: {
  68. ignoreForLoopInit: {
  69. type: "boolean"
  70. }
  71. },
  72. additionalProperties: false
  73. }
  74. ],
  75. minItems: 0,
  76. maxItems: 2
  77. }
  78. ]
  79. }
  80. },
  81. create(context) {
  82. const MODE_ALWAYS = "always",
  83. MODE_NEVER = "never";
  84. const mode = context.options[0] || MODE_ALWAYS;
  85. const params = context.options[1] || {};
  86. //--------------------------------------------------------------------------
  87. // Public API
  88. //--------------------------------------------------------------------------
  89. return {
  90. "VariableDeclaration:exit"(node) {
  91. const kind = node.kind,
  92. declarations = node.declarations;
  93. for (let i = 0; i < declarations.length; ++i) {
  94. const declaration = declarations[i],
  95. id = declaration.id,
  96. initialized = isInitialized(declaration),
  97. isIgnoredForLoop = params.ignoreForLoopInit && isForLoop(node.parent);
  98. if (id.type !== "Identifier") {
  99. continue;
  100. }
  101. if (mode === MODE_ALWAYS && !initialized) {
  102. context.report({
  103. node: declaration,
  104. message: "Variable '{{idName}}' should be initialized on declaration.",
  105. data: {
  106. idName: id.name
  107. }
  108. });
  109. } else if (mode === MODE_NEVER && kind !== "const" && initialized && !isIgnoredForLoop) {
  110. context.report({
  111. node: declaration,
  112. message: "Variable '{{idName}}' should not be initialized on declaration.",
  113. data: {
  114. idName: id.name
  115. }
  116. });
  117. }
  118. }
  119. }
  120. };
  121. }
  122. };