blob: 7641e7aafcc2692a201d0ad342a6d64a97d3a949 (
plain)
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
|
import { injectable, } from 'inversify';
import * as eslintScope from 'eslint-scope';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { IScopeAnalyzer } from '../../interfaces/analyzers/scope-analyzer/IScopeAnalyzer';
import { ecmaVersion } from '../../constants/EcmaVersion';
import { NodeGuards } from '../../node/NodeGuards';
@injectable()
export class ScopeAnalyzer implements IScopeAnalyzer {
/**
* @type {eslintScope.AnalysisOptions}
*/
private static readonly eslintScopeOptions: eslintScope.AnalysisOptions = {
ecmaVersion,
optimistic: true
};
/**
* @type {acorn.Options['sourceType'][]}
*/
private static readonly sourceTypes: acorn.Options['sourceType'][] = [
'script',
'module'
];
/**
* @type {number}
*/
private static readonly emptyRangeValue: number = 0;
/**
* @type {eslintScope.ScopeManager | null}
*/
private scopeManager: eslintScope.ScopeManager | null = null;
/**
* `eslint-scope` reads `ranges` property of a nodes
* Should attach that property to the some custom nodes
*
* @param {Node} astTree
*/
private static attachMissingRanges (astTree: ESTree.Node): void {
estraverse.replace(astTree, {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node => {
if (!node.range) {
node.range = [
parentNode?.range?.[0] ?? ScopeAnalyzer.emptyRangeValue,
parentNode?.range?.[1] ?? ScopeAnalyzer.emptyRangeValue
];
}
return node;
}
});
}
/**
* @param {Node} node
* @returns {boolean}
*/
private static isRootNode (node: ESTree.Node): boolean {
return NodeGuards.isProgramNode(node) || node.parentNode === node;
}
/**
* @param {Program} astTree
*/
public analyze (astTree: ESTree.Node): void {
const sourceTypeLength: number = ScopeAnalyzer.sourceTypes.length;
ScopeAnalyzer.attachMissingRanges(astTree);
for (let i: number = 0; i < sourceTypeLength; i++) {
try {
this.scopeManager = eslintScope.analyze(astTree, {
...ScopeAnalyzer.eslintScopeOptions,
sourceType: ScopeAnalyzer.sourceTypes[i]
});
return;
} catch (error) {
if (i < sourceTypeLength - 1) {
continue;
}
throw new Error(error);
}
}
throw new Error('Scope analyzing error');
}
/**
* @param {Node} node
* @returns {Scope}
*/
public acquireScope (node: ESTree.Node): eslintScope.Scope {
if (!this.scopeManager) {
throw new Error('Scope manager is not defined');
}
const scope: eslintScope.Scope | null = this.scopeManager.acquire(
node,
ScopeAnalyzer.isRootNode(node)
);
if (!scope) {
throw new Error('Cannot acquire scope for node');
}
this.sanitizeScopes(scope);
return scope;
}
/**
* @param {Scope} scope
*/
private sanitizeScopes (scope: eslintScope.Scope): void {
scope.childScopes.forEach((childScope: eslintScope.Scope) => {
// fix of class scopes
// trying to move class scope references to the parent scope
if (childScope.type === 'class' && childScope.upper) {
if (!childScope.variables.length) {
return;
}
// class name variable is always first
const classNameVariable: eslintScope.Variable = childScope.variables[0];
const upperVariable: eslintScope.Variable | undefined = childScope.upper.variables
.find((variable: eslintScope.Variable) => {
const isValidClassNameVariable: boolean = classNameVariable.defs
.some((definition: eslintScope.Definition) => definition.type === 'ClassName');
return isValidClassNameVariable && variable.name === classNameVariable.name;
});
upperVariable?.references.push(...childScope.variables[0].references);
}
});
for (const childScope of scope.childScopes) {
this.sanitizeScopes(childScope);
}
}
}
|