项目原始demo,不改动
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
Це архівний репозитарій. Ви можете переглядати і клонувати файли, але не можете робити пуш або відкривати питання/запити.
 
 
 
 

143 рядки
4.1 KiB

  1. /**
  2. * @fileoverview Disallow Labeled Statements
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "disallow labeled statements",
  17. category: "Best Practices",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/no-labels"
  20. },
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. allowLoop: {
  26. type: "boolean"
  27. },
  28. allowSwitch: {
  29. type: "boolean"
  30. }
  31. },
  32. additionalProperties: false
  33. }
  34. ]
  35. },
  36. create(context) {
  37. const options = context.options[0];
  38. const allowLoop = Boolean(options && options.allowLoop);
  39. const allowSwitch = Boolean(options && options.allowSwitch);
  40. let scopeInfo = null;
  41. /**
  42. * Gets the kind of a given node.
  43. *
  44. * @param {ASTNode} node - A node to get.
  45. * @returns {string} The kind of the node.
  46. */
  47. function getBodyKind(node) {
  48. if (astUtils.isLoop(node)) {
  49. return "loop";
  50. }
  51. if (node.type === "SwitchStatement") {
  52. return "switch";
  53. }
  54. return "other";
  55. }
  56. /**
  57. * Checks whether the label of a given kind is allowed or not.
  58. *
  59. * @param {string} kind - A kind to check.
  60. * @returns {boolean} `true` if the kind is allowed.
  61. */
  62. function isAllowed(kind) {
  63. switch (kind) {
  64. case "loop": return allowLoop;
  65. case "switch": return allowSwitch;
  66. default: return false;
  67. }
  68. }
  69. /**
  70. * Checks whether a given name is a label of a loop or not.
  71. *
  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. };