Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0219f68

Browse files
committed
feat: support some more nodes
1 parent 7d1031e commit 0219f68

40 files changed

+1097
-1202
lines changed

packages/scope-manager/src/ScopeManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { Variable } from './Variable';
1919

2020
interface ScopeManagerOptions {
21-
gloablReturn?: boolean;
21+
globalReturn?: boolean;
2222
sourceType?: 'module' | 'script';
2323
impliedStrict?: boolean;
2424
ecmaVersion?: number;
@@ -50,7 +50,7 @@ class ScopeManager {
5050
}
5151

5252
public isGlobalReturn(): boolean {
53-
return this.options.gloablReturn === true;
53+
return this.options.globalReturn === true;
5454
}
5555

5656
public isModule(): boolean {

packages/scope-manager/src/analyze.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
import { Referencer, ReferencerOptions } from './referencer';
77
import { ScopeManager } from './ScopeManager';
88

9-
interface Options {
9+
interface AnalyzeOptions {
1010
/**
1111
* whether the whole script is executed under node.js environment.
1212
* When enabled, the scope manager adds a function scope immediately following the global scope.
1313
*/
14-
gloablReturn?: boolean;
14+
globalReturn?: boolean;
1515

1616
/**
1717
* implied strict mode (if ecmaVersion >= 5).
@@ -40,8 +40,8 @@ interface Options {
4040
fallback?: ReferencerOptions['fallback'];
4141
}
4242

43-
const DEFAULT_OPTIONS: Options = {
44-
gloablReturn: false,
43+
const DEFAULT_OPTIONS: AnalyzeOptions = {
44+
globalReturn: false,
4545
impliedStrict: false,
4646
sourceType: 'script',
4747
ecmaVersion: 2018,
@@ -52,7 +52,10 @@ const DEFAULT_OPTIONS: Options = {
5252
/**
5353
* Takes an AST and returns the analyzed scopes.
5454
*/
55-
function analyze(tree: TSESTree.Node, providedOptions?: Options): ScopeManager {
55+
function analyze(
56+
tree: TSESTree.Node,
57+
providedOptions?: AnalyzeOptions,
58+
): ScopeManager {
5659
const options = Object.assign({}, DEFAULT_OPTIONS, providedOptions);
5760
const scopeManager = new ScopeManager(options);
5861
const referencer = new Referencer(options, scopeManager);
@@ -67,4 +70,4 @@ export { Reference } from './referencer/Reference';
6770
export * from './scope';
6871
export { ScopeManager } from './ScopeManager';
6972
export { Variable } from './Variable';
70-
export { analyze };
73+
export { analyze, AnalyzeOptions };

packages/scope-manager/src/definition/FunctionNameDefinition.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { DefinitionBase } from './DefinitionBase';
44

55
class FunctionNameDefinition extends DefinitionBase<
66
DefinitionType.FunctionName,
7-
TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
7+
| TSESTree.FunctionDeclaration
8+
| TSESTree.FunctionExpression
9+
| TSESTree.TSDeclareFunction
10+
| TSESTree.TSEmptyBodyFunctionExpression,
811
null
912
> {
1013
constructor(name: TSESTree.Identifier, node: FunctionNameDefinition['node']) {

packages/scope-manager/src/definition/ImportBindingDefinition.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,34 @@ class ImportBindingDefinition extends DefinitionBase<
66
DefinitionType.ImportBinding,
77
| TSESTree.ImportSpecifier
88
| TSESTree.ImportDefaultSpecifier
9-
| TSESTree.ImportNamespaceSpecifier,
10-
TSESTree.ImportDeclaration
9+
| TSESTree.ImportNamespaceSpecifier
10+
| TSESTree.TSImportEqualsDeclaration,
11+
TSESTree.ImportDeclaration | TSESTree.TSImportEqualsDeclaration
1112
> {
1213
constructor(
1314
name: TSESTree.Identifier,
14-
node: ImportBindingDefinition['node'],
15+
node: TSESTree.TSImportEqualsDeclaration,
16+
decl: TSESTree.TSImportEqualsDeclaration,
17+
);
18+
constructor(
19+
name: TSESTree.Identifier,
20+
node: Exclude<
21+
ImportBindingDefinition['node'],
22+
TSESTree.TSImportEqualsDeclaration
23+
>,
1524
decl: TSESTree.ImportDeclaration,
25+
);
26+
constructor(
27+
name: TSESTree.Identifier,
28+
node: ImportBindingDefinition['node'],
29+
decl: TSESTree.ImportDeclaration | TSESTree.TSImportEqualsDeclaration,
1630
) {
1731
super(DefinitionType.ImportBinding, name, node, decl);
18-
this.isVariableDefinition = this.parent.importKind !== 'type';
32+
if ('importKind' in this.parent && this.parent.importKind === 'type') {
33+
this.isVariableDefinition = false;
34+
} else {
35+
this.isVariableDefinition = true;
36+
}
1937
}
2038

2139
public readonly isTypeDefinition = true;

packages/scope-manager/src/definition/ParameterDefinition.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { DefinitionBase } from './DefinitionBase';
44

55
class ParameterDefinition extends DefinitionBase<
66
DefinitionType.Parameter,
7+
| TSESTree.ArrowFunctionExpression
78
| TSESTree.FunctionDeclaration
89
| TSESTree.FunctionExpression
9-
| TSESTree.ArrowFunctionExpression,
10+
| TSESTree.TSDeclareFunction
11+
| TSESTree.TSEmptyBodyFunctionExpression,
1012
null
1113
> {
1214
/**

packages/scope-manager/src/referencer/ImportReferencer.ts renamed to packages/scope-manager/src/referencer/ImportVisitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ImportBindingDefinition } from '../definition';
33
import { Referencer } from './Referencer';
44
import { Visitor } from './Visitor';
55

6-
class ImportReferencer extends Visitor {
6+
class ImportVisitor extends Visitor {
77
public readonly declaration: TSESTree.ImportDeclaration;
88
public readonly referencer: Referencer;
99

@@ -17,7 +17,7 @@ class ImportReferencer extends Visitor {
1717
referencer: Referencer,
1818
declaration: TSESTree.ImportDeclaration,
1919
): void {
20-
const importReferencer = new ImportReferencer(declaration, referencer);
20+
const importReferencer = new ImportVisitor(declaration, referencer);
2121
importReferencer.visit(declaration);
2222
}
2323

@@ -56,4 +56,4 @@ class ImportReferencer extends Visitor {
5656
}
5757
}
5858

59-
export { ImportReferencer };
59+
export { ImportVisitor };

packages/scope-manager/src/referencer/PatternVisitor.ts

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type PatternVisitorCallback = (
1515

1616
type PatternVisitorOptions = VisitorOptions;
1717
class PatternVisitor extends Visitor {
18-
static isPattern(
18+
public static isPattern(
1919
node: TSESTree.Node,
2020
): node is
2121
| TSESTree.Identifier
@@ -55,54 +55,55 @@ class PatternVisitor extends Visitor {
5555
this.callback = callback;
5656
}
5757

58-
Identifier(pattern: TSESTree.Identifier): void {
59-
const lastRestElement =
60-
this.restElements[this.restElements.length - 1] ?? null;
61-
62-
this.callback(pattern, {
63-
topLevel: pattern === this.rootPattern,
64-
rest:
65-
lastRestElement !== null &&
66-
lastRestElement !== undefined &&
67-
lastRestElement.argument === pattern,
68-
assignments: this.assignments,
69-
});
70-
}
71-
72-
Property(property: TSESTree.Property): void {
73-
// Computed property's key is a right hand node.
74-
if (property.computed) {
75-
this.rightHandNodes.push(property.key);
76-
}
77-
78-
// If it's shorthand, its key is same as its value.
79-
// If it's shorthand and has its default value, its key is same as its value.left (the value is AssignmentPattern).
80-
// If it's not shorthand, the name of new variable is its value's.
81-
this.visit(property.value);
58+
protected ArrayExpression(node: TSESTree.ArrayExpression): void {
59+
node.elements.forEach(this.visit, this);
8260
}
8361

84-
ArrayPattern(pattern: TSESTree.ArrayPattern): void {
62+
protected ArrayPattern(pattern: TSESTree.ArrayPattern): void {
8563
for (let i = 0; i < pattern.elements.length; ++i) {
8664
const element = pattern.elements[i];
8765

8866
this.visit(element);
8967
}
9068
}
9169

92-
AssignmentPattern(pattern: TSESTree.AssignmentPattern): void {
70+
protected AssignmentExpression(node: TSESTree.AssignmentExpression): void {
71+
this.assignments.push(node);
72+
this.visit(node.left);
73+
this.rightHandNodes.push(node.right);
74+
this.assignments.pop();
75+
}
76+
77+
protected AssignmentPattern(pattern: TSESTree.AssignmentPattern): void {
9378
this.assignments.push(pattern);
9479
this.visit(pattern.left);
9580
this.rightHandNodes.push(pattern.right);
9681
this.assignments.pop();
9782
}
9883

99-
RestElement(pattern: TSESTree.RestElement): void {
100-
this.restElements.push(pattern);
101-
this.visit(pattern.argument);
102-
this.restElements.pop();
84+
protected CallExpression(node: TSESTree.CallExpression): void {
85+
// arguments are right hand nodes.
86+
node.arguments.forEach(a => {
87+
this.rightHandNodes.push(a);
88+
});
89+
this.visit(node.callee);
10390
}
10491

105-
MemberExpression(node: TSESTree.MemberExpression): void {
92+
protected Identifier(pattern: TSESTree.Identifier): void {
93+
const lastRestElement =
94+
this.restElements[this.restElements.length - 1] ?? null;
95+
96+
this.callback(pattern, {
97+
topLevel: pattern === this.rootPattern,
98+
rest:
99+
lastRestElement !== null &&
100+
lastRestElement !== undefined &&
101+
lastRestElement.argument === pattern,
102+
assignments: this.assignments,
103+
});
104+
}
105+
106+
protected MemberExpression(node: TSESTree.MemberExpression): void {
106107
// Computed property's key is a right hand node.
107108
if (node.computed) {
108109
this.rightHandNodes.push(node.property);
@@ -112,34 +113,26 @@ class PatternVisitor extends Visitor {
112113
this.rightHandNodes.push(node.object);
113114
}
114115

115-
//
116-
// ForInStatement.left and AssignmentExpression.left are LeftHandSideExpression.
117-
// By spec, LeftHandSideExpression is Pattern or MemberExpression.
118-
// (see also: https://github.com/estree/estree/pull/20#issuecomment-74584758)
119-
// But espree 2.0 parses to ArrayExpression, ObjectExpression, etc...
120-
//
121-
122-
SpreadElement(node: TSESTree.SpreadElement): void {
123-
this.visit(node.argument);
124-
}
116+
protected Property(property: TSESTree.Property): void {
117+
// Computed property's key is a right hand node.
118+
if (property.computed) {
119+
this.rightHandNodes.push(property.key);
120+
}
125121

126-
ArrayExpression(node: TSESTree.ArrayExpression): void {
127-
node.elements.forEach(this.visit, this);
122+
// If it's shorthand, its key is same as its value.
123+
// If it's shorthand and has its default value, its key is same as its value.left (the value is AssignmentPattern).
124+
// If it's not shorthand, the name of new variable is its value's.
125+
this.visit(property.value);
128126
}
129127

130-
AssignmentExpression(node: TSESTree.AssignmentExpression): void {
131-
this.assignments.push(node);
132-
this.visit(node.left);
133-
this.rightHandNodes.push(node.right);
134-
this.assignments.pop();
128+
protected RestElement(pattern: TSESTree.RestElement): void {
129+
this.restElements.push(pattern);
130+
this.visit(pattern.argument);
131+
this.restElements.pop();
135132
}
136133

137-
CallExpression(node: TSESTree.CallExpression): void {
138-
// arguments are right hand nodes.
139-
node.arguments.forEach(a => {
140-
this.rightHandNodes.push(a);
141-
});
142-
this.visit(node.callee);
134+
protected SpreadElement(node: TSESTree.SpreadElement): void {
135+
this.visit(node.argument);
143136
}
144137
}
145138

0 commit comments

Comments
 (0)