js-code-detector 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,571 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/util/ast_util/AstUtil.ts
20
+ var AstUtil_exports = {};
21
+ __export(AstUtil_exports, {
22
+ default: () => AstUtil
23
+ });
24
+ module.exports = __toCommonJS(AstUtil_exports);
25
+ var import_windowProperties = require("./windowProperties");
26
+ var AstUtil = class {
27
+ static getNodePath(node) {
28
+ return [...node._util.ancestors, node].map((n) => n.type).join(":") + ":" + node.name;
29
+ }
30
+ static deepFirstTravel(node, filePath, mapUuidToNode, mapFileLineToNodeSet) {
31
+ const visitedNodeSet = /* @__PURE__ */ new Set();
32
+ if (!node) {
33
+ return;
34
+ }
35
+ return this._deepFirstTravel(node, visitedNodeSet, { filePath, depth: 0, mapUuidToNode, mapFileLineToNodeSet });
36
+ }
37
+ static _deepFirstTravel(node, visitedNodeSet, extra) {
38
+ visitedNodeSet.add(node);
39
+ const { filePath, depth, mapUuidToNode, mapFileLineToNodeSet } = extra;
40
+ const _util = {
41
+ startLine: NaN,
42
+ endLine: NaN,
43
+ startColumn: NaN,
44
+ endColumn: NaN,
45
+ filePath,
46
+ parent: null,
47
+ parentProperty: "",
48
+ indexOfProperty: null,
49
+ ancestors: [],
50
+ nodeCollection: [],
51
+ children: [],
52
+ uuid: "",
53
+ variableScope: [],
54
+ dependenceIds: /* @__PURE__ */ new Set(),
55
+ holdingIds: /* @__PURE__ */ new Set(),
56
+ holdingIdNameMap: /* @__PURE__ */ new Map(),
57
+ inject: /* @__PURE__ */ new Set(),
58
+ provide: /* @__PURE__ */ new Set(),
59
+ effectIds: /* @__PURE__ */ new Set(),
60
+ crossScope: /* @__PURE__ */ new Set()
61
+ };
62
+ node._util = _util;
63
+ const { nodeCollection, children } = _util;
64
+ Object.keys(node).forEach((nodeKey) => {
65
+ if (this.invalidNodeKey.includes(nodeKey) || nodeKey.startsWith("TS") || nodeKey.endsWith("Annotation")) {
66
+ return;
67
+ }
68
+ const nodeValue = node[nodeKey];
69
+ if (visitedNodeSet.has(nodeValue) || !nodeValue) {
70
+ return;
71
+ }
72
+ if (this.isValidNodeCollect(nodeValue)) {
73
+ const childNode = this._deepFirstTravel(nodeValue, visitedNodeSet, { filePath, depth: depth + 1, mapUuidToNode, mapFileLineToNodeSet });
74
+ nodeCollection.push(childNode, ...childNode._util.nodeCollection);
75
+ children.push(childNode);
76
+ childNode._util.parentProperty = nodeKey;
77
+ } else if (this.isValidArrayNodeCollect(nodeValue)) {
78
+ const validNodeArray = nodeValue.filter((nodeItem) => this.isValidNodeCollect(nodeItem)).map((v) => {
79
+ return this._deepFirstTravel(v, visitedNodeSet, { filePath, depth: depth + 1, mapUuidToNode, mapFileLineToNodeSet });
80
+ });
81
+ nodeCollection.push(...validNodeArray.map((n) => [n, ...n._util.nodeCollection]).flat());
82
+ children.push(...validNodeArray);
83
+ validNodeArray.forEach((v, index) => {
84
+ v._util.parentProperty = nodeKey;
85
+ v._util.indexOfProperty = index;
86
+ });
87
+ }
88
+ });
89
+ children.forEach((child) => child._util.parent = node);
90
+ nodeCollection.forEach((nodeItem) => nodeItem._util.ancestors.unshift(node));
91
+ this.collectHoldingIds(node);
92
+ this.collectInjectProvide(node);
93
+ this.collectEffectId(node);
94
+ this.updateLoc(node, { mapUuidToNode, mapFileLineToNodeSet });
95
+ return node;
96
+ }
97
+ static collectInjectProvide(ast) {
98
+ const { type, _util: { inject, provide, holdingIds } } = ast;
99
+ if (type !== "Program" || !this.isBodyArray(ast)) {
100
+ return;
101
+ }
102
+ ast.body.forEach((node) => {
103
+ node.type === "ImportDeclaration" && this.findIdOfImport(node, (id) => inject.add(id));
104
+ if (node.type === "ExportAllDeclaration") {
105
+ const { exported } = node;
106
+ provide.add(exported);
107
+ return;
108
+ }
109
+ if (node.type === "ExportDefaultDeclaration") {
110
+ const declaration = node.declaration;
111
+ if (declaration.type === "Identifier") {
112
+ const theOne = [...holdingIds].find((e) => e.name === declaration.name);
113
+ theOne && provide.add(theOne);
114
+ } else {
115
+ this.deepFindIdOfExpression(declaration, (id) => provide.add(id));
116
+ }
117
+ }
118
+ if (node.type === "ExportNamedDeclaration") {
119
+ const { specifiers, declaration } = node;
120
+ for (const specifier of specifiers) {
121
+ const { exported } = specifier;
122
+ provide.add(exported);
123
+ }
124
+ if (declaration && Array.isArray(declaration.declarations)) {
125
+ for (const dec of declaration.declarations) {
126
+ provide.add(dec);
127
+ }
128
+ }
129
+ }
130
+ });
131
+ }
132
+ static collectHoldingIds(node) {
133
+ const { holdingIds, holdingIdNameMap } = node._util;
134
+ if (this.isBodyArray(node)) {
135
+ const { body } = node;
136
+ body.forEach((cur) => {
137
+ if (cur.type === "ImportDeclaration") {
138
+ this.findIdOfImport(cur, (id) => holdingIds.add(id));
139
+ } else if (cur.type === "FunctionDeclaration" || cur.type === "ClassDeclaration") {
140
+ const id = cur.id;
141
+ id && holdingIds.add(id);
142
+ } else if (cur.type === "VariableDeclaration") {
143
+ this.findIdOfVariable(cur, (id) => holdingIds.add(id));
144
+ }
145
+ });
146
+ }
147
+ if (node.body && this.isBodyArray(node.body)) {
148
+ const { body } = node.body;
149
+ body.forEach((cur) => {
150
+ this.findIdOfFunctionOrVariableOrClass(cur, (id) => holdingIds.add(id));
151
+ });
152
+ }
153
+ if (["FunctionDeclaration", "ArrowFunctionExpression", "FunctionExpression"].includes(node.type)) {
154
+ node.params.forEach((param) => this._deepFindIdentifier(param, (id) => holdingIds.add(id)));
155
+ }
156
+ holdingIds.forEach((holdingId) => {
157
+ const holdingIdName = holdingId.name;
158
+ const nodeSetOfIdName = holdingIdNameMap.get(holdingIdName) || /* @__PURE__ */ new Set();
159
+ nodeSetOfIdName.add(holdingId);
160
+ holdingIdNameMap.set(holdingIdName, nodeSetOfIdName);
161
+ });
162
+ this.collectDependenceIds(node);
163
+ }
164
+ static isVarInit(node) {
165
+ var _a;
166
+ return node._util.parentProperty === "init" && ((_a = node._util.parent) == null ? void 0 : _a.type) === "VariableDeclarator";
167
+ }
168
+ static isReturnArgument(node) {
169
+ var _a;
170
+ return node._util.parentProperty === "argument" && ((_a = node._util.parent) == null ? void 0 : _a.type) === "ReturnStatement";
171
+ }
172
+ static collectDependenceIds(node) {
173
+ const { nodeCollection, dependenceIds, holdingIdNameMap } = node._util;
174
+ nodeCollection.forEach((e) => {
175
+ this.deepFindIdOfExpression(e, (id) => dependenceIds.add(id));
176
+ this.findExportIdentifiers(e, (id) => dependenceIds.add(id));
177
+ if (e.type === "Identifier" && (this.isVarInit(e) || this.isReturnArgument(e))) {
178
+ dependenceIds.add(e);
179
+ }
180
+ });
181
+ for (const dependenceId of dependenceIds) {
182
+ if (dependenceId._util.variableScope.length === 0) {
183
+ const sameNameIds = [...holdingIdNameMap.get(dependenceId.name) || []];
184
+ dependenceId._util.variableScope.push(...sameNameIds);
185
+ const firstPick = sameNameIds[0];
186
+ if (firstPick) {
187
+ firstPick._util.effectIds.add(dependenceId);
188
+ this.markDependenceIdAsCrossScope(dependenceId, firstPick);
189
+ }
190
+ }
191
+ }
192
+ }
193
+ static markDependenceIdAsCrossScope(node, scopeId) {
194
+ var _a, _b, _c;
195
+ const isParamsElement = scopeId._util.parentProperty === "params";
196
+ const isCalleeId = ((_a = node._util.parent) == null ? void 0 : _a.type) === "CallExpression" && node._util.parentProperty === "callee";
197
+ if (isParamsElement && isCalleeId) {
198
+ const fn = scopeId._util.parent;
199
+ const isFn = fn.type === "FunctionExpression" || fn.type === "ArrowFunctionExpression";
200
+ const isFnInVarDec = fn._util.parentProperty === "init" && ((_b = fn._util.parent) == null ? void 0 : _b.type) === "VariableDeclarator";
201
+ const isFnInAssign = fn._util.parentProperty === "right" && ((_c = fn._util.parent) == null ? void 0 : _c.type) === "AssignmentExpression";
202
+ if (isFn && isFnInVarDec) {
203
+ const idHolderOfFn = fn._util.parent.id;
204
+ node._util.crossScope.add(idHolderOfFn);
205
+ } else if (isFn && isFnInAssign) {
206
+ const idHolderOfFn = fn._util.parent.left;
207
+ node._util.crossScope.add(idHolderOfFn);
208
+ } else if (fn.type === "FunctionDeclaration") {
209
+ node._util.crossScope.add(fn);
210
+ }
211
+ }
212
+ }
213
+ static isUseStateVarDec(node) {
214
+ if (node.type !== "VariableDeclarator")
215
+ return false;
216
+ const { id, init } = node;
217
+ if (init.type !== "CallExpression" || id.type !== "ArrayPattern")
218
+ return false;
219
+ const { callee } = init;
220
+ return callee.type === "Identifier" && callee.name === "useState";
221
+ }
222
+ static isUseMemoVarDec(node) {
223
+ if (node.type !== "VariableDeclarator")
224
+ return false;
225
+ const { id, init } = node;
226
+ if (init.type !== "CallExpression")
227
+ return false;
228
+ const { callee } = init;
229
+ return callee.type === "Identifier" && callee.name === "useMemo";
230
+ }
231
+ static isUseCallbackVarDec(node) {
232
+ if (node.type !== "VariableDeclarator")
233
+ return false;
234
+ const { id, init } = node;
235
+ if (init.type !== "CallExpression")
236
+ return false;
237
+ const { callee } = init;
238
+ return callee.type === "Identifier" && callee.name === "useCallback";
239
+ }
240
+ // todo 不断完善
241
+ static collectEffectId(node) {
242
+ if (node.type === "VariableDeclarator") {
243
+ this.collectEffectIdOfVarDec(node);
244
+ }
245
+ if (node.type === "AssignmentExpression") {
246
+ this.collectEffectIdOfAssign(node);
247
+ return;
248
+ }
249
+ const isDeleteOperator = "UnaryExpression" === node.type && node.operator === "delete";
250
+ if (isDeleteOperator || ["UpdateExpression"].includes(node.type)) {
251
+ this.collectEffectIdOfUnaryUpdate(node);
252
+ }
253
+ }
254
+ static collectEffectIdOfVarDec(node) {
255
+ const { id, init } = node;
256
+ if (!init) {
257
+ return;
258
+ }
259
+ if (this.isUseStateVarDec(node)) {
260
+ this.collectEffectIdOfUseState(node);
261
+ return;
262
+ }
263
+ if (this.isUseMemoVarDec(node)) {
264
+ this.collectEffectIdOfUseMemo(node);
265
+ return;
266
+ }
267
+ if (this.isUseCallbackVarDec(node)) {
268
+ this.collectEffectIdOfUseCallback(node);
269
+ return;
270
+ }
271
+ const idSet = /* @__PURE__ */ new Set();
272
+ this._deepFindIdentifier(id, (ele) => idSet.add(ele));
273
+ const createdExpIdSet = /* @__PURE__ */ new Set();
274
+ ["Identifier", "ArrowFunctionExpression", "FunctionExpression"].includes(init.type) ? createdExpIdSet.add(init) : this.deepFindIdOfExpression(init, (id2) => createdExpIdSet.add(id2));
275
+ for (const createdId of idSet) {
276
+ createdId._util.effectIds = /* @__PURE__ */ new Set([...createdExpIdSet, ...createdId._util.effectIds]);
277
+ }
278
+ }
279
+ static collectEffectIdOfUseState(node) {
280
+ const { id, init } = node;
281
+ const args = init.arguments;
282
+ const argsIdSet = /* @__PURE__ */ new Set();
283
+ this._deepFindIdentifier(args[0], (ele) => argsIdSet.add(ele));
284
+ const stateNode = id.elements[0];
285
+ const setStateNode = id.elements[1];
286
+ if (stateNode) {
287
+ const stateIdSet = /* @__PURE__ */ new Set();
288
+ this._deepFindIdentifier(stateNode, (ele) => stateIdSet.add(ele));
289
+ const setStateIdSet = /* @__PURE__ */ new Set();
290
+ if (setStateNode) {
291
+ this._deepFindIdentifier(setStateNode, (ele) => setStateIdSet.add(ele));
292
+ }
293
+ for (const stateId of stateIdSet) {
294
+ stateId._util.effectIds = /* @__PURE__ */ new Set([...argsIdSet, ...setStateIdSet]);
295
+ }
296
+ }
297
+ }
298
+ static collectEffectIdOfUseMemo(node) {
299
+ const { id, init } = node;
300
+ const args = init.arguments;
301
+ const depsIdSet = /* @__PURE__ */ new Set();
302
+ this._deepFindIdentifier(args[1], (ele) => depsIdSet.add(ele));
303
+ const memoIdSet = /* @__PURE__ */ new Set();
304
+ this._deepFindIdentifier(id, (ele) => memoIdSet.add(ele));
305
+ for (const memoId of memoIdSet) {
306
+ memoId._util.effectIds = /* @__PURE__ */ new Set([...depsIdSet, ...memoId._util.effectIds]);
307
+ }
308
+ }
309
+ static collectEffectIdOfUseCallback(node) {
310
+ const { id, init } = node;
311
+ const args = init.arguments;
312
+ const depsIdSet = /* @__PURE__ */ new Set();
313
+ this._deepFindIdentifier(args[1], (ele) => depsIdSet.add(ele));
314
+ if (id) {
315
+ id._util.effectIds = /* @__PURE__ */ new Set([...depsIdSet, ...id._util.effectIds]);
316
+ }
317
+ }
318
+ static collectEffectIdOfAssign(node) {
319
+ const { left, right } = node;
320
+ const idSetOfRight = /* @__PURE__ */ new Set();
321
+ this.deepFindIdOfExpression(right, (id) => idSetOfRight.add(id));
322
+ if (left.type === "Identifier") {
323
+ const { left: left2 } = node;
324
+ left2._util.effectIds = /* @__PURE__ */ new Set([...idSetOfRight, ...left2._util.effectIds]);
325
+ } else {
326
+ const { left: left2 } = node;
327
+ const idSetOfLeft = /* @__PURE__ */ new Set();
328
+ this.deepFindIdOfExpression(left2, (id) => idSetOfLeft.add(id));
329
+ for (const id of idSetOfLeft) {
330
+ id._util.effectIds = /* @__PURE__ */ new Set([...idSetOfRight, ...id._util.effectIds]);
331
+ }
332
+ }
333
+ ;
334
+ }
335
+ static collectEffectIdOfUnaryUpdate(node) {
336
+ const { argument } = node;
337
+ const idSetOfArgument = /* @__PURE__ */ new Set();
338
+ this.deepFindIdOfExpression(argument, (id) => idSetOfArgument.add(id));
339
+ argument._util.effectIds = /* @__PURE__ */ new Set([...argument._util.effectIds, ...idSetOfArgument]);
340
+ }
341
+ static isBodyArray(node) {
342
+ return ["BlockStatement", "Program"].includes(node.type) && Array.isArray(node.body);
343
+ }
344
+ static findExportIdentifiers(node, callback) {
345
+ var _a;
346
+ if (!node) {
347
+ return;
348
+ }
349
+ if (node.type === "ExportDefaultDeclaration" && ((_a = node.declaration) == null ? void 0 : _a.type) === "Identifier") {
350
+ callback(node.declaration);
351
+ return;
352
+ }
353
+ if (node.type === "ExportNamedDeclaration" && Array.isArray(node.specifiers)) {
354
+ for (const specifier of node.specifiers) {
355
+ if (specifier.type === "ExportSpecifier") {
356
+ const local = specifier.local;
357
+ callback(local);
358
+ }
359
+ }
360
+ }
361
+ }
362
+ static expressionTypeIsIdentifier(exp) {
363
+ return (exp == null ? void 0 : exp.type) === "Identifier";
364
+ }
365
+ static deepFindIdOfExpression(exp, callback) {
366
+ if (!exp || exp.type === "ThisExpression") {
367
+ return;
368
+ }
369
+ this._deepFindIdOfExpression(exp, callback);
370
+ }
371
+ static _deepFindIdOfExpression(exp, callback) {
372
+ if (!exp || exp.type === "ThisExpression") {
373
+ return;
374
+ }
375
+ if (exp.type === "SpreadElement") {
376
+ const { argument } = exp;
377
+ this.expressionTypeIsIdentifier(argument) && callback(argument);
378
+ } else if (exp.type === "JSXSpreadChild") {
379
+ const expression = exp.expression;
380
+ callback(expression);
381
+ } else if (exp.type === "MemberExpression" || exp.type === "JSXMemberExpression") {
382
+ const rootIdentifier = this.getRootIdentifierOfMemberExpression(exp);
383
+ this.expressionTypeIsIdentifier(rootIdentifier) && callback(rootIdentifier);
384
+ } else if (exp.type === "ObjectExpression") {
385
+ const properties = exp.properties;
386
+ for (const property of properties) {
387
+ if (property.type === "SpreadElement") {
388
+ this._deepFindIdOfExpression(property, callback);
389
+ } else {
390
+ const value = property.value;
391
+ this.expressionTypeIsIdentifier(value) ? callback(value) : this._deepFindIdOfExpression(value, callback);
392
+ }
393
+ }
394
+ } else if (exp.type === "ArrayExpression") {
395
+ const elements = exp.elements;
396
+ for (const element of elements) {
397
+ this.expressionTypeIsIdentifier(element) ? callback(element) : this._deepFindIdOfExpression(element, callback);
398
+ }
399
+ } else if (exp.type === "ArrowFunctionExpression") {
400
+ const body = exp.body;
401
+ this.expressionTypeIsIdentifier(body) ? callback(body) : this._deepFindIdOfExpression(body, callback);
402
+ } else if (exp.type === "CallExpression") {
403
+ const callee = exp.callee;
404
+ this.expressionTypeIsIdentifier(callee) ? callback(callee) : this._deepFindIdOfExpression(callee, callback);
405
+ const args = exp.arguments;
406
+ for (const argument of args) {
407
+ this.expressionTypeIsIdentifier(argument) ? callback(argument) : this._deepFindIdOfExpression(argument, callback);
408
+ }
409
+ } else if (exp.type === "AssignmentExpression" || exp.type === "BinaryExpression" || exp.type === "LogicalExpression") {
410
+ const { left, right } = exp;
411
+ [left, right].forEach((e) => this.expressionTypeIsIdentifier(e) ? callback(e) : this._deepFindIdOfExpression(e, callback));
412
+ } else if (exp.type === "UpdateExpression") {
413
+ const argument = exp.argument;
414
+ this.expressionTypeIsIdentifier(argument) ? callback(argument) : this._deepFindIdOfExpression(argument, callback);
415
+ } else if (exp.type === "SequenceExpression") {
416
+ const { expressions } = exp;
417
+ for (const expression of expressions) {
418
+ this.expressionTypeIsIdentifier(expression) ? callback(expression) : this._deepFindIdOfExpression(expression, callback);
419
+ }
420
+ } else if (exp.type === "ConditionalExpression") {
421
+ const { test, consequent, alternate } = exp;
422
+ [test, consequent, alternate].forEach((e) => this._deepFindIdOfExpression(e, callback));
423
+ } else if (exp.type === "JSXExpressionContainer") {
424
+ const { expression } = exp;
425
+ expression.name && callback(expression);
426
+ } else if (exp.type === "JSXIdentifier") {
427
+ callback(exp);
428
+ }
429
+ }
430
+ static getRootIdentifierOfMemberExpression(memExp) {
431
+ if (memExp.type === "MemberExpression" || memExp.type === "JSXMemberExpression") {
432
+ return this.getRootIdentifierOfMemberExpression(memExp.object);
433
+ }
434
+ return memExp;
435
+ }
436
+ static findIdOfImport(node, callback) {
437
+ const specifiers = node.specifiers;
438
+ for (const specifier of specifiers) {
439
+ const local = specifier.local;
440
+ callback(local);
441
+ }
442
+ }
443
+ static findIdOfFunctionOrVariableOrClass(node, callback) {
444
+ if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
445
+ const id = node.id;
446
+ id && callback(id);
447
+ } else if (node.type === "VariableDeclaration") {
448
+ this.findIdOfVariable(node, callback);
449
+ }
450
+ }
451
+ static findIdOfVariable(node, callback) {
452
+ if (node.type !== "VariableDeclaration") {
453
+ return;
454
+ }
455
+ const declarations = node.declarations;
456
+ if (!Array.isArray(declarations)) {
457
+ return;
458
+ }
459
+ for (const declaration of declarations) {
460
+ const id = declaration.id;
461
+ id && this._deepFindIdentifier(id, callback);
462
+ }
463
+ }
464
+ static _deepFindIdentifier(id, callback) {
465
+ if (!id) {
466
+ return;
467
+ }
468
+ if (id.type === "Identifier") {
469
+ callback(id);
470
+ }
471
+ if (id.type === "ObjectPattern") {
472
+ const properties = id.properties;
473
+ for (const property of properties) {
474
+ const value = property.value;
475
+ this._deepFindIdentifier(value, callback);
476
+ }
477
+ }
478
+ if (id.type === "ArrayPattern") {
479
+ const elements = id.elements;
480
+ for (const element of elements) {
481
+ this._deepFindIdentifier(element, callback);
482
+ }
483
+ }
484
+ if (id.type === "RestElement") {
485
+ this._deepFindIdentifier(id.argument, callback);
486
+ }
487
+ }
488
+ static updateLoc(astNode, extra) {
489
+ var _a, _b, _c, _d, _e;
490
+ const { _util, type, name } = astNode;
491
+ const { mapUuidToNode, mapFileLineToNodeSet } = extra;
492
+ const { nodeCollection, filePath } = _util;
493
+ _util.startLine = Math.min(...nodeCollection.map((n) => {
494
+ var _a2, _b2;
495
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.start) == null ? void 0 : _b2.line;
496
+ }), (_a = astNode.loc) == null ? void 0 : _a.start.line);
497
+ _util.endLine = Math.max(...nodeCollection.map((n) => {
498
+ var _a2, _b2;
499
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.end) == null ? void 0 : _b2.line;
500
+ }), (_b = astNode.loc) == null ? void 0 : _b.end.line);
501
+ _util.startColumn = Math.min(...nodeCollection.map((n) => {
502
+ var _a2, _b2;
503
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.start) == null ? void 0 : _b2.column;
504
+ }), (_c = astNode.loc) == null ? void 0 : _c.start.column);
505
+ _util.endColumn = Math.max(...nodeCollection.map((n) => {
506
+ var _a2, _b2;
507
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.end) == null ? void 0 : _b2.column;
508
+ }), (_d = astNode.loc) == null ? void 0 : _d.end.column);
509
+ _util.uuid = `${filePath}:${type}:${name}「${_util.startLine}:${_util.startColumn},${_util.endLine}:${_util.endColumn}」`;
510
+ mapUuidToNode.set(_util.uuid, astNode);
511
+ for (let i = _util.startLine; i <= _util.endLine; i++) {
512
+ mapFileLineToNodeSet.set(i, mapFileLineToNodeSet.get(i) || /* @__PURE__ */ new Set());
513
+ (_e = mapFileLineToNodeSet.get(i)) == null ? void 0 : _e.add(astNode);
514
+ }
515
+ if (astNode.type === "Program") {
516
+ mapUuidToNode.set(astNode.type, astNode);
517
+ }
518
+ }
519
+ static isValidArrayNodeCollect(astNode) {
520
+ return Array.isArray(astNode) && astNode.some((v) => typeof (v == null ? void 0 : v.type) === "string");
521
+ }
522
+ static isValidNodeCollect(astNode) {
523
+ return typeof (astNode == null ? void 0 : astNode.type) === "string";
524
+ }
525
+ static isPropertyOfGlobal(node) {
526
+ return node.type === "Identifier" && !node._util.variableScope.length && this.windowProperties.includes(node.name);
527
+ }
528
+ static getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet, lineNumberStart, lineNumberEnd) {
529
+ const nodeSet = /* @__PURE__ */ new Set();
530
+ for (let i = lineNumberStart; i <= lineNumberEnd; i++) {
531
+ const astNode = mapFileLineToNodeSet.get(i);
532
+ if (!astNode) {
533
+ continue;
534
+ }
535
+ for (const nodeItem of astNode) {
536
+ const { startLine, endLine } = nodeItem._util;
537
+ if (startLine >= lineNumberStart && endLine <= lineNumberEnd) {
538
+ nodeSet.add(nodeItem);
539
+ }
540
+ }
541
+ }
542
+ const collections = [...nodeSet].map((e) => e._util.nodeCollection).flat();
543
+ return [...nodeSet].filter((e) => !collections.includes(e));
544
+ }
545
+ static getTopScopeNodesByLineNumber(mapFileLineToNodeSet, lineNumber) {
546
+ const astNode = mapFileLineToNodeSet.get(lineNumber);
547
+ const lineOfNodes = [...new Set(astNode)].filter((e) => {
548
+ const { startLine, endLine } = e._util;
549
+ return startLine === lineNumber && endLine === lineNumber;
550
+ });
551
+ const collections = [...lineOfNodes].map((e) => e._util.nodeCollection).flat();
552
+ return lineOfNodes.filter((e) => !collections.includes(e));
553
+ }
554
+ static isSubNodeOfVariableDeclarator(node) {
555
+ const { ancestors } = node._util;
556
+ const typeList = ancestors.map((e) => e.type);
557
+ const index = typeList.lastIndexOf("VariableDeclarator");
558
+ if (index === -1) {
559
+ return false;
560
+ }
561
+ return ancestors.slice(index).every((ancestor) => ["Identifier", "ObjectPattern", "ArrayPattern"].includes(ancestor.type));
562
+ }
563
+ static createScopeContent(node) {
564
+ return [node._util.filePath, ":", node._util.startLine, "-", node._util.endLine, ":[", node.name || node.value, "]"].join("");
565
+ }
566
+ };
567
+ AstUtil.invalidNodeKey = [
568
+ "comments",
569
+ "tokens"
570
+ ];
571
+ AstUtil.windowProperties = import_windowProperties.windowProperties;
@@ -0,0 +1,19 @@
1
+ import * as babelParse from "@babel/parser";
2
+ import * as vueParse from "vue-eslint-parser";
3
+ import { AstNode } from "./AstUtil";
4
+ export declare const extensionsOfJs: string[];
5
+ export declare const extensions: string[];
6
+ export type MapFilePathToDetail = Map<string, FileSegment & {
7
+ lines: string[];
8
+ }>;
9
+ export type FileSegment = {
10
+ filePath: string;
11
+ fileContent: string;
12
+ };
13
+ export default class FileUtil {
14
+ static parseVue(filePath: string, fileContent: string): vueParse.AST.ESLintProgram;
15
+ static parseJsxLike(filePath: string, fileContent: string): babelParse.ParseResult<import("@babel/types").File>;
16
+ static createMapFilePathToDetail(list: FileSegment[]): MapFilePathToDetail;
17
+ static parseFile(filePath: string, fileContent: string): [string, Omit<AstNode, '_util'> | null];
18
+ static getASTByFilePath(filePath: string): Omit<AstNode, "_util"> | null;
19
+ }