4 * Copyright 2013 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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_BRACE = "check-open-brace";
24 var OPTION_CATCH = "check-catch";
25 var OPTION_ELSE = "check-else";
26 var OPTION_FINALLY = "check-finally";
27 var OPTION_WHITESPACE = "check-whitespace";
28 var Rule = /** @class */ (function (_super) {
29 tslib_1.__extends(Rule, _super);
31 return _super !== null && _super.apply(this, arguments) || this;
33 Rule.prototype.apply = function (sourceFile) {
34 return this.applyWithWalker(new OneLineWalker(sourceFile, this.ruleName, {
35 brace: this.ruleArguments.indexOf(OPTION_BRACE) !== -1,
36 catch: this.ruleArguments.indexOf(OPTION_CATCH) !== -1,
37 else: this.ruleArguments.indexOf(OPTION_ELSE) !== -1,
38 finally: this.ruleArguments.indexOf(OPTION_FINALLY) !== -1,
39 whitespace: this.ruleArguments.indexOf(OPTION_WHITESPACE) !== -1,
42 /* tslint:disable:object-literal-sort-keys */
45 description: "Requires the specified tokens to be on the same line as the expression preceding them.",
46 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Five arguments may be optionally provided:\n\n * `\"", "\"` checks that `catch` is on the same line as the closing brace for `try`.\n * `\"", "\"` checks that `finally` is on the same line as the closing brace for `catch`.\n * `\"", "\"` checks that `else` is on the same line as the closing brace for `if`.\n * `\"", "\"` checks that an open brace falls on the same line as its preceding expression.\n * `\"", "\"` checks preceding whitespace for the specified tokens."], ["\n Five arguments may be optionally provided:\n\n * \\`\"", "\"\\` checks that \\`catch\\` is on the same line as the closing brace for \\`try\\`.\n * \\`\"", "\"\\` checks that \\`finally\\` is on the same line as the closing brace for \\`catch\\`.\n * \\`\"", "\"\\` checks that \\`else\\` is on the same line as the closing brace for \\`if\\`.\n * \\`\"", "\"\\` checks that an open brace falls on the same line as its preceding expression.\n * \\`\"", "\"\\` checks preceding whitespace for the specified tokens."])), OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE, OPTION_BRACE, OPTION_WHITESPACE),
51 enum: [OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE, OPTION_BRACE, OPTION_WHITESPACE],
56 optionExamples: [[true, OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE]],
58 typescriptOnly: false,
61 /* tslint:enable:object-literal-sort-keys */
62 Rule.WHITESPACE_FAILURE_STRING = "missing whitespace";
64 }(Lint.Rules.AbstractRule));
66 var OneLineWalker = /** @class */ (function (_super) {
67 tslib_1.__extends(OneLineWalker, _super);
68 function OneLineWalker() {
69 return _super !== null && _super.apply(this, arguments) || this;
71 OneLineWalker.prototype.walk = function (sourceFile) {
73 var cb = function (node) {
75 case ts.SyntaxKind.Block:
76 if (!tsutils_1.isBlockLike(node.parent) ||
77 (node.parent.kind === ts.SyntaxKind.CaseClause &&
78 node.parent.statements.length === 1)) {
79 _this.check({ pos: node.pos, end: node.statements.pos });
82 case ts.SyntaxKind.CaseBlock:
83 _this.check({ pos: node.pos, end: node.clauses.pos });
85 case ts.SyntaxKind.ModuleBlock:
86 _this.check({ pos: node.pos, end: node.statements.pos });
88 case ts.SyntaxKind.EnumDeclaration:
90 end: node.members.pos,
94 case ts.SyntaxKind.InterfaceDeclaration:
95 case ts.SyntaxKind.ClassDeclaration:
96 case ts.SyntaxKind.ClassExpression: {
97 var openBrace = tsutils_1.getChildOfKind(node, ts.SyntaxKind.OpenBraceToken, sourceFile);
98 if (openBrace !== undefined) {
99 _this.check(openBrace);
103 case ts.SyntaxKind.IfStatement: {
104 var _a = node, thenStatement = _a.thenStatement, elseStatement = _a.elseStatement;
105 if (elseStatement !== undefined && thenStatement.kind === ts.SyntaxKind.Block) {
106 _this.check({ pos: thenStatement.end, end: elseStatement.pos }, "else");
110 case ts.SyntaxKind.TryStatement: {
111 var _b = node, finallyBlock = _b.finallyBlock, catchClause = _b.catchClause, tryBlock = _b.tryBlock;
112 if (catchClause !== undefined) {
113 _this.check(catchClause.getChildAt(0, sourceFile), "catch");
114 if (finallyBlock !== undefined) {
115 _this.check({ pos: catchClause.end, end: finallyBlock.pos }, "finally");
118 else if (finallyBlock !== undefined) {
119 _this.check({ pos: tryBlock.end, end: finallyBlock.pos }, "finally");
123 case ts.SyntaxKind.BinaryExpression: {
124 var _c = node, operatorToken = _c.operatorToken, right = _c.right;
125 if (operatorToken.kind === ts.SyntaxKind.EqualsToken &&
126 tsutils_1.isObjectLiteralExpression(right)) {
127 _this.check({ pos: right.pos, end: right.properties.pos });
131 case ts.SyntaxKind.VariableDeclaration: {
132 var initializer = node.initializer;
133 if (initializer !== undefined && tsutils_1.isObjectLiteralExpression(initializer)) {
134 _this.check({ pos: initializer.pos, end: initializer.properties.pos });
138 case ts.SyntaxKind.TypeAliasDeclaration: {
139 var type = node.type;
140 if (type.kind === ts.SyntaxKind.MappedType ||
141 type.kind === ts.SyntaxKind.TypeLiteral) {
142 _this.check(type.getChildAt(0, sourceFile));
146 return ts.forEachChild(node, cb);
148 return ts.forEachChild(sourceFile, cb);
150 OneLineWalker.prototype.check = function (range, kind) {
151 var tokenStart = range.end - (kind === undefined ? 1 : kind.length);
152 if (this.options[kind === undefined ? "brace" : kind] &&
153 !tsutils_1.isSameLine(this.sourceFile, range.pos, tokenStart)) {
154 this.addFailure(tokenStart, range.end, "misplaced " + (kind === undefined ? "opening brace" : "'" + kind + "'"), Lint.Replacement.replaceFromTo(range.pos, tokenStart, this.options.whitespace ? " " : ""));
156 else if (this.options.whitespace && range.pos === tokenStart) {
157 this.addFailure(tokenStart, range.end, Rule.WHITESPACE_FAILURE_STRING, Lint.Replacement.appendText(range.pos, " "));
160 return OneLineWalker;
161 }(Lint.AbstractWalker));
162 var templateObject_1;