.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / typedefWhitespaceRule.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 /* tslint:disable:object-literal-sort-keys */
24 var SPACE_OPTIONS = {
25     type: "string",
26     enum: ["nospace", "onespace", "space"],
27 };
28 var SPACE_OBJECT = {
29     type: "object",
30     properties: {
31         "call-signature": SPACE_OPTIONS,
32         "index-signature": SPACE_OPTIONS,
33         parameter: SPACE_OPTIONS,
34         "property-declaration": SPACE_OPTIONS,
35         "variable-declaration": SPACE_OPTIONS,
36     },
37     additionalProperties: false,
38 };
39 var Rule = /** @class */ (function (_super) {
40     tslib_1.__extends(Rule, _super);
41     function Rule() {
42         return _super !== null && _super.apply(this, arguments) || this;
43     }
44     /* tslint:enable:object-literal-sort-keys */
45     Rule.FAILURE_STRING = function (option, location, type) {
46         return "expected " + option + " " + location + " colon in " + type;
47     };
48     Rule.prototype.apply = function (sourceFile) {
49         var args = this.ruleArguments;
50         var options = {
51             left: args[0],
52             right: args[1],
53         };
54         return this.applyWithWalker(new TypedefWhitespaceWalker(sourceFile, this.ruleName, options));
55     };
56     Rule.metadata = {
57         ruleName: "typedef-whitespace",
58         description: "Requires or disallows whitespace for type definitions.",
59         descriptionDetails: "Determines if a space is required or not before the colon in a type specifier.",
60         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Two arguments which are both objects.\n            The first argument specifies how much space should be to the _left_ of a typedef colon.\n            The second argument specifies how much space should be to the _right_ of a typedef colon.\n            Each key should have a value of `\"onespace\"`, `\"space\"` or `\"nospace\"`.\n            Possible keys are:\n\n            * `\"call-signature\"` checks return type of functions.\n            * `\"index-signature\"` checks index type specifier of indexers.\n            * `\"parameter\"` checks function parameters.\n            * `\"property-declaration\"` checks object property declarations.\n            * `\"variable-declaration\"` checks variable declaration."], ["\n            Two arguments which are both objects.\n            The first argument specifies how much space should be to the _left_ of a typedef colon.\n            The second argument specifies how much space should be to the _right_ of a typedef colon.\n            Each key should have a value of \\`\"onespace\"\\`, \\`\"space\"\\` or \\`\"nospace\"\\`.\n            Possible keys are:\n\n            * \\`\"call-signature\"\\` checks return type of functions.\n            * \\`\"index-signature\"\\` checks index type specifier of indexers.\n            * \\`\"parameter\"\\` checks function parameters.\n            * \\`\"property-declaration\"\\` checks object property declarations.\n            * \\`\"variable-declaration\"\\` checks variable declaration."]))),
61         options: {
62             type: "array",
63             items: [SPACE_OBJECT, SPACE_OBJECT],
64             additionalItems: false,
65         },
66         optionExamples: [
67             [
68                 true,
69                 {
70                     "call-signature": "nospace",
71                     "index-signature": "nospace",
72                     parameter: "nospace",
73                     "property-declaration": "nospace",
74                     "variable-declaration": "nospace",
75                 },
76                 {
77                     "call-signature": "onespace",
78                     "index-signature": "onespace",
79                     parameter: "onespace",
80                     "property-declaration": "onespace",
81                     "variable-declaration": "onespace",
82                 },
83             ],
84         ],
85         type: "formatting",
86         typescriptOnly: true,
87         hasFix: true,
88     };
89     return Rule;
90 }(Lint.Rules.AbstractRule));
91 exports.Rule = Rule;
92 var TypedefWhitespaceWalker = /** @class */ (function (_super) {
93     tslib_1.__extends(TypedefWhitespaceWalker, _super);
94     function TypedefWhitespaceWalker() {
95         return _super !== null && _super.apply(this, arguments) || this;
96     }
97     TypedefWhitespaceWalker.prototype.walk = function (sourceFile) {
98         var _this = this;
99         var cb = function (node) {
100             var optionType = getOptionType(node);
101             if (optionType !== undefined) {
102                 _this.checkSpace(node, optionType);
103             }
104             return ts.forEachChild(node, cb);
105         };
106         return ts.forEachChild(sourceFile, cb);
107     };
108     TypedefWhitespaceWalker.prototype.checkSpace = function (node, key) {
109         if (!("type" in node) || node.type === undefined) {
110             return;
111         }
112         var _a = this.options, left = _a.left, right = _a.right;
113         var colon = tsutils_1.getChildOfKind(node, ts.SyntaxKind.ColonToken, this.sourceFile);
114         if (right !== undefined && right[key] !== undefined) {
115             this.checkRight(colon.end, right[key], key);
116         }
117         if (left !== undefined && left[key] !== undefined) {
118             this.checkLeft(colon.end - 1, left[key], key);
119         }
120     };
121     TypedefWhitespaceWalker.prototype.checkRight = function (colonEnd, option, key) {
122         var pos = colonEnd;
123         var text = this.sourceFile.text;
124         var current = text.charCodeAt(pos);
125         if (ts.isLineBreak(current)) {
126             return;
127         }
128         while (ts.isWhiteSpaceSingleLine(current)) {
129             ++pos;
130             current = text.charCodeAt(pos);
131         }
132         return this.validateWhitespace(colonEnd, pos, option, "after", key);
133     };
134     TypedefWhitespaceWalker.prototype.checkLeft = function (colonStart, option, key) {
135         var pos = colonStart;
136         var text = this.sourceFile.text;
137         var current = text.charCodeAt(pos - 1);
138         while (ts.isWhiteSpaceSingleLine(current)) {
139             --pos;
140             current = text.charCodeAt(pos - 1);
141         }
142         if (ts.isLineBreak(current)) {
143             return;
144         }
145         return this.validateWhitespace(pos, colonStart, option, "before", key);
146     };
147     TypedefWhitespaceWalker.prototype.validateWhitespace = function (start, end, option, location, key) {
148         switch (option) {
149             case "nospace":
150                 if (start !== end) {
151                     this.addFailure(start, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.deleteFromTo(start, end));
152                 }
153                 break;
154             case "space":
155                 if (start === end) {
156                     this.addFailure(end, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.appendText(end, " "));
157                 }
158                 break;
159             case "onespace":
160                 switch (end - start) {
161                     case 0:
162                         this.addFailure(end, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.appendText(end, " "));
163                         break;
164                     case 1:
165                         break;
166                     default:
167                         this.addFailure(start + 1, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.deleteFromTo(start + 1, end));
168                 }
169         }
170     };
171     return TypedefWhitespaceWalker;
172 }(Lint.AbstractWalker));
173 function getOptionType(node) {
174     switch (node.kind) {
175         case ts.SyntaxKind.FunctionDeclaration:
176         case ts.SyntaxKind.FunctionExpression:
177         case ts.SyntaxKind.MethodDeclaration:
178         case ts.SyntaxKind.ArrowFunction:
179         case ts.SyntaxKind.GetAccessor:
180         case ts.SyntaxKind.SetAccessor:
181         case ts.SyntaxKind.MethodSignature:
182         case ts.SyntaxKind.ConstructSignature:
183         case ts.SyntaxKind.CallSignature:
184             return "call-signature";
185         case ts.SyntaxKind.IndexSignature:
186             return "index-signature";
187         case ts.SyntaxKind.VariableDeclaration:
188             return "variable-declaration";
189         case ts.SyntaxKind.Parameter:
190             return "parameter";
191         case ts.SyntaxKind.PropertySignature:
192         case ts.SyntaxKind.PropertyDeclaration:
193             return "property-declaration";
194         default:
195             return undefined;
196     }
197 }
198 var templateObject_1;