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

Skip to content

Commit d86998b

Browse files
committed
feat: [untested] support type declarations
1 parent 47e0776 commit d86998b

File tree

19 files changed

+368
-251
lines changed

19 files changed

+368
-251
lines changed

packages/scope-manager/src/Variable.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { TSESTree } from '@typescript-eslint/experimental-utils';
2-
import { Definition } from './definition';
3-
import { Reference } from './Reference';
2+
import {
3+
Definition,
4+
TypeDefinitionTypes,
5+
ValueDefinitionTypes,
6+
} from './definition';
7+
import { Reference } from './referencer/Reference';
48
import { Scope } from './scope';
59

610
/**
@@ -43,6 +47,20 @@ class Variable {
4347
this.name = name;
4448
this.scope = scope;
4549
}
50+
51+
/**
52+
* `true` if the variable is valid in a type context, false otherwise
53+
*/
54+
public isTypeVariable(): boolean {
55+
return this.defs.some(def => TypeDefinitionTypes.has(def.type));
56+
}
57+
58+
/**
59+
* `true` if the variable is valid in a value context, false otherwise
60+
*/
61+
public isValueVariable(): boolean {
62+
return this.defs.some(def => ValueDefinitionTypes.has(def.type));
63+
}
4664
}
4765

4866
export { Variable };

packages/scope-manager/src/analyze.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { Referencer, ReferencerOptions } from './referencer';
23
import { ScopeManager } from './ScopeManager';
3-
import { Referencer, ReferencerOptions } from './Referencer';
44

55
interface Options {
66
/**
@@ -59,7 +59,7 @@ function analyze(tree: TSESTree.Node, providedOptions?: Options): ScopeManager {
5959
}
6060

6161
export * from './definition';
62-
export { Reference } from './Reference';
62+
export { Reference } from './referencer/Reference';
6363
export * from './scope';
6464
export { ScopeManager } from './ScopeManager';
6565
export { Variable } from './Variable';

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FunctionNameDefinition } from './FunctionNameDefinition';
44
import { ImplicitGlobalVariableDefinition } from './ImplicitGlobalVariableDefinition';
55
import { ImportBindingDefinition } from './ImportBindingDefinition';
66
import { ParameterDefinition } from './ParameterDefinition';
7+
import { TypeDefinition } from './TypeDefinition';
78
import { VariableDefinition } from './VariableDefinition';
89

910
type Definition =
@@ -13,6 +14,7 @@ type Definition =
1314
| ImplicitGlobalVariableDefinition
1415
| ImportBindingDefinition
1516
| ParameterDefinition
17+
| TypeDefinition
1618
| VariableDefinition;
1719

1820
export { Definition };

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@ enum DefinitionType {
66
ImplicitGlobalVariable = 'ImplicitGlobalVariable',
77
ImportBinding = 'ImportBinding',
88
Parameter = 'Parameter',
9+
Type = 'Type',
910
Variable = 'Variable',
1011
}
1112

12-
export { DefinitionType };
13+
/**
14+
* The DefinitionTypes that are valid in a type context
15+
*/
16+
const TypeDefinitionTypes = new Set([
17+
DefinitionType.ClassName,
18+
DefinitionType.Type,
19+
]);
20+
21+
/**
22+
* The DefinitionTypes that are valid in a value context
23+
*/
24+
const ValueDefinitionTypes = new Set([
25+
DefinitionType.CatchClause,
26+
DefinitionType.ClassName,
27+
DefinitionType.FunctionName,
28+
DefinitionType.ImplicitGlobalVariable,
29+
DefinitionType.ImportBinding,
30+
DefinitionType.Parameter,
31+
DefinitionType.Variable,
32+
]);
33+
34+
export { DefinitionType, TypeDefinitionTypes, ValueDefinitionTypes };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { DefinitionType } from './DefinitionType';
3+
import { DefinitionBase } from './DefinitionBase';
4+
5+
class TypeDefinition extends DefinitionBase<
6+
DefinitionType.Type,
7+
TSESTree.Node, // TODO
8+
null
9+
> {
10+
constructor(name: TSESTree.Identifier, node: TSESTree.Node) {
11+
super(DefinitionType.Type, name, node, null);
12+
}
13+
}
14+
15+
export { TypeDefinition };
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
export { CatchClauseDefinition } from './CatchClauseDefinition';
2-
export { ClassNameDefinition } from './ClassNameDefinition';
3-
export { Definition } from './Definition';
4-
export { DefinitionType } from './DefinitionType';
5-
export { FunctionNameDefinition } from './FunctionNameDefinition';
6-
export { ImplicitGlobalVariableDefinition } from './ImplicitGlobalVariableDefinition';
7-
export { ImportBindingDefinition } from './ImportBindingDefinition';
8-
export { ParameterDefinition } from './ParameterDefinition';
9-
export { VariableDefinition } from './VariableDefinition';
1+
export * from './CatchClauseDefinition';
2+
export * from './ClassNameDefinition';
3+
export * from './Definition';
4+
export * from './DefinitionType';
5+
export * from './FunctionNameDefinition';
6+
export * from './ImplicitGlobalVariableDefinition';
7+
export * from './ImportBindingDefinition';
8+
export * from './ParameterDefinition';
9+
export * from './TypeDefinition';
10+
export * from './VariableDefinition';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { ImportBindingDefinition } from '../definition';
3+
import { Referencer } from './Referencer';
4+
import { Visitor } from './Visitor';
5+
6+
class ImportReferencer extends Visitor {
7+
public readonly declaration: TSESTree.ImportDeclaration;
8+
public readonly referencer: Referencer;
9+
10+
constructor(declaration: TSESTree.ImportDeclaration, referencer: Referencer) {
11+
super(null, referencer.options);
12+
this.declaration = declaration;
13+
this.referencer = referencer;
14+
}
15+
16+
visitImport(
17+
id: TSESTree.Identifier,
18+
specifier:
19+
| TSESTree.ImportDefaultSpecifier
20+
| TSESTree.ImportNamespaceSpecifier
21+
| TSESTree.ImportSpecifier,
22+
): void {
23+
this.referencer.visitPattern(id, pattern => {
24+
this.referencer
25+
.currentScope()
26+
.defineIdentifier(
27+
pattern,
28+
new ImportBindingDefinition(pattern, specifier, this.declaration),
29+
);
30+
});
31+
}
32+
33+
ImportNamespaceSpecifier(node: TSESTree.ImportNamespaceSpecifier): void {
34+
const local = node.local;
35+
this.visitImport(local, node);
36+
}
37+
38+
ImportDefaultSpecifier(node: TSESTree.ImportDefaultSpecifier): void {
39+
const local = node.local;
40+
this.visitImport(local, node);
41+
}
42+
43+
ImportSpecifier(node: TSESTree.ImportSpecifier): void {
44+
const local = node.local;
45+
this.visitImport(local, node);
46+
}
47+
}
48+
49+
export { ImportReferencer };

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ type PatternVisitorCallback = (
1515

1616
type PatternVisitorOptions = VisitorOptions;
1717
class PatternVisitor extends Visitor {
18-
static isPattern(node: TSESTree.Node): boolean {
18+
static isPattern(
19+
node: TSESTree.Node,
20+
): node is
21+
| TSESTree.Identifier
22+
| TSESTree.ObjectPattern
23+
| TSESTree.ArrayPattern
24+
| TSESTree.SpreadElement
25+
| TSESTree.RestElement
26+
| TSESTree.AssignmentPattern {
1927
const nodeType = node.type;
2028

2129
return (

packages/scope-manager/src/Reference.ts renamed to packages/scope-manager/src/referencer/Reference.ts

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TSESTree } from '@typescript-eslint/experimental-utils';
2-
import { Scope } from './scope';
3-
import { Variable } from './Variable';
2+
import { Scope } from '../scope';
3+
import { Variable } from '../Variable';
44

55
enum ReferenceFlag {
66
READ = 0x1,
@@ -50,36 +50,19 @@ class Reference {
5050

5151
public maybeImplicitGlobal?: ReferenceImplicitGlobal | null;
5252

53-
constructor(
54-
identifier: TSESTree.Identifier,
55-
scope: Scope,
56-
flag: ReferenceFlag.READ,
57-
);
58-
constructor(
59-
identifier: TSESTree.Identifier,
60-
scope: Scope,
61-
flag: ReferenceFlag.WRITE | ReferenceFlag.RW,
62-
writeExpr: TSESTree.Node | null,
63-
maybeImplicitGlobal: ReferenceImplicitGlobal | null,
64-
partial: boolean,
65-
init: boolean,
66-
);
67-
constructor(
68-
identifier: TSESTree.Identifier,
69-
scope: Scope,
70-
flag: ReferenceFlag,
71-
writeExpr?: TSESTree.Node | null,
72-
maybeImplicitGlobal?: ReferenceImplicitGlobal | null,
73-
partial?: boolean,
74-
init?: boolean,
75-
);
53+
/**
54+
* True if this reference was created from a type context, false otherwise
55+
*/
56+
public readonly isTypeReference: boolean;
57+
7658
constructor(
7759
identifier: TSESTree.Identifier,
7860
scope: Scope,
7961
flag: ReferenceFlag,
8062
writeExpr?: TSESTree.Node | null,
8163
maybeImplicitGlobal?: ReferenceImplicitGlobal | null,
8264
init?: boolean,
65+
isTypeReference = false,
8366
) {
8467
this.identifier = identifier;
8568
this.from = scope;
@@ -92,6 +75,7 @@ class Reference {
9275
}
9376

9477
this.maybeImplicitGlobal = maybeImplicitGlobal;
78+
this.isTypeReference = isTypeReference;
9579
}
9680

9781
/**

0 commit comments

Comments
 (0)