no-labels.js 4.2 KB

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