no-labels.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. },
  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. * @param {ASTNode} node A node to get.
  47. * @returns {string} The kind of the node.
  48. */
  49. function getBodyKind(node) {
  50. if (astUtils.isLoop(node)) {
  51. return "loop";
  52. }
  53. if (node.type === "SwitchStatement") {
  54. return "switch";
  55. }
  56. return "other";
  57. }
  58. /**
  59. * Checks whether the label of a given kind is allowed or not.
  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. * @param {string} label A name of a label to check.
  73. * @returns {boolean} `true` if the name is a label of a loop.
  74. */
  75. function getKind(label) {
  76. let info = scopeInfo;
  77. while (info) {
  78. if (info.label === label) {
  79. return info.kind;
  80. }
  81. info = info.upper;
  82. }
  83. /* istanbul ignore next: syntax error */
  84. return "other";
  85. }
  86. //--------------------------------------------------------------------------
  87. // Public
  88. //--------------------------------------------------------------------------
  89. return {
  90. LabeledStatement(node) {
  91. scopeInfo = {
  92. label: node.label.name,
  93. kind: getBodyKind(node.body),
  94. upper: scopeInfo
  95. };
  96. },
  97. "LabeledStatement:exit"(node) {
  98. if (!isAllowed(scopeInfo.kind)) {
  99. context.report({
  100. node,
  101. message: "Unexpected labeled statement."
  102. });
  103. }
  104. scopeInfo = scopeInfo.upper;
  105. },
  106. BreakStatement(node) {
  107. if (node.label && !isAllowed(getKind(node.label.name))) {
  108. context.report({
  109. node,
  110. message: "Unexpected label in break statement."
  111. });
  112. }
  113. },
  114. ContinueStatement(node) {
  115. if (node.label && !isAllowed(getKind(node.label.name))) {
  116. context.report({
  117. node,
  118. message: "Unexpected label in continue statement."
  119. });
  120. }
  121. }
  122. };
  123. }
  124. };