-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseClosed.ts
612 lines (528 loc) · 23.6 KB
/
parseClosed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import { Node, Identifier, LineAndColumnData, SourceLocation, Program, Parameter } from "@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree";
import { parse, AST_NODE_TYPES } from "@typescript-eslint/typescript-estree";
import { EnterExitTraverser } from "./enterExitTraverser";
import { UpdatedAST } from "./extension";
export interface ScopeObj {
posStart: number;
posLineCol: LineAndColumnData;
scopeName: string;
parentFncScope: ScopeObj | null;
parentScope: ScopeObj | undefined;
variables: Map<string, DeclObj>;
type: "function" | "brace";
}
export interface DeclObj {
varName: string;
varPos: number;
identifier: Node;
// Absolute text positions of the uses
uses: Set<number>;
}
export function parseClosed(
code: string,
documentAST: Node,
onDeclare: (obj: DeclObj, scope: ScopeObj) => void,
onAccess: (
declScope: ScopeObj | undefined,
varName: string,
varPos: number,
curScope: ScopeObj,
curFncScope: ScopeObj,
declObj: DeclObj | undefined,
) => void,
): void {
let lines = code.split(/\n/g);
let lineStarts: number[] = [];
{
let pos = 0;
for (let line of lines) {
lineStarts.push(pos);
pos += line.length + 1;
}
}
function getText(range: SourceLocation) {
let { start, end } = range;
let text = "";
for (let line = start.line; line <= end.line; line++) {
let curLineText = lines[line - 1] + "\n";
let colStart = 0;
if (line === start.line) {
colStart = start.column;
}
let colEnd = curLineText.length;
if (line === end.line) {
colEnd = end.column;
}
text += curLineText.slice(colStart, colEnd);
}
return text;
}
function getAbsolutePos(pos: LineAndColumnData) {
return lineStarts[pos.line - 1] + pos.column;
}
function fixIdentifier(identifier: Identifier): { pos: number, idName: string } {
// Try to adjust the range to be just the id, and not the type information. This should work with
// just an indexOf, because identifiers presumably can't be on multiple lines, or have
// stuff in the middle of them (so they should match just by a plain text search).
let idText = getText(identifier.loc);
let idName = identifier.name;
let offset = idText.indexOf(idName);
if (offset < 0) {
throw new Error(`Identifier doesn't contain its name in its text. Should be impossible`);
}
return {
pos: getAbsolutePos(identifier.loc.start) + offset,
idName
};
}
let phase: "populate" | "use" = "populate";
class ScopeHolder {
// (null represents the root scope)
// { [scope]: { [variableName]: variableNode } }
scopeEntries: Map<Node | null, ScopeObj> = new Map();
scopeStack: (Node | null)[] = [null];
constructor(parentFncScope: ScopeObj | null, private type: "function" | "brace") {
this.scopeEntries.set(null, {
posStart: 0,
posLineCol: { line: 1, column: 0 },
scopeName: "module",
parentFncScope,
parentScope: undefined,
variables: new Map(),
type,
});
}
public getVariableScope(variableName: string) {
for (let i = this.scopeStack.length - 1; i >= 0; i--) {
let scope = this.scopeStack[i];
let scopeObj = this.scopeEntries.get(scope);
let varObj = scopeObj?.variables.get(variableName);
if (scopeObj && varObj) {
return { scopeObj, varObj };
}
}
return undefined;
}
public declareIdentifier(identifier: Node, nameOverride?: string) {
let name = nameOverride || "";
let varPos = -1;
if (!name) {
if (identifier.type === AST_NODE_TYPES.Identifier) {
let fixObj = fixIdentifier(identifier);
name = fixObj.idName;
varPos = fixObj.pos;
} else {
debugger;
throw new Error(`Either identifier must be of type Identifier, or nameOverride must be passed.`);
}
}
let scope = this.scopeEntries.get(this.scopeStack[this.scopeStack.length - 1]);
if (!scope) throw new Error(`Internal error, no current scope`);
if (scope.variables.has(name)) return;
scope.variables.set(name, { varName: name, identifier, varPos, uses: new Set() });
}
public getCurrentScope(): ScopeObj {
let scope = this.scopeEntries.get(this.scopeStack[this.scopeStack.length - 1]);
if (!scope) {
throw new Error(`Internal error, no scope. What happened to the root scope?`);
}
return scope;
}
public declareScope(node: Node, parentFncScope: ScopeObj) {
let scope = this.scopeEntries.get(node);
if (phase === "populate") {
if (scope) {
throw new Error(`Internal error, scope declared twice`);
}
let scopeName = "";
if ("id" in node) {
let id = node.id;
if (id && id.type === AST_NODE_TYPES.Identifier) {
scopeName = id.name;
}
}
if (!scopeName) {
scopeName = parentFncScope.scopeName;
}
let pos = getAbsolutePos(node.loc.start);
this.scopeEntries.set(node, {
posStart: pos,
posLineCol: node.loc.start,
scopeName,
parentFncScope,
parentScope: this.getCurrentScope(),
variables: new Map(),
type: this.type,
});
} else {
if (!scope) {
throw new Error(`Internal error, cannot find scope, must have varied during rerun`);
}
}
this.scopeStack.push(node);
}
public onNodeExit(node: Node) {
let curScope = this.scopeStack[this.scopeStack.length - 1];
if (curScope === node) {
this.scopeStack.pop();
}
}
public emitDeclarations() {
for (let scope of this.scopeEntries.values()) {
for (let varObj of scope.variables.values()) {
onDeclare(varObj, scope);
}
}
//let declObj = getDeclaration(identifier, false);
//onDeclare(declObj, identifier);
}
}
let functionScope = new ScopeHolder(null, "function");
let braceScope = new ScopeHolder(functionScope.getCurrentScope(), "brace");
let usedRanges: Set<string> = new Set();
function setScope(scope: Node, type: "function" | "brace") {
if (type === "function") {
functionScope.declareScope(scope, functionScope.getCurrentScope());
} else {
braceScope.declareScope(scope, functionScope.getCurrentScope());
}
}
function getHideHash(node: Node) {
return (
node.loc.start.column + "_" + node.loc.start.line
+ "_" + node.loc.end.column + "_" + node.loc.end.line + "_"
);
}
function getDeclaration(identifier: Identifier, isAccess: boolean): {
scopeObj: ScopeObj;
varObj: DeclObj;
} | undefined {
let idObj = fixIdentifier(identifier);
let fnc = functionScope.getVariableScope(idObj.idName);
let brace = braceScope.getVariableScope(idObj.idName);
let scope;
let type: "function" | "brace";
if (!brace) {
scope = fnc;
type = "function";
} else if (!fnc) {
scope = brace;
type = "brace";
} else {
if (fnc.scopeObj.posStart >= brace.scopeObj.posStart) {
scope = fnc;
type = "function";
} else {
scope = brace;
type = "brace";
}
}
if (!scope) {
return undefined;
}
if (isAccess) {
let variablesObj = scope.scopeObj.variables.get(idObj.idName);
if (!variablesObj) {
throw new Error(`Internal error, variable isn't in scope matched`);
}
variablesObj.uses.add(idObj.pos);
}
return scope;
}
function declareIdentifier(identifier: Node, type: "function" | "brace", nameOverride?: string) {
if (!nameOverride) {
if (usedRanges.has(getHideHash(identifier))) return;
usedRanges.add(getHideHash(identifier));
}
if (phase === "populate") {
if (type === "function") {
functionScope.declareIdentifier(identifier, nameOverride);
} else {
braceScope.declareIdentifier(identifier, nameOverride);
}
} else {
}
}
function accessIdentifier(identifier: Identifier) {
if (usedRanges.has(getHideHash(identifier))) return;
usedRanges.add(getHideHash(identifier));
if (identifier.name === "breakhere") {
debugger;
}
if (phase === "populate") {
} else {
let fixObj = fixIdentifier(identifier);
let declObj = getDeclaration(identifier, true);
let fnc = functionScope.getCurrentScope();
let brace = braceScope.getCurrentScope();
let scope;
if (fnc.posStart >= brace.posStart) {
scope = fnc;
} else {
scope = brace;
}
onAccess(declObj?.scopeObj, fixObj.idName, fixObj.pos, scope, fnc, declObj?.varObj);
}
}
let isInDeclaration = 0;
let isInAssigment = 0;
let curDeclarationType: "function" | "brace" = "function";
runTraverse();
usedRanges = new Set();
phase = "use";
isInDeclaration = 0;
isInAssigment = 0;
runTraverse();
braceScope.emitDeclarations();
functionScope.emitDeclarations();
function runTraverse() {
new EnterExitTraverser({
enter(statement, parent, property) {
if (statement.type === AST_NODE_TYPES.Identifier) {
if (statement.name === "breakhere") {
debugger;
}
}
if (property === "id" || property === "params") {
isInDeclaration++;
if (isInDeclaration > 1) {
//debugger;
// Is this an error?
}
}
if (parent?.type === AST_NODE_TYPES.AssignmentExpression) {
isInAssigment++;
}
if (statement.type === AST_NODE_TYPES.VariableDeclarator) {
let identifierType = (
parent?.type === AST_NODE_TYPES.VariableDeclaration
&& parent.kind === "var"
? "function" as "function"
: "brace" as "brace"
);
if (isInDeclaration === 1) {
curDeclarationType = identifierType;
}
if (parent?.type === AST_NODE_TYPES.VariableDeclaration
&& parent.declare
) {
// Ignore type declarations
return "ignore";
}
if (statement.id.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(statement.id, identifierType);
}
if (statement.id.type === AST_NODE_TYPES.ArrayPattern) {
for (let elem of statement.id.elements) {
if (elem?.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(elem, identifierType);
}
}
}
}
if (statement.type === AST_NODE_TYPES.TSEnumDeclaration) {
// Enums appear to be at the brace level. At the global level they are
// defined with var... but inside nested braces they use let, so... brace looks like the intention.
declareIdentifier(statement.id, "brace");
}
if (statement.type === AST_NODE_TYPES.ExportSpecifier) {
// If we export a name different than the exported variable, ignore that identifer,
// it isn't an access, and doesn't really exist in any scope.
if (statement.local.name !== statement.exported.name) {
usedRanges.add(getHideHash(statement.exported));
}
}
if (statement.type === AST_NODE_TYPES.ClassDeclaration) {
if (statement.id) {
declareIdentifier(statement.id, "function");
}
// Interestingly enough, this is scoped to the class. This means even in static constructors,
// the this from any parent context is eclipsed. Of course... accessing this in static constructors
// will give a typescript error (but not a javascript error), BUT, even with this error, the parent
// this will still be eclipsed. Very interesting.
declareIdentifier(statement, "function", "this");
}
if (
statement.type === AST_NODE_TYPES.ImportSpecifier
|| statement.type === AST_NODE_TYPES.ImportDefaultSpecifier
|| statement.type === AST_NODE_TYPES.ImportNamespaceSpecifier
) {
declareIdentifier(statement.local, "function");
}
if (statement.type === AST_NODE_TYPES.MethodDefinition) {
if (statement.key.type === AST_NODE_TYPES.Identifier) {
usedRanges.add(getHideHash(statement.key));
}
}
if (statement.type === AST_NODE_TYPES.FunctionDeclaration
|| statement.type === AST_NODE_TYPES.FunctionExpression
) {
if ("id" in statement && statement.id) {
declareIdentifier(statement.id, "function");
}
setScope(statement, "function");
// Declare implicit defines after the scope, so they exist within the scope.
// Every function implicitly defines these
declareIdentifier(statement, "function", "this");
declareIdentifier(statement, "function", "arguments");
declareIdentifier(statement, "function", "new.target");
// TODO: Implicit defines for super, in class constructors
}
if (statement.type === AST_NODE_TYPES.ArrowFunctionExpression) {
setScope(statement, "function");
}
if (statement.type === AST_NODE_TYPES.BlockStatement
// Need to include the for statements, as their variable declarations are siblings to their
// block statement, but are inside the for scope.
|| statement.type === AST_NODE_TYPES.ForInStatement
|| statement.type === AST_NODE_TYPES.ForOfStatement
|| statement.type === AST_NODE_TYPES.ForStatement
) {
setScope(statement, "brace");
}
// If it has params, it is probably a function call
if ("params" in statement && statement.type !== AST_NODE_TYPES.TSMethodSignature) {
function parseParam(param: Node) {
if (param.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(param, "function");
} else if (param.type === AST_NODE_TYPES.AssignmentPattern) {
if (param.left.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(param.left, "function");
}
} else if (param.type === AST_NODE_TYPES.RestElement) {
if (param.argument.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(param.argument, "function");
}
}
}
for (let param of statement.params) {
if (param.type === AST_NODE_TYPES.TSParameterProperty) {
parseParam(param.parameter);
} else {
parseParam(param);
}
}
}
if (statement.type === AST_NODE_TYPES.Property) {/// && isInAssigment === 0) {
if (isInDeclaration > 0) {
if (statement.key.type === AST_NODE_TYPES.Identifier) {
if (
statement.value.type === AST_NODE_TYPES.Identifier
&& statement.key.loc.start.line === statement.value.loc.start.line
&& statement.key.loc.start.column === statement.value.loc.start.column
) {
declareIdentifier(statement.value, curDeclarationType);
} else {
if (statement.value.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(statement.value, curDeclarationType);
usedRanges.add(getHideHash(statement.key));
}
}
// It's... an access, but from the object on the other side, uh...
// { x: y } = obj, is the same as y = obj.x,
// and so we shouldn't register an access for x, as it is really an access under an object.
usedRanges.add(getHideHash(statement.key));
}
} else {
if (
statement.key.type === AST_NODE_TYPES.Identifier
&& statement.value.type === AST_NODE_TYPES.Identifier
&& statement.key.loc.start.line === statement.value.loc.start.line
&& statement.key.loc.start.column === statement.value.loc.start.column
) {
accessIdentifier(statement.key);
}
if (statement.key.type === AST_NODE_TYPES.Identifier) {
usedRanges.add(getHideHash(statement.key));
}
}
}
if (isInDeclaration > 0) {
if (statement.type === AST_NODE_TYPES.RestElement) {
if (statement.argument.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(statement.argument, curDeclarationType);
}
}
}
if (statement.type === AST_NODE_TYPES.ArrayPattern) {
if (isInDeclaration > 0) {
for (let elem of statement.elements) {
if (elem && elem.type === AST_NODE_TYPES.Identifier) {
declareIdentifier(elem, "function");
}
}
}
}
if (statement.type === AST_NODE_TYPES.ClassProperty) {
if (statement.key.type === AST_NODE_TYPES.Identifier) {
usedRanges.add(getHideHash(statement.key));
}
}
if (parent?.type === AST_NODE_TYPES.CatchClause && property === "param") {
return "ignore";
}
if (statement.type === AST_NODE_TYPES.TSEnumDeclaration) {
return "ignore";
}
// Ignore all "TS" stuff. It isn't realy, and will go away after compilation.
if (statement.type.startsWith("TS")
// Exception don't ignore TSAsExpression, as it can have real code inside of it
&& statement.type !== "TSAsExpression"
) {
return "ignore";
}
if ((parent?.type === AST_NODE_TYPES.MemberExpression || parent?.type === AST_NODE_TYPES.OptionalMemberExpression)
&& property === "property" && !parent.computed) {
// x.Y, so, ignore Y
return "ignore";
}
/*
let isObjectDestructure = false;
if(
statement.type === AST_NODE_TYPES.Identifier
&& property === "key"
parent
&& "value" in parent && parent.value && typeof parent.value === "object"
&& "type" in parent.value && parent.value.type === AST_NODE_TYPES.Identifier
&& parent.value.name) {
}
//!parent || !("value" in parent) || !parent.value || parent.value.type !== AST_NODE_TYPES.Ide
*/
if (
statement.type === AST_NODE_TYPES.Identifier
&& (isInAssigment === 0 || property !== "key")
) {
accessIdentifier(statement);
}
if (statement.type === AST_NODE_TYPES.ThisExpression) {
// Not great, but we want to both preserve the object (so we can use it in maps),
// and not have to have every place that handles an identifier also have to handle Identifer and ThisExpression
(statement as any).name = "this";
accessIdentifier(statement as any);
}
if (statement.type === AST_NODE_TYPES.MetaProperty) {
if (statement.meta.name === "new" && statement.property.name === "target") {
(statement as any).name = "new.target";
accessIdentifier(statement as any);
return "ignore";
}
}
},
exit(statement, parent, property) {
if (property === "id" || property === "params") {
isInDeclaration--;
}
if (parent?.type === AST_NODE_TYPES.AssignmentExpression) {
isInAssigment--;
}
/*
if(parent?.type === AST_NODE_TYPES.VariableDeclarator && property === "init") {
isInAssigment--;
}
*/
functionScope.onNodeExit(statement);
braceScope.onNodeExit(statement);
}
}).traverse(documentAST);
}
}