1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const utils_1 = require("@typescript-eslint/utils");
|
23 | const scope_manager_1 = require("@typescript-eslint/scope-manager");
|
24 | const util = __importStar(require("../util"));
|
25 | const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/;
|
26 |
|
27 |
|
28 |
|
29 | function parseOptions(options) {
|
30 | let functions = true;
|
31 | let classes = true;
|
32 | let enums = true;
|
33 | let variables = true;
|
34 | let typedefs = true;
|
35 | let ignoreTypeReferences = true;
|
36 | if (typeof options === 'string') {
|
37 | functions = options !== 'nofunc';
|
38 | }
|
39 | else if (typeof options === 'object' && options !== null) {
|
40 | functions = options.functions !== false;
|
41 | classes = options.classes !== false;
|
42 | enums = options.enums !== false;
|
43 | variables = options.variables !== false;
|
44 | typedefs = options.typedefs !== false;
|
45 | ignoreTypeReferences = options.ignoreTypeReferences !== false;
|
46 | }
|
47 | return {
|
48 | functions,
|
49 | classes,
|
50 | enums,
|
51 | variables,
|
52 | typedefs,
|
53 | ignoreTypeReferences,
|
54 | };
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 | function isFunction(variable) {
|
60 | return variable.defs[0].type === scope_manager_1.DefinitionType.FunctionName;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 | function isTypedef(variable) {
|
66 | return variable.defs[0].type === scope_manager_1.DefinitionType.Type;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 | function isOuterEnum(variable, reference) {
|
72 | return (variable.defs[0].type == scope_manager_1.DefinitionType.TSEnumName &&
|
73 | variable.scope.variableScope !== reference.from.variableScope);
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | function isOuterClass(variable, reference) {
|
79 | return (variable.defs[0].type === scope_manager_1.DefinitionType.ClassName &&
|
80 | variable.scope.variableScope !== reference.from.variableScope);
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 | function isOuterVariable(variable, reference) {
|
86 | return (variable.defs[0].type === scope_manager_1.DefinitionType.Variable &&
|
87 | variable.scope.variableScope !== reference.from.variableScope);
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 | function referenceContainsTypeQuery(node) {
|
93 | switch (node.type) {
|
94 | case utils_1.AST_NODE_TYPES.TSTypeQuery:
|
95 | return true;
|
96 | case utils_1.AST_NODE_TYPES.TSQualifiedName:
|
97 | case utils_1.AST_NODE_TYPES.Identifier:
|
98 | if (!node.parent) {
|
99 | return false;
|
100 | }
|
101 | return referenceContainsTypeQuery(node.parent);
|
102 | default:
|
103 |
|
104 | return false;
|
105 | }
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | function isTypeReference(reference) {
|
111 | return (reference.isTypeReference ||
|
112 | referenceContainsTypeQuery(reference.identifier));
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 | function isInRange(node, location) {
|
118 | return !!node && node.range[0] <= location && location <= node.range[1];
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function isClassRefInClassDecorator(variable, reference) {
|
125 | if (variable.defs[0].type !== scope_manager_1.DefinitionType.ClassName) {
|
126 | return false;
|
127 | }
|
128 | if (!variable.defs[0].node.decorators ||
|
129 | variable.defs[0].node.decorators.length === 0) {
|
130 | return false;
|
131 | }
|
132 | for (const deco of variable.defs[0].node.decorators) {
|
133 | if (reference.identifier.range[0] >= deco.range[0] &&
|
134 | reference.identifier.range[1] <= deco.range[1]) {
|
135 | return true;
|
136 | }
|
137 | }
|
138 | return false;
|
139 | }
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function isInInitializer(variable, reference) {
|
151 | var _a;
|
152 | if (variable.scope !== reference.from) {
|
153 | return false;
|
154 | }
|
155 | let node = variable.identifiers[0].parent;
|
156 | const location = reference.identifier.range[1];
|
157 | while (node) {
|
158 | if (node.type === utils_1.AST_NODE_TYPES.VariableDeclarator) {
|
159 | if (isInRange(node.init, location)) {
|
160 | return true;
|
161 | }
|
162 | if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) &&
|
163 | (node.parent.parent.type === utils_1.AST_NODE_TYPES.ForInStatement ||
|
164 | node.parent.parent.type === utils_1.AST_NODE_TYPES.ForOfStatement) &&
|
165 | isInRange(node.parent.parent.right, location)) {
|
166 | return true;
|
167 | }
|
168 | break;
|
169 | }
|
170 | else if (node.type === utils_1.AST_NODE_TYPES.AssignmentPattern) {
|
171 | if (isInRange(node.right, location)) {
|
172 | return true;
|
173 | }
|
174 | }
|
175 | else if (SENTINEL_TYPE.test(node.type)) {
|
176 | break;
|
177 | }
|
178 | node = node.parent;
|
179 | }
|
180 | return false;
|
181 | }
|
182 | exports.default = util.createRule({
|
183 | name: 'no-use-before-define',
|
184 | meta: {
|
185 | type: 'problem',
|
186 | docs: {
|
187 | description: 'Disallow the use of variables before they are defined',
|
188 | recommended: false,
|
189 | extendsBaseRule: true,
|
190 | },
|
191 | messages: {
|
192 | noUseBeforeDefine: "'{{name}}' was used before it was defined.",
|
193 | },
|
194 | schema: [
|
195 | {
|
196 | oneOf: [
|
197 | {
|
198 | enum: ['nofunc'],
|
199 | },
|
200 | {
|
201 | type: 'object',
|
202 | properties: {
|
203 | functions: { type: 'boolean' },
|
204 | classes: { type: 'boolean' },
|
205 | enums: { type: 'boolean' },
|
206 | variables: { type: 'boolean' },
|
207 | typedefs: { type: 'boolean' },
|
208 | ignoreTypeReferences: { type: 'boolean' },
|
209 | },
|
210 | additionalProperties: false,
|
211 | },
|
212 | ],
|
213 | },
|
214 | ],
|
215 | },
|
216 | defaultOptions: [
|
217 | {
|
218 | functions: true,
|
219 | classes: true,
|
220 | enums: true,
|
221 | variables: true,
|
222 | typedefs: true,
|
223 | ignoreTypeReferences: true,
|
224 | },
|
225 | ],
|
226 | create(context, optionsWithDefault) {
|
227 | const options = parseOptions(optionsWithDefault[0]);
|
228 | |
229 |
|
230 |
|
231 |
|
232 |
|
233 | function isForbidden(variable, reference) {
|
234 | if (options.ignoreTypeReferences && isTypeReference(reference)) {
|
235 | return false;
|
236 | }
|
237 | if (isFunction(variable)) {
|
238 | return options.functions;
|
239 | }
|
240 | if (isOuterClass(variable, reference)) {
|
241 | return options.classes;
|
242 | }
|
243 | if (isOuterVariable(variable, reference)) {
|
244 | return options.variables;
|
245 | }
|
246 | if (isOuterEnum(variable, reference)) {
|
247 | return options.enums;
|
248 | }
|
249 | if (isTypedef(variable)) {
|
250 | return options.typedefs;
|
251 | }
|
252 | return true;
|
253 | }
|
254 | |
255 |
|
256 |
|
257 | function findVariablesInScope(scope) {
|
258 | scope.references.forEach(reference => {
|
259 | const variable = reference.resolved;
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | if (reference.init ||
|
267 | !variable ||
|
268 | variable.identifiers.length === 0 ||
|
269 | (variable.identifiers[0].range[1] <= reference.identifier.range[1] &&
|
270 | !isInInitializer(variable, reference)) ||
|
271 | !isForbidden(variable, reference) ||
|
272 | isClassRefInClassDecorator(variable, reference) ||
|
273 | reference.from.type === utils_1.TSESLint.Scope.ScopeType.functionType) {
|
274 | return;
|
275 | }
|
276 |
|
277 | context.report({
|
278 | node: reference.identifier,
|
279 | messageId: 'noUseBeforeDefine',
|
280 | data: {
|
281 | name: reference.identifier.name,
|
282 | },
|
283 | });
|
284 | });
|
285 | scope.childScopes.forEach(findVariablesInScope);
|
286 | }
|
287 | return {
|
288 | Program() {
|
289 | findVariablesInScope(context.getScope());
|
290 | },
|
291 | };
|
292 | },
|
293 | });
|
294 |
|
\ | No newline at end of file |