.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / oneLineRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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_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);
30     function Rule() {
31         return _super !== null && _super.apply(this, arguments) || this;
32     }
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,
40         }));
41     };
42     /* tslint:disable:object-literal-sort-keys */
43     Rule.metadata = {
44         ruleName: "one-line",
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),
47         options: {
48             type: "array",
49             items: {
50                 type: "string",
51                 enum: [OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE, OPTION_BRACE, OPTION_WHITESPACE],
52             },
53             minLength: 0,
54             maxLength: 5,
55         },
56         optionExamples: [[true, OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE]],
57         type: "style",
58         typescriptOnly: false,
59         hasFix: true,
60     };
61     /* tslint:enable:object-literal-sort-keys */
62     Rule.WHITESPACE_FAILURE_STRING = "missing whitespace";
63     return Rule;
64 }(Lint.Rules.AbstractRule));
65 exports.Rule = Rule;
66 var OneLineWalker = /** @class */ (function (_super) {
67     tslib_1.__extends(OneLineWalker, _super);
68     function OneLineWalker() {
69         return _super !== null && _super.apply(this, arguments) || this;
70     }
71     OneLineWalker.prototype.walk = function (sourceFile) {
72         var _this = this;
73         var cb = function (node) {
74             switch (node.kind) {
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 });
80                     }
81                     break;
82                 case ts.SyntaxKind.CaseBlock:
83                     _this.check({ pos: node.pos, end: node.clauses.pos });
84                     break;
85                 case ts.SyntaxKind.ModuleBlock:
86                     _this.check({ pos: node.pos, end: node.statements.pos });
87                     break;
88                 case ts.SyntaxKind.EnumDeclaration:
89                     _this.check({
90                         end: node.members.pos,
91                         pos: node.name.end,
92                     });
93                     break;
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);
100                     }
101                     break;
102                 }
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");
107                     }
108                     break;
109                 }
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");
116                         }
117                     }
118                     else if (finallyBlock !== undefined) {
119                         _this.check({ pos: tryBlock.end, end: finallyBlock.pos }, "finally");
120                     }
121                     break;
122                 }
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 });
128                     }
129                     break;
130                 }
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 });
135                     }
136                     break;
137                 }
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));
143                     }
144                 }
145             }
146             return ts.forEachChild(node, cb);
147         };
148         return ts.forEachChild(sourceFile, cb);
149     };
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 ? " " : ""));
155         }
156         else if (this.options.whitespace && range.pos === tokenStart) {
157             this.addFailure(tokenStart, range.end, Rule.WHITESPACE_FAILURE_STRING, Lint.Replacement.appendText(range.pos, " "));
158         }
159     };
160     return OneLineWalker;
161 }(Lint.AbstractWalker));
162 var templateObject_1;