noUnsafeFinallyRule.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var utils = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. /* tslint:enable:object-literal-sort-keys */
  29. Rule.FAILURE_STRING = function (name) {
  30. return "'" + name + "' statements in finally blocks are forbidden.";
  31. };
  32. Rule.prototype.apply = function (sourceFile) {
  33. return this.applyWithFunction(sourceFile, walk);
  34. };
  35. /* tslint:disable:object-literal-sort-keys */
  36. Rule.metadata = {
  37. ruleName: "no-unsafe-finally",
  38. description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows control flow statements, such as `return`, `continue`,\n `break` and `throws` in finally blocks."], ["\n Disallows control flow statements, such as \\`return\\`, \\`continue\\`,\n \\`break\\` and \\`throws\\` in finally blocks."]))),
  39. descriptionDetails: "",
  40. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n When used inside `finally` blocks, control flow statements,\n such as `return`, `continue`, `break` and `throws`\n override any other control flow statements in the same try/catch scope.\n This is confusing and unexpected behavior."], ["\n When used inside \\`finally\\` blocks, control flow statements,\n such as \\`return\\`, \\`continue\\`, \\`break\\` and \\`throws\\`\n override any other control flow statements in the same try/catch scope.\n This is confusing and unexpected behavior."]))),
  41. optionsDescription: "Not configurable.",
  42. options: null,
  43. optionExamples: [true],
  44. type: "functionality",
  45. typescriptOnly: false,
  46. };
  47. return Rule;
  48. }(Lint.Rules.AbstractRule));
  49. exports.Rule = Rule;
  50. function walk(ctx) {
  51. var inFinally = false;
  52. ts.forEachChild(ctx.sourceFile, function cb(node) {
  53. switch (node.kind) {
  54. case ts.SyntaxKind.TryStatement:
  55. var _a = node, tryBlock = _a.tryBlock, catchClause = _a.catchClause, finallyBlock = _a.finallyBlock;
  56. ts.forEachChild(tryBlock, cb);
  57. if (catchClause !== undefined) {
  58. ts.forEachChild(catchClause, cb);
  59. }
  60. if (finallyBlock !== undefined) {
  61. var old = inFinally;
  62. inFinally = true;
  63. cb(finallyBlock);
  64. inFinally = old;
  65. }
  66. break;
  67. case ts.SyntaxKind.BreakStatement:
  68. case ts.SyntaxKind.ContinueStatement:
  69. case ts.SyntaxKind.ThrowStatement:
  70. case ts.SyntaxKind.ReturnStatement:
  71. if (inFinally && !jumpIsLocalToFinallyBlock(node)) {
  72. ctx.addFailureAtNode(node, Rule.FAILURE_STRING(printJumpKind(node)));
  73. }
  74. // falls through
  75. default:
  76. return ts.forEachChild(node, cb);
  77. }
  78. });
  79. }
  80. function jumpIsLocalToFinallyBlock(jump) {
  81. var isBreakOrContinue = utils.isBreakOrContinueStatement(jump);
  82. var label = isBreakOrContinue ? jump.label : undefined;
  83. var node = jump;
  84. // This should only be called inside a finally block, so we'll eventually reach the TryStatement case and return.
  85. while (true) {
  86. var parent = node.parent;
  87. switch (parent.kind) {
  88. case ts.SyntaxKind.TryStatement:
  89. if (parent.finallyBlock === node) {
  90. return false;
  91. }
  92. break;
  93. case ts.SyntaxKind.SwitchStatement:
  94. if (jump.kind === ts.SyntaxKind.BreakStatement && label === undefined) {
  95. return true;
  96. }
  97. break;
  98. case ts.SyntaxKind.ForInStatement:
  99. case ts.SyntaxKind.ForOfStatement:
  100. case ts.SyntaxKind.ForStatement:
  101. case ts.SyntaxKind.WhileStatement:
  102. case ts.SyntaxKind.DoStatement:
  103. if (isBreakOrContinue && label === undefined) {
  104. return true;
  105. }
  106. break;
  107. case ts.SyntaxKind.LabeledStatement: {
  108. var text = parent.label.text;
  109. if (label !== undefined && label.text === text) {
  110. return true;
  111. }
  112. break;
  113. }
  114. default:
  115. if (utils.isFunctionScopeBoundary(parent)) {
  116. // Haven't seen TryStatement yet, so the function is inside it.
  117. // No jump statement can escape a function, so the jump is local.
  118. return true;
  119. }
  120. }
  121. node = parent;
  122. }
  123. }
  124. function printJumpKind(node) {
  125. switch (node.kind) {
  126. case ts.SyntaxKind.BreakStatement:
  127. return "break";
  128. case ts.SyntaxKind.ContinueStatement:
  129. return "continue";
  130. case ts.SyntaxKind.ThrowStatement:
  131. return "throw";
  132. case ts.SyntaxKind.ReturnStatement:
  133. return "return";
  134. }
  135. }
  136. var templateObject_1, templateObject_2;