Ohm-Management - Projektarbeit B-ME
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.

no-unsafe-finally.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. type: "problem",
  18. docs: {
  19. description: "disallow control flow statements in `finally` blocks",
  20. category: "Possible Errors",
  21. recommended: true,
  22. url: "https://eslint.org/docs/rules/no-unsafe-finally"
  23. },
  24. schema: []
  25. },
  26. create(context) {
  27. /**
  28. * Checks if the node is the finalizer of a TryStatement
  29. *
  30. * @param {ASTNode} node - node to check.
  31. * @returns {boolean} - true if the node is the finalizer of a TryStatement
  32. */
  33. function isFinallyBlock(node) {
  34. return node.parent.type === "TryStatement" && node.parent.finalizer === node;
  35. }
  36. /**
  37. * Climbs up the tree if the node is not a sentinel node
  38. *
  39. * @param {ASTNode} node - node to check.
  40. * @param {string} label - label of the break or continue statement
  41. * @returns {boolean} - return whether the node is a finally block or a sentinel node
  42. */
  43. function isInFinallyBlock(node, label) {
  44. let labelInside = false;
  45. let sentinelNodeType;
  46. if (node.type === "BreakStatement" && !node.label) {
  47. sentinelNodeType = SENTINEL_NODE_TYPE_BREAK;
  48. } else if (node.type === "ContinueStatement") {
  49. sentinelNodeType = SENTINEL_NODE_TYPE_CONTINUE;
  50. } else {
  51. sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW;
  52. }
  53. for (
  54. let currentNode = node;
  55. currentNode && !sentinelNodeType.test(currentNode.type);
  56. currentNode = currentNode.parent
  57. ) {
  58. if (currentNode.parent.label && label && (currentNode.parent.label.name === label.name)) {
  59. labelInside = true;
  60. }
  61. if (isFinallyBlock(currentNode)) {
  62. if (label && labelInside) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * Checks whether the possibly-unsafe statement is inside a finally block.
  72. *
  73. * @param {ASTNode} node - node to check.
  74. * @returns {void}
  75. */
  76. function check(node) {
  77. if (isInFinallyBlock(node, node.label)) {
  78. context.report({
  79. message: "Unsafe usage of {{nodeType}}.",
  80. data: {
  81. nodeType: node.type
  82. },
  83. node,
  84. line: node.loc.line,
  85. column: node.loc.column
  86. });
  87. }
  88. }
  89. return {
  90. ReturnStatement: check,
  91. ThrowStatement: check,
  92. BreakStatement: check,
  93. ContinueStatement: check
  94. };
  95. }
  96. };