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 /* tslint:disable:object-literal-sort-keys */
26 enum: ["nospace", "onespace", "space"],
31 "call-signature": SPACE_OPTIONS,
32 "index-signature": SPACE_OPTIONS,
33 parameter: SPACE_OPTIONS,
34 "property-declaration": SPACE_OPTIONS,
35 "variable-declaration": SPACE_OPTIONS,
37 additionalProperties: false,
39 var Rule = /** @class */ (function (_super) {
40 tslib_1.__extends(Rule, _super);
42 return _super !== null && _super.apply(this, arguments) || this;
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING = function (option, location, type) {
46 return "expected " + option + " " + location + " colon in " + type;
48 Rule.prototype.apply = function (sourceFile) {
49 var args = this.ruleArguments;
54 return this.applyWithWalker(new TypedefWhitespaceWalker(sourceFile, this.ruleName, options));
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."]))),
63 items: [SPACE_OBJECT, SPACE_OBJECT],
64 additionalItems: false,
70 "call-signature": "nospace",
71 "index-signature": "nospace",
73 "property-declaration": "nospace",
74 "variable-declaration": "nospace",
77 "call-signature": "onespace",
78 "index-signature": "onespace",
79 parameter: "onespace",
80 "property-declaration": "onespace",
81 "variable-declaration": "onespace",
90 }(Lint.Rules.AbstractRule));
92 var TypedefWhitespaceWalker = /** @class */ (function (_super) {
93 tslib_1.__extends(TypedefWhitespaceWalker, _super);
94 function TypedefWhitespaceWalker() {
95 return _super !== null && _super.apply(this, arguments) || this;
97 TypedefWhitespaceWalker.prototype.walk = function (sourceFile) {
99 var cb = function (node) {
100 var optionType = getOptionType(node);
101 if (optionType !== undefined) {
102 _this.checkSpace(node, optionType);
104 return ts.forEachChild(node, cb);
106 return ts.forEachChild(sourceFile, cb);
108 TypedefWhitespaceWalker.prototype.checkSpace = function (node, key) {
109 if (!("type" in node) || node.type === undefined) {
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);
117 if (left !== undefined && left[key] !== undefined) {
118 this.checkLeft(colon.end - 1, left[key], key);
121 TypedefWhitespaceWalker.prototype.checkRight = function (colonEnd, option, key) {
123 var text = this.sourceFile.text;
124 var current = text.charCodeAt(pos);
125 if (ts.isLineBreak(current)) {
128 while (ts.isWhiteSpaceSingleLine(current)) {
130 current = text.charCodeAt(pos);
132 return this.validateWhitespace(colonEnd, pos, option, "after", key);
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)) {
140 current = text.charCodeAt(pos - 1);
142 if (ts.isLineBreak(current)) {
145 return this.validateWhitespace(pos, colonStart, option, "before", key);
147 TypedefWhitespaceWalker.prototype.validateWhitespace = function (start, end, option, location, key) {
151 this.addFailure(start, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.deleteFromTo(start, end));
156 this.addFailure(end, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.appendText(end, " "));
160 switch (end - start) {
162 this.addFailure(end, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.appendText(end, " "));
167 this.addFailure(start + 1, end, Rule.FAILURE_STRING(option, location, key), Lint.Replacement.deleteFromTo(start + 1, end));
171 return TypedefWhitespaceWalker;
172 }(Lint.AbstractWalker));
173 function getOptionType(node) {
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:
191 case ts.SyntaxKind.PropertySignature:
192 case ts.SyntaxKind.PropertyDeclaration:
193 return "property-declaration";
198 var templateObject_1;