aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/switchFinalBreakRule.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/tslint/lib/rules/switchFinalBreakRule.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/tslint/lib/rules/switchFinalBreakRule.js')
-rw-r--r--node_modules/tslint/lib/rules/switchFinalBreakRule.js47
1 files changed, 32 insertions, 15 deletions
diff --git a/node_modules/tslint/lib/rules/switchFinalBreakRule.js b/node_modules/tslint/lib/rules/switchFinalBreakRule.js
index d2477edab..bc6417eab 100644
--- a/node_modules/tslint/lib/rules/switchFinalBreakRule.js
+++ b/node_modules/tslint/lib/rules/switchFinalBreakRule.js
@@ -33,7 +33,8 @@ var Rule = /** @class */ (function (_super) {
Rule.metadata = {
ruleName: "switch-final-break",
description: "Checks whether the final clause of a switch statement ends in \`break;\`.",
- optionsDescription: (_a = ["\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."], _a.raw = ["\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."], Lint.Utils.dedent(_a)),
+ hasFix: true,
+ 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."]))),
options: {
type: "string",
enum: [
@@ -65,29 +66,45 @@ function walk(ctx) {
}
if (always) {
if (!tsutils_1.endsControlFlow(clause)) {
- ctx.addFailureAtNode(clause.getChildAt(0), Rule.FAILURE_STRING_ALWAYS);
+ ctx.addFailureAtNode(clause.getChildAt(0), Rule.FAILURE_STRING_ALWAYS, createAddFix(clause));
}
return;
}
- if (clause.statements.length === 0) {
+ var lastStatement = getLastStatement(clause);
+ if (lastStatement === undefined || !tsutils_1.isBreakStatement(lastStatement)) {
return;
}
- var block = clause.statements[0];
- var statements = clause.statements.length === 1 && tsutils_1.isBlock(block) ? block.statements : clause.statements;
- var lastStatement = last(statements);
- if (lastStatement !== undefined && tsutils_1.isBreakStatement(lastStatement)) {
- if (lastStatement.label !== undefined) {
- var parent = node.parent;
- if (!tsutils_1.isLabeledStatement(parent) || parent.label === lastStatement.label) {
- // break jumps somewhere else, don't complain
- return;
- }
+ if (lastStatement.label !== undefined) {
+ var parent = node.parent;
+ if (!tsutils_1.isLabeledStatement(parent) || parent.label === lastStatement.label) {
+ // break jumps somewhere else, don't complain
+ return;
}
- ctx.addFailureAtNode(lastStatement, Rule.FAILURE_STRING_NEVER);
}
+ ctx.addFailureAtNode(lastStatement, Rule.FAILURE_STRING_NEVER, createRemoveFix(lastStatement));
}
+ function createAddFix(clause) {
+ var lastStatement = getLastStatement(clause);
+ if (lastStatement === undefined) {
+ return Lint.Replacement.appendText(clause.end, " break;");
+ }
+ var fullText = lastStatement.getFullText(ctx.sourceFile);
+ var indentation = fullText.slice(0, fullText.search(/\S+/));
+ return Lint.Replacement.appendText(lastStatement.end, indentation + "break;");
+ }
+ function createRemoveFix(lastStatement) {
+ return Lint.Replacement.replaceFromTo(lastStatement.getFullStart(), lastStatement.end, "");
+ }
+}
+function getLastStatement(clause) {
+ if (clause.statements.length === 0) {
+ return undefined;
+ }
+ var block = clause.statements[0];
+ var statements = clause.statements.length === 1 && tsutils_1.isBlock(block) ? block.statements : clause.statements;
+ return last(statements);
}
function last(arr) {
return arr[arr.length - 1];
}
-var _a;
+var templateObject_1;