UNPKG

9.49 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const util = __importStar(require("../util"));
24exports.default = util.createRule({
25 name: 'typedef',
26 meta: {
27 docs: {
28 description: 'Requires type annotations to exist',
29 recommended: false,
30 },
31 messages: {
32 expectedTypedef: 'Expected a type annotation.',
33 expectedTypedefNamed: 'Expected {{name}} to have a type annotation.',
34 },
35 schema: [
36 {
37 type: 'object',
38 properties: {
39 ["arrayDestructuring" /* ArrayDestructuring */]: { type: 'boolean' },
40 ["arrowParameter" /* ArrowParameter */]: { type: 'boolean' },
41 ["memberVariableDeclaration" /* MemberVariableDeclaration */]: { type: 'boolean' },
42 ["objectDestructuring" /* ObjectDestructuring */]: { type: 'boolean' },
43 ["parameter" /* Parameter */]: { type: 'boolean' },
44 ["propertyDeclaration" /* PropertyDeclaration */]: { type: 'boolean' },
45 ["variableDeclaration" /* VariableDeclaration */]: { type: 'boolean' },
46 ["variableDeclarationIgnoreFunction" /* VariableDeclarationIgnoreFunction */]: { type: 'boolean' },
47 },
48 },
49 ],
50 type: 'suggestion',
51 },
52 defaultOptions: [
53 {
54 ["arrayDestructuring" /* ArrayDestructuring */]: false,
55 ["arrowParameter" /* ArrowParameter */]: false,
56 ["memberVariableDeclaration" /* MemberVariableDeclaration */]: false,
57 ["objectDestructuring" /* ObjectDestructuring */]: false,
58 ["parameter" /* Parameter */]: false,
59 ["propertyDeclaration" /* PropertyDeclaration */]: false,
60 ["variableDeclaration" /* VariableDeclaration */]: false,
61 ["variableDeclarationIgnoreFunction" /* VariableDeclarationIgnoreFunction */]: false,
62 },
63 ],
64 create(context, [{ arrayDestructuring, arrowParameter, memberVariableDeclaration, objectDestructuring, parameter, propertyDeclaration, variableDeclaration, variableDeclarationIgnoreFunction, },]) {
65 function report(location, name) {
66 context.report({
67 node: location,
68 messageId: name ? 'expectedTypedefNamed' : 'expectedTypedef',
69 data: { name },
70 });
71 }
72 function getNodeName(node) {
73 return node.type === utils_1.AST_NODE_TYPES.Identifier ? node.name : undefined;
74 }
75 function isForOfStatementContext(node) {
76 let current = node.parent;
77 while (current) {
78 switch (current.type) {
79 case utils_1.AST_NODE_TYPES.VariableDeclarator:
80 case utils_1.AST_NODE_TYPES.VariableDeclaration:
81 case utils_1.AST_NODE_TYPES.ObjectPattern:
82 case utils_1.AST_NODE_TYPES.ArrayPattern:
83 case utils_1.AST_NODE_TYPES.Property:
84 current = current.parent;
85 break;
86 case utils_1.AST_NODE_TYPES.ForOfStatement:
87 return true;
88 default:
89 current = undefined;
90 }
91 }
92 return false;
93 }
94 function checkParameters(params) {
95 for (const param of params) {
96 let annotationNode;
97 switch (param.type) {
98 case utils_1.AST_NODE_TYPES.AssignmentPattern:
99 annotationNode = param.left;
100 break;
101 case utils_1.AST_NODE_TYPES.TSParameterProperty:
102 annotationNode = param.parameter;
103 // Check TS parameter property with default value like `constructor(private param: string = 'something') {}`
104 if (annotationNode &&
105 annotationNode.type === utils_1.AST_NODE_TYPES.AssignmentPattern) {
106 annotationNode = annotationNode.left;
107 }
108 break;
109 default:
110 annotationNode = param;
111 break;
112 }
113 if (annotationNode !== undefined && !annotationNode.typeAnnotation) {
114 report(param, getNodeName(param));
115 }
116 }
117 }
118 function isVariableDeclarationIgnoreFunction(node) {
119 return (variableDeclarationIgnoreFunction === true &&
120 (node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
121 node.type === utils_1.AST_NODE_TYPES.FunctionExpression));
122 }
123 function isAncestorHasTypeAnnotation(node) {
124 let ancestor = node.parent;
125 while (ancestor) {
126 if (ancestor.type === utils_1.AST_NODE_TYPES.ObjectPattern &&
127 ancestor.typeAnnotation) {
128 return true;
129 }
130 ancestor = ancestor.parent;
131 }
132 return false;
133 }
134 return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (arrayDestructuring && {
135 ArrayPattern(node) {
136 var _a;
137 if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.RestElement &&
138 node.parent.typeAnnotation) {
139 return;
140 }
141 if (!node.typeAnnotation && !isForOfStatementContext(node)) {
142 report(node);
143 }
144 },
145 })), (arrowParameter && {
146 ArrowFunctionExpression(node) {
147 checkParameters(node.params);
148 },
149 })), (memberVariableDeclaration && {
150 PropertyDefinition(node) {
151 if (!(node.value && isVariableDeclarationIgnoreFunction(node.value)) &&
152 !node.typeAnnotation) {
153 report(node, node.key.type === utils_1.AST_NODE_TYPES.Identifier
154 ? node.key.name
155 : undefined);
156 }
157 },
158 })), (parameter && {
159 'FunctionDeclaration, FunctionExpression'(node) {
160 checkParameters(node.params);
161 },
162 })), (objectDestructuring && {
163 ObjectPattern(node) {
164 if (!node.typeAnnotation &&
165 !isForOfStatementContext(node) &&
166 !isAncestorHasTypeAnnotation(node)) {
167 report(node);
168 }
169 },
170 })), (propertyDeclaration && {
171 'TSIndexSignature, TSPropertySignature'(node) {
172 if (!node.typeAnnotation) {
173 report(node, node.type === utils_1.AST_NODE_TYPES.TSPropertySignature
174 ? getNodeName(node.key)
175 : undefined);
176 }
177 },
178 })), { VariableDeclarator(node) {
179 if (!variableDeclaration ||
180 node.id.typeAnnotation ||
181 (node.id.type === utils_1.AST_NODE_TYPES.ArrayPattern &&
182 !arrayDestructuring) ||
183 (node.id.type === utils_1.AST_NODE_TYPES.ObjectPattern &&
184 !objectDestructuring) ||
185 (node.init && isVariableDeclarationIgnoreFunction(node.init))) {
186 return;
187 }
188 let current = node.parent;
189 while (current) {
190 switch (current.type) {
191 case utils_1.AST_NODE_TYPES.VariableDeclaration:
192 // Keep looking upwards
193 current = current.parent;
194 break;
195 case utils_1.AST_NODE_TYPES.ForOfStatement:
196 case utils_1.AST_NODE_TYPES.ForInStatement:
197 // Stop traversing and don't report an error
198 return;
199 default:
200 // Stop traversing
201 current = undefined;
202 break;
203 }
204 }
205 report(node, getNodeName(node.id));
206 } });
207 },
208});
209//# sourceMappingURL=typedef.js.map
\No newline at end of file