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

Skip to content

Commit ea9686b

Browse files
committed
feat: create separate definition classes
1 parent 70599e5 commit ea9686b

25 files changed

+319
-177
lines changed

packages/scope-manager/src/Referencer.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import {
33
AST_NODE_TYPES,
44
} from '@typescript-eslint/experimental-utils';
55
import assert from 'assert';
6-
import { Definition, ParameterDefinition } from './definition';
6+
import {
7+
CatchClauseDefinition,
8+
ClassNameDefinition,
9+
FunctionNameDefinition,
10+
ImportBindingDefinition,
11+
ParameterDefinition,
12+
VariableDefinition,
13+
} from './definition';
714
import {
815
PatternVisitor,
916
PatternVisitorCallback,
@@ -12,7 +19,6 @@ import {
1219
import { ReferenceFlag, ReferenceImplicitGlobal } from './Reference';
1320
import { Scope } from './scope';
1421
import { ScopeManager } from './ScopeManager';
15-
import { VariableType } from './VariableType';
1622
import { Visitor, VisitorOptions } from './Visitor';
1723
import { FunctionScope } from './scope/FunctionScope';
1824

@@ -64,14 +70,7 @@ class Importer extends Visitor {
6470
.currentScope(true)
6571
.__define(
6672
pattern,
67-
new Definition(
68-
VariableType.ImportBinding,
69-
pattern,
70-
specifier,
71-
this.declaration,
72-
null,
73-
null,
74-
),
73+
new ImportBindingDefinition(pattern, specifier, this.declaration),
7574
);
7675
});
7776
}
@@ -198,14 +197,7 @@ class Referencer extends Visitor {
198197
// id is defined in upper scope
199198
this.currentScope(true).__define(
200199
node.id,
201-
new Definition(
202-
VariableType.FunctionName,
203-
node.id,
204-
node,
205-
null,
206-
null,
207-
null,
208-
),
200+
new FunctionNameDefinition(node.id, node),
209201
);
210202
}
211203

@@ -254,7 +246,7 @@ class Referencer extends Visitor {
254246
if (node.type === AST_NODE_TYPES.ClassDeclaration && node.id) {
255247
this.currentScope(true).__define(
256248
node.id,
257-
new Definition(VariableType.ClassName, node.id, node, null, null, null),
249+
new ClassNameDefinition(node.id, node),
258250
);
259251
}
260252

@@ -265,7 +257,7 @@ class Referencer extends Visitor {
265257
if (node.id) {
266258
this.currentScope(true).__define(
267259
node.id,
268-
new Definition(VariableType.ClassName, node.id, node),
260+
new ClassNameDefinition(node.id, node),
269261
);
270262
}
271263
this.visit(node.body);
@@ -354,7 +346,6 @@ class Referencer extends Visitor {
354346

355347
visitVariableDeclaration(
356348
variableTargetScope: Scope,
357-
type: VariableType.Variable,
358349
node: TSESTree.VariableDeclaration,
359350
index: number,
360351
): void {
@@ -367,7 +358,7 @@ class Referencer extends Visitor {
367358
(pattern, info) => {
368359
variableTargetScope.__define(
369360
pattern,
370-
new Definition(type, pattern, decl, node, index, node.kind),
361+
new VariableDefinition(pattern, decl, node, index, node.kind),
371362
);
372363

373364
this.referencingDefaultValue(pattern, info.assignments, null, true);
@@ -440,14 +431,7 @@ class Referencer extends Visitor {
440431
(pattern, info) => {
441432
this.currentScope(true).__define(
442433
pattern,
443-
new Definition(
444-
VariableType.CatchClause,
445-
param,
446-
node,
447-
null,
448-
null,
449-
null,
450-
),
434+
new CatchClauseDefinition(param, node),
451435
);
452436
this.referencingDefaultValue(pattern, info.assignments, null, true);
453437
},
@@ -584,12 +568,7 @@ class Referencer extends Visitor {
584568
for (let i = 0, iz = node.declarations.length; i < iz; ++i) {
585569
const decl = node.declarations[i];
586570

587-
this.visitVariableDeclaration(
588-
variableTargetScope,
589-
VariableType.Variable,
590-
node,
591-
i,
592-
);
571+
this.visitVariableDeclaration(variableTargetScope, node, i);
593572
if (decl.init) {
594573
this.visit(decl.init);
595574
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class CatchClauseDefinition extends DefinitionBase {
6+
declare type: DefinitionType.CatchClause;
7+
8+
constructor(name: TSESTree.BindingName, node: TSESTree.Node) {
9+
super(DefinitionType.CatchClause, name, node, null, null, null);
10+
}
11+
}
12+
13+
export { CatchClauseDefinition };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class ClassNameDefinition extends DefinitionBase {
6+
declare type: DefinitionType.ClassName;
7+
8+
constructor(name: TSESTree.Identifier, node: TSESTree.Node) {
9+
super(DefinitionType.ClassName, name, node, null, null, null);
10+
}
11+
}
12+
13+
export { ClassNameDefinition };
Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,18 @@
1-
import { TSESTree } from '@typescript-eslint/experimental-utils';
2-
import { VariableType } from '../VariableType';
3-
4-
class Definition {
5-
/**
6-
* type of the occurrence
7-
*/
8-
public readonly type: VariableType;
9-
10-
/**
11-
* the identifier AST node of the occurrence.
12-
*/
13-
public readonly name: TSESTree.BindingName;
14-
15-
/**
16-
* the enclosing node of the identifier.
17-
*/
18-
public readonly node: TSESTree.Node;
19-
20-
/**
21-
* the enclosing statement node of the identifier.
22-
*/
23-
public readonly parent: TSESTree.Node | null | undefined;
24-
25-
/**
26-
* the index in the declaration statement.
27-
*/
28-
public readonly index: number | null | undefined;
29-
30-
/**
31-
* the kind of the declaration statement.
32-
*/
33-
public readonly kind: string | null | undefined;
34-
35-
constructor(
36-
type: VariableType,
37-
name: TSESTree.BindingName,
38-
node: TSESTree.Node,
39-
parent?: TSESTree.Node | null,
40-
index?: number | null,
41-
kind?: string | null,
42-
) {
43-
this.type = type;
44-
this.name = name;
45-
this.node = node;
46-
this.parent = parent;
47-
this.index = index;
48-
this.kind = kind;
49-
}
50-
}
1+
import { CatchClauseDefinition } from './CatchClauseDefinition';
2+
import { ClassNameDefinition } from './ClassNameDefinition';
3+
import { FunctionNameDefinition } from './FunctionNameDefinition';
4+
import { ImplicitGlobalVariableDefinition } from './ImplicitGlobalVariableDefinition';
5+
import { ImportBindingDefinition } from './ImportBindingDefinition';
6+
import { ParameterDefinition } from './ParameterDefinition';
7+
import { VariableDefinition } from './VariableDefinition';
8+
9+
type Definition =
10+
| CatchClauseDefinition
11+
| ClassNameDefinition
12+
| FunctionNameDefinition
13+
| ImplicitGlobalVariableDefinition
14+
| ImportBindingDefinition
15+
| ParameterDefinition
16+
| VariableDefinition;
5117

5218
export { Definition };
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
4+
abstract class DefinitionBase {
5+
/**
6+
* type of the occurrence
7+
*/
8+
public readonly type: DefinitionType;
9+
10+
/**
11+
* the identifier AST node of the occurrence.
12+
*/
13+
public readonly name: TSESTree.BindingName;
14+
15+
/**
16+
* the enclosing node of the identifier.
17+
*/
18+
public readonly node: TSESTree.Node;
19+
20+
/**
21+
* the enclosing statement node of the identifier.
22+
*/
23+
public readonly parent: TSESTree.Node | null | undefined;
24+
25+
/**
26+
* the index in the declaration statement.
27+
*/
28+
public readonly index: number | null | undefined;
29+
30+
/**
31+
* the kind of the declaration statement.
32+
*/
33+
public readonly kind: string | null | undefined;
34+
35+
constructor(
36+
type: DefinitionType,
37+
name: TSESTree.BindingName,
38+
node: TSESTree.Node,
39+
parent?: TSESTree.Node | null,
40+
index?: number | null,
41+
kind?: string | null,
42+
) {
43+
this.type = type;
44+
this.name = name;
45+
this.node = node;
46+
this.parent = parent;
47+
this.index = index;
48+
this.kind = kind;
49+
}
50+
}
51+
52+
export { DefinitionBase };
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
enum VariableType {
1+
enum DefinitionType {
22
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
33
CatchClause = 'CatchClause',
4-
Parameter = 'Parameter',
5-
FunctionName = 'FunctionName',
64
ClassName = 'ClassName',
7-
Variable = 'Variable',
8-
ImportBinding = 'ImportBinding',
5+
FunctionName = 'FunctionName',
96
ImplicitGlobalVariable = 'ImplicitGlobalVariable',
7+
ImportBinding = 'ImportBinding',
8+
Parameter = 'Parameter',
9+
Variable = 'Variable',
1010
}
1111

12-
export { VariableType };
12+
export { DefinitionType };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class FunctionNameDefinition extends DefinitionBase {
6+
declare type: DefinitionType.FunctionName;
7+
8+
constructor(name: TSESTree.Identifier, node: TSESTree.Node) {
9+
super(DefinitionType.FunctionName, name, node, null, null, null);
10+
}
11+
}
12+
13+
export { FunctionNameDefinition };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class ImplicitGlobalVariableDefinition extends DefinitionBase {
6+
declare type: DefinitionType.ImplicitGlobalVariable;
7+
8+
constructor(name: TSESTree.BindingName, node: TSESTree.Node) {
9+
super(DefinitionType.ImplicitGlobalVariable, name, node, null, null, null);
10+
}
11+
}
12+
13+
export { ImplicitGlobalVariableDefinition };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class ImportBindingDefinition extends DefinitionBase {
6+
declare type: DefinitionType.ImportBinding;
7+
8+
constructor(
9+
name: TSESTree.Identifier,
10+
node: TSESTree.Node,
11+
decl: TSESTree.ImportDeclaration,
12+
) {
13+
super(DefinitionType.ImportBinding, name, node, decl, null, null);
14+
}
15+
}
16+
17+
export { ImportBindingDefinition };

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { TSESTree } from '@typescript-eslint/experimental-utils';
2-
import { VariableType } from '../VariableType';
3-
import { Definition } from './Definition';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class ParameterDefinition extends DefinitionBase {
6+
declare type: DefinitionType.Parameter;
47

5-
class ParameterDefinition extends Definition {
6-
declare type: VariableType.Parameter;
78
/**
89
* Whether the parameter definition is a part of a rest parameter.
9-
* @member {boolean} ParameterDefinition#rest
1010
*/
1111
public readonly rest: boolean;
1212
constructor(
@@ -15,7 +15,7 @@ class ParameterDefinition extends Definition {
1515
index: number,
1616
rest: boolean,
1717
) {
18-
super(VariableType.Parameter, name, node, null, index, null);
18+
super(DefinitionType.Parameter, name, node, null, index, null);
1919
this.rest = rest;
2020
}
2121
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class VariableDefinition extends DefinitionBase {
6+
declare type: DefinitionType.Variable;
7+
8+
constructor(
9+
name: TSESTree.Identifier,
10+
node: TSESTree.Node,
11+
decl: TSESTree.VariableDeclaration,
12+
index: number,
13+
kind: TSESTree.VariableDeclaration['kind'],
14+
) {
15+
super(DefinitionType.Variable, name, node, decl, index, kind);
16+
}
17+
}
18+
19+
export { VariableDefinition };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
export { CatchClauseDefinition } from './CatchClauseDefinition';
2+
export { ClassNameDefinition } from './ClassNameDefinition';
13
export { Definition } from './Definition';
4+
export { DefinitionType } from './DefinitionType';
5+
export { FunctionNameDefinition } from './FunctionNameDefinition';
6+
export { ImplicitGlobalVariableDefinition } from './ImplicitGlobalVariableDefinition';
7+
export { ImportBindingDefinition } from './ImportBindingDefinition';
28
export { ParameterDefinition } from './ParameterDefinition';
9+
export { VariableDefinition } from './VariableDefinition';

0 commit comments

Comments
 (0)