no-labels.js 3.3 KB

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