项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Deze repo is gearchiveerd. U kunt bestanden bekijken en het klonen, maar niet pushen of problemen/pull-requests openen.

no-unsafe-finally.js 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @fileoverview Rule to flag unsafe statements in finally block
  3. * @author Onur Temizkan
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. const SENTINEL_NODE_TYPE_RETURN_THROW = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression)$/;
  10. const SENTINEL_NODE_TYPE_BREAK = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement|SwitchStatement)$/;
  11. const SENTINEL_NODE_TYPE_CONTINUE = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement)$/;
  12. //------------------------------------------------------------------------------
  13. // Rule Definition
  14. //------------------------------------------------------------------------------
  15. module.exports = {
  16. meta: {
  17. docs: {
  18. description: "disallow control flow statements in `finally` blocks",
  19. category: "Possible Errors",
  20. recommended: true,
  21. url: "https://eslint.org/docs/rules/no-unsafe-finally"
  22. },
  23. schema: []
  24. },
  25. create(context) {
  26. /**
  27. * Checks if the node is the finalizer of a TryStatement
  28. *
  29. * @param {ASTNode} node - node to check.
  30. * @returns {boolean} - true if the node is the finalizer of a TryStatement
  31. */
  32. function isFinallyBlock(node) {
  33. return node.parent.type === "TryStatement" && node.parent.finalizer === node;
  34. }
  35. /**
  36. * Climbs up the tree if the node is not a sentinel node
  37. *
  38. * @param {ASTNode} node - node to check.
  39. * @param {string} label - label of the break or continue statement
  40. * @returns {boolean} - return whether the node is a finally block or a sentinel node
  41. */
  42. function isInFinallyBlock(node, label) {
  43. let labelInside = false;
  44. let sentinelNodeType;
  45. if (node.type === "BreakStatement" && !node.label) {
  46. sentinelNodeType = SENTINEL_NODE_TYPE_BREAK;
  47. } else if (node.type === "ContinueStatement") {
  48. sentinelNodeType = SENTINEL_NODE_TYPE_CONTINUE;
  49. } else {
  50. sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW;
  51. }
  52. for (
  53. let currentNode = node;
  54. currentNode && !sentinelNodeType.test(currentNode.type);
  55. currentNode = currentNode.parent
  56. ) {
  57. if (currentNode.parent.label && label && (currentNode.parent.label.name === label.name)) {
  58. labelInside = true;
  59. }
  60. if (isFinallyBlock(currentNode)) {
  61. if (label && labelInside) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. /**
  70. * Checks whether the possibly-unsafe statement is inside a finally block.
  71. *
  72. * @param {ASTNode} node - node to check.
  73. * @returns {void}
  74. */
  75. function check(node) {
  76. if (isInFinallyBlock(node, node.label)) {
  77. context.report({
  78. message: "Unsafe usage of {{nodeType}}.",
  79. data: {
  80. nodeType: node.type
  81. },
  82. node,
  83. line: node.loc.line,
  84. column: node.loc.column
  85. });
  86. }
  87. }
  88. return {
  89. ReturnStatement: check,
  90. ThrowStatement: check,
  91. BreakStatement: check,
  92. ContinueStatement: check
  93. };
  94. }
  95. };