aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/oneLineRule.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/tslint/lib/rules/oneLineRule.js')
-rw-r--r--node_modules/tslint/lib/rules/oneLineRule.js280
1 files changed, 81 insertions, 199 deletions
diff --git a/node_modules/tslint/lib/rules/oneLineRule.js b/node_modules/tslint/lib/rules/oneLineRule.js
index dcbd2169d..5d271ae91 100644
--- a/node_modules/tslint/lib/rules/oneLineRule.js
+++ b/node_modules/tslint/lib/rules/oneLineRule.js
@@ -31,8 +31,13 @@ var Rule = /** @class */ (function (_super) {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.apply = function (sourceFile) {
- var oneLineWalker = new OneLineWalker(sourceFile, this.getOptions());
- return this.applyWithWalker(oneLineWalker);
+ return this.applyWithWalker(new OneLineWalker(sourceFile, this.ruleName, {
+ brace: this.ruleArguments.indexOf(OPTION_BRACE) !== -1,
+ catch: this.ruleArguments.indexOf(OPTION_CATCH) !== -1,
+ else: this.ruleArguments.indexOf(OPTION_ELSE) !== -1,
+ finally: this.ruleArguments.indexOf(OPTION_FINALLY) !== -1,
+ whitespace: this.ruleArguments.indexOf(OPTION_WHITESPACE) !== -1,
+ }));
};
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
@@ -51,12 +56,9 @@ var Rule = /** @class */ (function (_super) {
optionExamples: [[true, OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE]],
type: "style",
typescriptOnly: false,
+ hasFix: true,
};
/* tslint:enable:object-literal-sort-keys */
- Rule.BRACE_FAILURE_STRING = "misplaced opening brace";
- Rule.CATCH_FAILURE_STRING = "misplaced 'catch'";
- Rule.ELSE_FAILURE_STRING = "misplaced 'else'";
- Rule.FINALLY_FAILURE_STRING = "misplaced 'finally'";
Rule.WHITESPACE_FAILURE_STRING = "missing whitespace";
return Rule;
}(Lint.Rules.AbstractRule));
@@ -66,204 +68,84 @@ var OneLineWalker = /** @class */ (function (_super) {
function OneLineWalker() {
return _super !== null && _super.apply(this, arguments) || this;
}
- OneLineWalker.prototype.visitIfStatement = function (node) {
- var thenStatement = node.thenStatement;
- var thenIsBlock = thenStatement.kind === ts.SyntaxKind.Block;
- if (thenIsBlock) {
- var expressionCloseParen = node.getChildAt(3);
- var thenOpeningBrace = thenStatement.getChildAt(0);
- this.handleOpeningBrace(expressionCloseParen, thenOpeningBrace);
- }
- var elseStatement = node.elseStatement;
- if (elseStatement != null) {
- // find the else keyword
- var elseKeyword = Lint.childOfKind(node, ts.SyntaxKind.ElseKeyword);
- if (elseStatement.kind === ts.SyntaxKind.Block) {
- var elseOpeningBrace = elseStatement.getChildAt(0);
- this.handleOpeningBrace(elseKeyword, elseOpeningBrace);
- }
- if (thenIsBlock && this.hasOption(OPTION_ELSE)) {
- var thenStatementEndLine = this.getLineAndCharacterOfPosition(thenStatement.getEnd()).line;
- var elseKeywordLine = this.getLineAndCharacterOfPosition(elseKeyword.getStart()).line;
- if (thenStatementEndLine !== elseKeywordLine) {
- this.addFailureAtNode(elseKeyword, Rule.ELSE_FAILURE_STRING);
+ OneLineWalker.prototype.walk = function (sourceFile) {
+ var _this = this;
+ var cb = function (node) {
+ switch (node.kind) {
+ case ts.SyntaxKind.Block:
+ if (!tsutils_1.isBlockLike(node.parent)) {
+ _this.check({ pos: node.pos, end: node.statements.pos });
+ }
+ break;
+ case ts.SyntaxKind.CaseBlock:
+ _this.check({ pos: node.pos, end: node.clauses.pos });
+ break;
+ case ts.SyntaxKind.ModuleBlock:
+ _this.check({ pos: node.pos, end: node.statements.pos });
+ break;
+ case ts.SyntaxKind.EnumDeclaration:
+ _this.check({ pos: node.name.end, end: node.members.pos });
+ break;
+ case ts.SyntaxKind.InterfaceDeclaration:
+ case ts.SyntaxKind.ClassDeclaration:
+ case ts.SyntaxKind.ClassExpression: {
+ _this.check(tsutils_1.getChildOfKind(node, ts.SyntaxKind.OpenBraceToken, sourceFile));
+ break;
}
- }
- }
- _super.prototype.visitIfStatement.call(this, node);
- };
- OneLineWalker.prototype.visitCatchClause = function (node) {
- var catchClosingParen = Lint.childOfKind(node, ts.SyntaxKind.CloseParenToken);
- var catchOpeningBrace = node.block.getChildAt(0);
- this.handleOpeningBrace(catchClosingParen, catchOpeningBrace);
- _super.prototype.visitCatchClause.call(this, node);
- };
- OneLineWalker.prototype.visitTryStatement = function (node) {
- var catchClause = node.catchClause;
- var finallyBlock = node.finallyBlock;
- var finallyKeyword = Lint.childOfKind(node, ts.SyntaxKind.FinallyKeyword);
- // "visit" try block
- var tryKeyword = node.getChildAt(0);
- var tryBlock = node.tryBlock;
- var tryOpeningBrace = tryBlock.getChildAt(0);
- this.handleOpeningBrace(tryKeyword, tryOpeningBrace);
- if (this.hasOption(OPTION_CATCH) && catchClause != null) {
- var tryClosingBrace = node.tryBlock.getChildAt(node.tryBlock.getChildCount() - 1);
- var catchKeyword = catchClause.getChildAt(0);
- var tryClosingBraceLine = this.getLineAndCharacterOfPosition(tryClosingBrace.getEnd()).line;
- var catchKeywordLine = this.getLineAndCharacterOfPosition(catchKeyword.getStart()).line;
- if (tryClosingBraceLine !== catchKeywordLine) {
- this.addFailureAtNode(catchKeyword, Rule.CATCH_FAILURE_STRING);
- }
- }
- if (finallyBlock != null && finallyKeyword != null) {
- var finallyOpeningBrace = finallyBlock.getChildAt(0);
- this.handleOpeningBrace(finallyKeyword, finallyOpeningBrace);
- if (this.hasOption(OPTION_FINALLY)) {
- var previousBlock = catchClause != null ? catchClause.block : node.tryBlock;
- var closingBrace = previousBlock.getChildAt(previousBlock.getChildCount() - 1);
- var closingBraceLine = this.getLineAndCharacterOfPosition(closingBrace.getEnd()).line;
- var finallyKeywordLine = this.getLineAndCharacterOfPosition(finallyKeyword.getStart()).line;
- if (closingBraceLine !== finallyKeywordLine) {
- this.addFailureAtNode(finallyKeyword, Rule.FINALLY_FAILURE_STRING);
+ case ts.SyntaxKind.IfStatement: {
+ var _a = node, thenStatement = _a.thenStatement, elseStatement = _a.elseStatement;
+ if (elseStatement !== undefined && thenStatement.kind === ts.SyntaxKind.Block) {
+ _this.check({ pos: thenStatement.end, end: elseStatement.pos }, "else");
+ }
+ break;
+ }
+ case ts.SyntaxKind.TryStatement: {
+ var _b = node, finallyBlock = _b.finallyBlock, catchClause = _b.catchClause, tryBlock = _b.tryBlock;
+ if (catchClause !== undefined) {
+ _this.check(catchClause.getChildAt(0, sourceFile), "catch");
+ if (finallyBlock !== undefined) {
+ _this.check({ pos: catchClause.end, end: finallyBlock.pos }, "finally");
+ }
+ }
+ else if (finallyBlock !== undefined) {
+ _this.check({ pos: tryBlock.end, end: finallyBlock.pos }, "finally");
+ }
+ break;
+ }
+ case ts.SyntaxKind.BinaryExpression: {
+ var _c = node, operatorToken = _c.operatorToken, right = _c.right;
+ if (operatorToken.kind === ts.SyntaxKind.EqualsToken && tsutils_1.isObjectLiteralExpression(right)) {
+ _this.check({ pos: right.pos, end: right.properties.pos });
+ }
+ break;
+ }
+ case ts.SyntaxKind.VariableDeclaration: {
+ var initializer = node.initializer;
+ if (initializer !== undefined && tsutils_1.isObjectLiteralExpression(initializer)) {
+ _this.check({ pos: initializer.pos, end: initializer.properties.pos });
+ }
+ break;
+ }
+ case ts.SyntaxKind.TypeAliasDeclaration: {
+ var type = node.type;
+ if (type.kind === ts.SyntaxKind.MappedType || type.kind === ts.SyntaxKind.TypeLiteral) {
+ _this.check(type.getChildAt(0, sourceFile));
+ }
}
}
+ return ts.forEachChild(node, cb);
+ };
+ return ts.forEachChild(sourceFile, cb);
+ };
+ OneLineWalker.prototype.check = function (range, kind) {
+ var tokenStart = range.end - (kind === undefined ? 1 : kind.length);
+ if (this.options[kind === undefined ? "brace" : kind] && !tsutils_1.isSameLine(this.sourceFile, range.pos, tokenStart)) {
+ this.addFailure(tokenStart, range.end, "misplaced " + (kind === undefined ? "opening brace" : "'" + kind + "'"), Lint.Replacement.replaceFromTo(range.pos, tokenStart, this.options.whitespace ? " " : ""));
}
- _super.prototype.visitTryStatement.call(this, node);
- };
- OneLineWalker.prototype.visitForStatement = function (node) {
- this.handleIterationStatement(node);
- _super.prototype.visitForStatement.call(this, node);
- };
- OneLineWalker.prototype.visitForInStatement = function (node) {
- this.handleIterationStatement(node);
- _super.prototype.visitForInStatement.call(this, node);
- };
- OneLineWalker.prototype.visitWhileStatement = function (node) {
- this.handleIterationStatement(node);
- _super.prototype.visitWhileStatement.call(this, node);
- };
- OneLineWalker.prototype.visitBinaryExpression = function (node) {
- var rightkind = node.right.kind;
- var opkind = node.operatorToken.kind;
- if (opkind === ts.SyntaxKind.EqualsToken && rightkind === ts.SyntaxKind.ObjectLiteralExpression) {
- var equalsToken = node.getChildAt(1);
- var openBraceToken = node.right.getChildAt(0);
- this.handleOpeningBrace(equalsToken, openBraceToken);
- }
- _super.prototype.visitBinaryExpression.call(this, node);
- };
- OneLineWalker.prototype.visitVariableDeclaration = function (node) {
- var initializer = node.initializer;
- if (initializer != null && initializer.kind === ts.SyntaxKind.ObjectLiteralExpression) {
- var equalsToken = Lint.childOfKind(node, ts.SyntaxKind.EqualsToken);
- var openBraceToken = initializer.getChildAt(0);
- this.handleOpeningBrace(equalsToken, openBraceToken);
- }
- _super.prototype.visitVariableDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitDoStatement = function (node) {
- var doKeyword = node.getChildAt(0);
- var statement = node.statement;
- if (statement.kind === ts.SyntaxKind.Block) {
- var openBraceToken = statement.getChildAt(0);
- this.handleOpeningBrace(doKeyword, openBraceToken);
- }
- _super.prototype.visitDoStatement.call(this, node);
- };
- OneLineWalker.prototype.visitModuleDeclaration = function (node) {
- var nameNode = node.name;
- var body = node.body;
- if (body != null && body.kind === ts.SyntaxKind.ModuleBlock) {
- var openBraceToken = body.getChildAt(0);
- this.handleOpeningBrace(nameNode, openBraceToken);
- }
- _super.prototype.visitModuleDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitEnumDeclaration = function (node) {
- var nameNode = node.name;
- var openBraceToken = Lint.childOfKind(node, ts.SyntaxKind.OpenBraceToken);
- this.handleOpeningBrace(nameNode, openBraceToken);
- _super.prototype.visitEnumDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitSwitchStatement = function (node) {
- var closeParenToken = node.getChildAt(3);
- var openBraceToken = node.caseBlock.getChildAt(0);
- this.handleOpeningBrace(closeParenToken, openBraceToken);
- _super.prototype.visitSwitchStatement.call(this, node);
- };
- OneLineWalker.prototype.visitInterfaceDeclaration = function (node) {
- this.handleClassLikeDeclaration(node);
- _super.prototype.visitInterfaceDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitClassDeclaration = function (node) {
- this.handleClassLikeDeclaration(node);
- _super.prototype.visitClassDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitFunctionDeclaration = function (node) {
- this.handleFunctionLikeDeclaration(node);
- _super.prototype.visitFunctionDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitMethodDeclaration = function (node) {
- this.handleFunctionLikeDeclaration(node);
- _super.prototype.visitMethodDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitConstructorDeclaration = function (node) {
- this.handleFunctionLikeDeclaration(node);
- _super.prototype.visitConstructorDeclaration.call(this, node);
- };
- OneLineWalker.prototype.visitArrowFunction = function (node) {
- var body = node.body;
- if (body != null && body.kind === ts.SyntaxKind.Block) {
- var arrowToken = Lint.childOfKind(node, ts.SyntaxKind.EqualsGreaterThanToken);
- var openBraceToken = body.getChildAt(0);
- this.handleOpeningBrace(arrowToken, openBraceToken);
- }
- _super.prototype.visitArrowFunction.call(this, node);
- };
- OneLineWalker.prototype.handleFunctionLikeDeclaration = function (node) {
- var body = node.body;
- if (body != null && body.kind === ts.SyntaxKind.Block) {
- var openBraceToken = body.getChildAt(0);
- if (node.type != null) {
- this.handleOpeningBrace(node.type, openBraceToken);
- }
- else {
- var closeParenToken = Lint.childOfKind(node, ts.SyntaxKind.CloseParenToken);
- this.handleOpeningBrace(closeParenToken, openBraceToken);
- }
- }
- };
- OneLineWalker.prototype.handleClassLikeDeclaration = function (node) {
- var openBraceToken = Lint.childOfKind(node, ts.SyntaxKind.OpenBraceToken);
- this.handleOpeningBrace(tsutils_1.getPreviousToken(openBraceToken), openBraceToken);
- };
- OneLineWalker.prototype.handleIterationStatement = function (node) {
- // last child is the statement, second to last child is the close paren
- var closeParenToken = node.getChildAt(node.getChildCount() - 2);
- var statement = node.statement;
- if (statement.kind === ts.SyntaxKind.Block) {
- var openBraceToken = statement.getChildAt(0);
- this.handleOpeningBrace(closeParenToken, openBraceToken);
- }
- };
- OneLineWalker.prototype.handleOpeningBrace = function (previousNode, openBraceToken) {
- if (previousNode == null || openBraceToken == null) {
- return;
- }
- var previousNodeLine = this.getLineAndCharacterOfPosition(previousNode.getEnd()).line;
- var openBraceLine = this.getLineAndCharacterOfPosition(openBraceToken.getStart()).line;
- var failure;
- if (this.hasOption(OPTION_BRACE) && previousNodeLine !== openBraceLine) {
- failure = Rule.BRACE_FAILURE_STRING;
- }
- else if (this.hasOption(OPTION_WHITESPACE) && previousNode.getEnd() === openBraceToken.getStart()) {
- failure = Rule.WHITESPACE_FAILURE_STRING;
- }
- if (failure !== undefined) {
- this.addFailureAtNode(openBraceToken, failure);
+ else if (this.options.whitespace && range.pos === tokenStart) {
+ this.addFailure(tokenStart, range.end, Rule.WHITESPACE_FAILURE_STRING, Lint.Replacement.appendText(range.pos, " "));
}
};
return OneLineWalker;
-}(Lint.RuleWalker));
+}(Lint.AbstractWalker));
var _a;