switchFinalBreakRule.js 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2017 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 tsutils_1 = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var OPTION_ALWAYS = "always";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. return this.applyWithFunction(sourceFile, walk, { always: this.ruleArguments.indexOf(OPTION_ALWAYS) !== -1 });
  31. };
  32. /* tslint:disable:object-literal-sort-keys */
  33. Rule.metadata = {
  34. ruleName: "switch-final-break",
  35. description: "Checks whether the final clause of a switch statement ends in \`break;\`.",
  36. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n If no options are passed, a final 'break;' is forbidden.\n If the \"always\" option is passed this will require a 'break;' to always be present\n unless control flow is escaped in some other way."], ["\n If no options are passed, a final 'break;' is forbidden.\n If the \"always\" option is passed this will require a 'break;' to always be present\n unless control flow is escaped in some other way."]))),
  37. options: {
  38. type: "string",
  39. enum: [
  40. OPTION_ALWAYS,
  41. ],
  42. },
  43. optionExamples: [true, [true, OPTION_ALWAYS]],
  44. type: "style",
  45. typescriptOnly: false,
  46. };
  47. /* tslint:enable:object-literal-sort-keys */
  48. Rule.FAILURE_STRING_ALWAYS = "Final clause in 'switch' statement should end with 'break;'.";
  49. Rule.FAILURE_STRING_NEVER = "Final clause in 'switch' statement should not end with 'break;'.";
  50. return Rule;
  51. }(Lint.Rules.AbstractRule));
  52. exports.Rule = Rule;
  53. function walk(ctx) {
  54. var sourceFile = ctx.sourceFile, always = ctx.options.always;
  55. ts.forEachChild(sourceFile, function cb(node) {
  56. if (tsutils_1.isSwitchStatement(node)) {
  57. check(node);
  58. }
  59. ts.forEachChild(node, cb);
  60. });
  61. function check(node) {
  62. var clause = last(node.caseBlock.clauses);
  63. if (clause === undefined) {
  64. return;
  65. }
  66. if (always) {
  67. if (!tsutils_1.endsControlFlow(clause)) {
  68. ctx.addFailureAtNode(clause.getChildAt(0), Rule.FAILURE_STRING_ALWAYS);
  69. }
  70. return;
  71. }
  72. if (clause.statements.length === 0) {
  73. return;
  74. }
  75. var block = clause.statements[0];
  76. var statements = clause.statements.length === 1 && tsutils_1.isBlock(block) ? block.statements : clause.statements;
  77. var lastStatement = last(statements);
  78. if (lastStatement !== undefined && tsutils_1.isBreakStatement(lastStatement)) {
  79. if (lastStatement.label !== undefined) {
  80. var parent = node.parent;
  81. if (!tsutils_1.isLabeledStatement(parent) || parent.label === lastStatement.label) {
  82. // break jumps somewhere else, don't complain
  83. return;
  84. }
  85. }
  86. ctx.addFailureAtNode(lastStatement, Rule.FAILURE_STRING_NEVER);
  87. }
  88. }
  89. }
  90. function last(arr) {
  91. return arr[arr.length - 1];
  92. }
  93. var templateObject_1;