diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/curlyRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/curlyRule.js | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/node_modules/tslint/lib/rules/curlyRule.js b/node_modules/tslint/lib/rules/curlyRule.js index dbd14038d..906201a12 100644 --- a/node_modules/tslint/lib/rules/curlyRule.js +++ b/node_modules/tslint/lib/rules/curlyRule.js @@ -61,6 +61,7 @@ var Rule = /** @class */ (function (_super) { ], type: "functionality", typescriptOnly: false, + hasFix: true, }; /* tslint:enable:object-literal-sort-keys */ Rule.FAILURE_STRING_AS_NEEDED = "Block contains only one statement; remove the curly braces."; @@ -120,11 +121,33 @@ var CurlyWalker = /** @class */ (function (_super) { }; CurlyWalker.prototype.checkStatement = function (statement, node, childIndex, end) { if (end === void 0) { end = statement.end; } + var sameLine = tsutils_1.isSameLine(this.sourceFile, statement.pos, statement.end); if (statement.kind !== ts.SyntaxKind.Block && - !(this.options.ignoreSameLine && tsutils_1.isSameLine(this.sourceFile, statement.pos, statement.end))) { + !(this.options.ignoreSameLine && sameLine)) { var token = node.getChildAt(childIndex, this.sourceFile); var tokenText = ts.tokenToString(token.kind); - this.addFailure(token.end - tokenText.length, end, Rule.FAILURE_STRING_FACTORY(tokenText)); + this.addFailure(token.end - tokenText.length, end, Rule.FAILURE_STRING_FACTORY(tokenText), this.createMissingBraceFix(statement, node, sameLine)); + } + }; + /** Generate the necessary replacement to add braces to a statement that needs them. */ + CurlyWalker.prototype.createMissingBraceFix = function (statement, node, sameLine) { + if (sameLine) { + return [ + Lint.Replacement.appendText(statement.getStart(), "{ "), + Lint.Replacement.appendText(statement.getEnd(), " }"), + ]; + } + else { + var match = /\n([\t ])/.exec(node.getFullText(this.sourceFile)); // determine which character to use (tab or space) + var indentation = match === null ? + "" : + // indentation should match start of statement + match[1].repeat(ts.getLineAndCharacterOfPosition(this.sourceFile, node.getStart(this.sourceFile)).character); + var maybeCarriageReturn = this.sourceFile.text[this.sourceFile.getLineEndOfPosition(node.pos) - 1] === "\r" ? "\r" : ""; + return [ + Lint.Replacement.appendText(this.sourceFile.getLineEndOfPosition(statement.pos), " {"), + Lint.Replacement.appendText(statement.getEnd(), maybeCarriageReturn + "\n" + indentation + "}"), + ]; } }; return CurlyWalker; |