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 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) {
36 for (var _i = 0, ruleArguments_1 = ruleArguments; _i < ruleArguments_1.length; _i++) {
37 var arg = ruleArguments_1[_i];
42 var Rule = /** @class */ (function (_super) {
43 tslib_1.__extends(Rule, _super);
45 return _super !== null && _super.apply(this, arguments) || this;
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)));
51 /* tslint:disable:object-literal-sort-keys */
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),
61 OPTION_CALL_SIGNATURE,
62 OPTION_ARROW_CALL_SIGNATURE,
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,
77 [true, OPTION_CALL_SIGNATURE, OPTION_PARAMETER, OPTION_MEMBER_VARIABLE_DECLARATION],
81 codeExamples: typedef_examples_1.codeExamples,
84 }(Lint.Rules.AbstractRule));
86 var TypedefWalker = /** @class */ (function (_super) {
87 tslib_1.__extends(TypedefWalker, _super);
88 function TypedefWalker() {
89 return _super !== null && _super.apply(this, arguments) || this;
91 TypedefWalker.prototype.walk = function (sourceFile) {
93 var cb = function (node) {
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);
104 case ts.SyntaxKind.ArrowFunction:
105 _this.checkArrowFunction(node);
107 case ts.SyntaxKind.Parameter:
108 _this.checkParameter(node);
110 case ts.SyntaxKind.PropertyDeclaration:
111 _this.checkPropertyDeclaration(node);
113 case ts.SyntaxKind.PropertySignature: {
114 var _b = node, name = _b.name, type = _b.type;
115 _this.checkTypeAnnotation("property-declaration", name, type, name);
118 case ts.SyntaxKind.VariableDeclaration:
119 _this.checkVariableDeclaration(node);
121 return ts.forEachChild(node, cb);
123 return ts.forEachChild(sourceFile, cb);
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);
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) {
138 else if (isTypedPropertyDeclaration(parent.parent)) {
141 else if (utils.isPropertyDeclaration(parent.parent)) {
142 return "member-variable-declaration";
145 return "arrow-parameter";
148 if (option !== undefined) {
149 this.checkTypeAnnotation(option, name, type, name);
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) {
162 this.checkTypeAnnotation("member-variable-declaration", name, type, name);
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) {
177 case ts.SyntaxKind.ObjectBindingPattern:
178 option = OPTION_OBJECT_DESTRUCTURING;
180 case ts.SyntaxKind.ArrayBindingPattern:
181 option = OPTION_ARRAY_DESTRUCTURING;
184 option = OPTION_VARIABLE_DECLARATION;
186 if (this.shouldIgnoreVariableDeclaration(node)) {
189 this.checkTypeAnnotation(option, name, type, name);
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));
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);
204 this.addFailureAtNode(location, failure);
208 return TypedefWalker;
209 }(Lint.AbstractWalker));
210 function isTypedPropertyDeclaration(node) {
211 return utils.isPropertyDeclaration(node) && node.type !== undefined;
213 function isNodeArray(nodeOrArray) {
214 return Array.isArray(nodeOrArray);
216 exports.isNodeArray = isNodeArray;
217 var templateObject_1;