no-labels.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 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. default: false
  29. },
  30. allowSwitch: {
  31. type: "boolean",
  32. default: false
  33. }
  34. },
  35. additionalProperties: false
  36. }
  37. ],
  38. messages: {
  39. unexpectedLabel: "Unexpected labeled statement.",
  40. unexpectedLabelInBreak: "Unexpected label in break statement.",
  41. unexpectedLabelInContinue: "Unexpected label in continue statement."
  42. }
  43. },
  44. create(context) {
  45. const options = context.options[0];
  46. const allowLoop = options && options.allowLoop;
  47. const allowSwitch = options && options.allowSwitch;
  48. let scopeInfo = null;
  49. /**
  50. * Gets the kind of a given node.
  51. * @param {ASTNode} node A node to get.
  52. * @returns {string} The kind of the node.
  53. */
  54. function getBodyKind(node) {
  55. if (astUtils.isLoop(node)) {
  56. return "loop";
  57. }
  58. if (node.type === "SwitchStatement") {
  59. return "switch";
  60. }
  61. return "other";
  62. }
  63. /**
  64. * Checks whether the label of a given kind is allowed or not.
  65. * @param {string} kind A kind to check.
  66. * @returns {boolean} `true` if the kind is allowed.
  67. */
  68. function isAllowed(kind) {
  69. switch (kind) {
  70. case "loop": return allowLoop;
  71. case "switch": return allowSwitch;
  72. default: return false;
  73. }
  74. }
  75. /**
  76. * Checks whether a given name is a label of a loop or not.
  77. * @param {string} label A name of a label to check.
  78. * @returns {boolean} `true` if the name is a label of a loop.
  79. */
  80. function getKind(label) {
  81. let info = scopeInfo;
  82. while (info) {
  83. if (info.label === label) {
  84. return info.kind;
  85. }
  86. info = info.upper;
  87. }
  88. /* istanbul ignore next: syntax error */
  89. return "other";
  90. }
  91. //--------------------------------------------------------------------------
  92. // Public
  93. //--------------------------------------------------------------------------
  94. return {
  95. LabeledStatement(node) {
  96. scopeInfo = {
  97. label: node.label.name,
  98. kind: getBodyKind(node.body),
  99. upper: scopeInfo
  100. };
  101. },
  102. "LabeledStatement:exit"(node) {
  103. if (!isAllowed(scopeInfo.kind)) {
  104. context.report({
  105. node,
  106. messageId: "unexpectedLabel"
  107. });
  108. }
  109. scopeInfo = scopeInfo.upper;
  110. },
  111. BreakStatement(node) {
  112. if (node.label && !isAllowed(getKind(node.label.name))) {
  113. context.report({
  114. node,
  115. messageId: "unexpectedLabelInBreak"
  116. });
  117. }
  118. },
  119. ContinueStatement(node) {
  120. if (node.label && !isAllowed(getKind(node.label.name))) {
  121. context.report({
  122. node,
  123. messageId: "unexpectedLabelInContinue"
  124. });
  125. }
  126. }
  127. };
  128. }
  129. };