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.

no-labels.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @fileoverview Disallow Labeled Statements
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "disallow labeled statements",
  18. category: "Best Practices",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-labels"
  21. },
  22. schema: [
  23. {
  24. type: "object",
  25. properties: {
  26. allowLoop: {
  27. type: "boolean"
  28. },
  29. allowSwitch: {
  30. type: "boolean"
  31. }
  32. },
  33. additionalProperties: false
  34. }
  35. ]
  36. },
  37. create(context) {
  38. const options = context.options[0];
  39. const allowLoop = Boolean(options && options.allowLoop);
  40. const allowSwitch = Boolean(options && options.allowSwitch);
  41. let scopeInfo = null;
  42. /**
  43. * Gets the kind of a given node.
  44. *
  45. * @param {ASTNode} node - A node to get.
  46. * @returns {string} The kind of the node.
  47. */
  48. function getBodyKind(node) {
  49. if (astUtils.isLoop(node)) {
  50. return "loop";
  51. }
  52. if (node.type === "SwitchStatement") {
  53. return "switch";
  54. }
  55. return "other";
  56. }
  57. /**
  58. * Checks whether the label of a given kind is allowed or not.
  59. *
  60. * @param {string} kind - A kind to check.
  61. * @returns {boolean} `true` if the kind is allowed.
  62. */
  63. function isAllowed(kind) {
  64. switch (kind) {
  65. case "loop": return allowLoop;
  66. case "switch": return allowSwitch;
  67. default: return false;
  68. }
  69. }
  70. /**
  71. * Checks whether a given name is a label of a loop or not.
  72. *
  73. * @param {string} label - A name of a label to check.
  74. * @returns {boolean} `true` if the name is a label of a loop.
  75. */
  76. function getKind(label) {
  77. let info = scopeInfo;
  78. while (info) {
  79. if (info.label === label) {
  80. return info.kind;
  81. }
  82. info = info.upper;
  83. }
  84. /* istanbul ignore next: syntax error */
  85. return "other";
  86. }
  87. //--------------------------------------------------------------------------
  88. // Public
  89. //--------------------------------------------------------------------------
  90. return {
  91. LabeledStatement(node) {
  92. scopeInfo = {
  93. label: node.label.name,
  94. kind: getBodyKind(node.body),
  95. upper: scopeInfo
  96. };
  97. },
  98. "LabeledStatement:exit"(node) {
  99. if (!isAllowed(scopeInfo.kind)) {
  100. context.report({
  101. node,
  102. message: "Unexpected labeled statement."
  103. });
  104. }
  105. scopeInfo = scopeInfo.upper;
  106. },
  107. BreakStatement(node) {
  108. if (node.label && !isAllowed(getKind(node.label.name))) {
  109. context.report({
  110. node,
  111. message: "Unexpected label in break statement."
  112. });
  113. }
  114. },
  115. ContinueStatement(node) {
  116. if (node.label && !isAllowed(getKind(node.label.name))) {
  117. context.report({
  118. node,
  119. message: "Unexpected label in continue statement."
  120. });
  121. }
  122. }
  123. };
  124. }
  125. };