.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / typedefRule.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 utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var typedef_examples_1 = require("./code-examples/typedef.examples");
24 var OPTION_CALL_SIGNATURE = "call-signature";
25 var OPTION_ARROW_CALL_SIGNATURE = "arrow-call-signature";
26 var OPTION_PARAMETER = "parameter";
27 var OPTION_ARROW_PARAMETER = "arrow-parameter";
28 var OPTION_PROPERTY_DECLARATION = "property-declaration";
29 var OPTION_VARIABLE_DECLARATION = "variable-declaration";
30 var OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION = "variable-declaration-ignore-function";
31 var OPTION_MEMBER_VARIABLE_DECLARATION = "member-variable-declaration";
32 var OPTION_OBJECT_DESTRUCTURING = "object-destructuring";
33 var OPTION_ARRAY_DESTRUCTURING = "array-destructuring";
34 function parseOptions(ruleArguments) {
35     var options = {};
36     for (var _i = 0, ruleArguments_1 = ruleArguments; _i < ruleArguments_1.length; _i++) {
37         var arg = ruleArguments_1[_i];
38         options[arg] = true;
39     }
40     return options;
41 }
42 var Rule = /** @class */ (function (_super) {
43     tslib_1.__extends(Rule, _super);
44     function Rule() {
45         return _super !== null && _super.apply(this, arguments) || this;
46     }
47     /* tslint:enable:object-literal-sort-keys */
48     Rule.prototype.apply = function (sourceFile) {
49         return this.applyWithWalker(new TypedefWalker(sourceFile, this.ruleName, parseOptions(this.ruleArguments)));
50     };
51     /* tslint:disable:object-literal-sort-keys */
52     Rule.metadata = {
53         ruleName: "typedef",
54         description: "Requires type definitions to exist.",
55         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Several arguments may be optionally provided:\n\n            * `\"", "\"` checks return type of functions.\n            * `\"", "\"` checks return type of arrow functions.\n            * `\"", "\"` checks type specifier of function parameters for non-arrow functions.\n            * `\"", "\"` checks type specifier of function parameters for arrow functions.\n            * `\"", "\"` checks return types of interface properties.\n            * `\"", "\"` checks non-binding variable declarations.\n            * `\"", "\"` ignore variable declarations for non-arrow and arrow functions.\n            * `\"", "\"` checks member variable declarations.\n            * `\"", "\"` checks object destructuring declarations.\n            * `\"", "\"` checks array destructuring declarations."], ["\n            Several arguments may be optionally provided:\n\n            * \\`\"", "\"\\` checks return type of functions.\n            * \\`\"", "\"\\` checks return type of arrow functions.\n            * \\`\"", "\"\\` checks type specifier of function parameters for non-arrow functions.\n            * \\`\"", "\"\\` checks type specifier of function parameters for arrow functions.\n            * \\`\"", "\"\\` checks return types of interface properties.\n            * \\`\"", "\"\\` checks non-binding variable declarations.\n            * \\`\"", "\"\\` ignore variable declarations for non-arrow and arrow functions.\n            * \\`\"", "\"\\` checks member variable declarations.\n            * \\`\"", "\"\\` checks object destructuring declarations.\n            * \\`\"", "\"\\` checks array destructuring declarations."])), OPTION_CALL_SIGNATURE, OPTION_ARROW_CALL_SIGNATURE, OPTION_PARAMETER, OPTION_ARROW_PARAMETER, OPTION_PROPERTY_DECLARATION, OPTION_VARIABLE_DECLARATION, OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION, OPTION_MEMBER_VARIABLE_DECLARATION, OPTION_OBJECT_DESTRUCTURING, OPTION_ARRAY_DESTRUCTURING),
56         options: {
57             type: "array",
58             items: {
59                 type: "string",
60                 enum: [
61                     OPTION_CALL_SIGNATURE,
62                     OPTION_ARROW_CALL_SIGNATURE,
63                     OPTION_PARAMETER,
64                     OPTION_ARROW_PARAMETER,
65                     OPTION_PROPERTY_DECLARATION,
66                     OPTION_VARIABLE_DECLARATION,
67                     OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION,
68                     OPTION_MEMBER_VARIABLE_DECLARATION,
69                     OPTION_OBJECT_DESTRUCTURING,
70                     OPTION_ARRAY_DESTRUCTURING,
71                 ],
72             },
73             minLength: 0,
74             maxLength: 10,
75         },
76         optionExamples: [
77             [true, OPTION_CALL_SIGNATURE, OPTION_PARAMETER, OPTION_MEMBER_VARIABLE_DECLARATION],
78         ],
79         type: "typescript",
80         typescriptOnly: true,
81         codeExamples: typedef_examples_1.codeExamples,
82     };
83     return Rule;
84 }(Lint.Rules.AbstractRule));
85 exports.Rule = Rule;
86 var TypedefWalker = /** @class */ (function (_super) {
87     tslib_1.__extends(TypedefWalker, _super);
88     function TypedefWalker() {
89         return _super !== null && _super.apply(this, arguments) || this;
90     }
91     TypedefWalker.prototype.walk = function (sourceFile) {
92         var _this = this;
93         var cb = function (node) {
94             switch (node.kind) {
95                 case ts.SyntaxKind.FunctionDeclaration:
96                 case ts.SyntaxKind.FunctionExpression:
97                 case ts.SyntaxKind.GetAccessor:
98                 case ts.SyntaxKind.MethodDeclaration:
99                 case ts.SyntaxKind.MethodSignature: {
100                     var _a = node, name = _a.name, parameters = _a.parameters, type = _a.type;
101                     _this.checkTypeAnnotation("call-signature", name !== undefined ? name : parameters, type, name);
102                     break;
103                 }
104                 case ts.SyntaxKind.ArrowFunction:
105                     _this.checkArrowFunction(node);
106                     break;
107                 case ts.SyntaxKind.Parameter:
108                     _this.checkParameter(node);
109                     break;
110                 case ts.SyntaxKind.PropertyDeclaration:
111                     _this.checkPropertyDeclaration(node);
112                     break;
113                 case ts.SyntaxKind.PropertySignature: {
114                     var _b = node, name = _b.name, type = _b.type;
115                     _this.checkTypeAnnotation("property-declaration", name, type, name);
116                     break;
117                 }
118                 case ts.SyntaxKind.VariableDeclaration:
119                     _this.checkVariableDeclaration(node);
120             }
121             return ts.forEachChild(node, cb);
122         };
123         return ts.forEachChild(sourceFile, cb);
124     };
125     TypedefWalker.prototype.checkArrowFunction = function (_a) {
126         var parent = _a.parent, parameters = _a.parameters, type = _a.type;
127         if (parent.kind !== ts.SyntaxKind.CallExpression && !isTypedPropertyDeclaration(parent)) {
128             this.checkTypeAnnotation("arrow-call-signature", parameters, type);
129         }
130     };
131     TypedefWalker.prototype.checkParameter = function (node) {
132         var _a = node, parent = _a.parent, name = _a.name, type = _a.type;
133         var isArrowFunction = parent.kind === ts.SyntaxKind.ArrowFunction;
134         var option = (function () {
135             if (!isArrowFunction) {
136                 return "parameter";
137             }
138             else if (isTypedPropertyDeclaration(parent.parent)) {
139                 return undefined;
140             }
141             else if (utils.isPropertyDeclaration(parent.parent)) {
142                 return "member-variable-declaration";
143             }
144             else {
145                 return "arrow-parameter";
146             }
147         })();
148         if (option !== undefined) {
149             this.checkTypeAnnotation(option, name, type, name);
150         }
151     };
152     TypedefWalker.prototype.checkPropertyDeclaration = function (_a) {
153         var initializer = _a.initializer, name = _a.name, type = _a.type;
154         // If this is an arrow function, it doesn't need to have a typedef on the property declaration
155         // as the typedefs can be on the function's parameters instead
156         if (initializer === undefined || initializer.kind !== ts.SyntaxKind.ArrowFunction) {
157             if (this.options[OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION] === true &&
158                 initializer !== undefined &&
159                 initializer.kind === ts.SyntaxKind.FunctionExpression) {
160                 return;
161             }
162             this.checkTypeAnnotation("member-variable-declaration", name, type, name);
163         }
164     };
165     TypedefWalker.prototype.checkVariableDeclaration = function (node) {
166         var _a = node, parent = _a.parent, name = _a.name, type = _a.type;
167         // variable declarations should always have a grandparent, but check that to be on the safe side.
168         // catch statements will be the parent of the variable declaration
169         // for-in/for-of loops will be the gradparent of the variable declaration
170         if (parent.kind === ts.SyntaxKind.CatchClause ||
171             parent.parent.kind === ts.SyntaxKind.ForInStatement ||
172             parent.parent.kind === ts.SyntaxKind.ForOfStatement) {
173             return;
174         }
175         var option;
176         switch (name.kind) {
177             case ts.SyntaxKind.ObjectBindingPattern:
178                 option = OPTION_OBJECT_DESTRUCTURING;
179                 break;
180             case ts.SyntaxKind.ArrayBindingPattern:
181                 option = OPTION_ARRAY_DESTRUCTURING;
182                 break;
183             default:
184                 option = OPTION_VARIABLE_DECLARATION;
185         }
186         if (this.shouldIgnoreVariableDeclaration(node)) {
187             return;
188         }
189         this.checkTypeAnnotation(option, name, type, name);
190     };
191     TypedefWalker.prototype.shouldIgnoreVariableDeclaration = function (node) {
192         var ignoreFunctions = this.options[OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION] === true;
193         return (ignoreFunctions &&
194             (utils.getChildOfKind(node, ts.SyntaxKind.ArrowFunction) !== undefined ||
195                 utils.getChildOfKind(node, ts.SyntaxKind.FunctionExpression) !== undefined));
196     };
197     TypedefWalker.prototype.checkTypeAnnotation = function (option, location, typeAnnotation, name) {
198         if (this.options[option] === true && typeAnnotation === undefined) {
199             var failure = "expected " + option + (name === undefined ? "" : ": '" + name.getText() + "'") + " to have a typedef";
200             if (isNodeArray(location)) {
201                 this.addFailure(location.pos - 1, location.end + 1, failure);
202             }
203             else {
204                 this.addFailureAtNode(location, failure);
205             }
206         }
207     };
208     return TypedefWalker;
209 }(Lint.AbstractWalker));
210 function isTypedPropertyDeclaration(node) {
211     return utils.isPropertyDeclaration(node) && node.type !== undefined;
212 }
213 function isNodeArray(nodeOrArray) {
214     return Array.isArray(nodeOrArray);
215 }
216 exports.isNodeArray = isNodeArray;
217 var templateObject_1;