Skip to content

Commit 56dbdf5

Browse files
committed
fix: improve typing
1 parent bd609e3 commit 56dbdf5

File tree

5 files changed

+137
-129
lines changed

5 files changed

+137
-129
lines changed

src/node-path.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { maybeSetModuleExports } from "./shared";
77
export interface NodePath<N = any, V = any> extends Path<V> {
88
node: N;
99
parent: any;
10-
scope: any;
10+
scope: Scope | null;
1111
replace: Path['replace'];
1212
prune(...args: any[]): any;
1313
_computeNode(): any;
@@ -86,6 +86,7 @@ export default function nodePathPlugin(fork: Fork): NodePathConstructor {
8686
NPp.replace = function () {
8787
delete this.node;
8888
delete this.parent;
89+
// @ts-ignore
8990
delete this.scope;
9091
return Path.prototype.replace.apply(this, arguments);
9192
};

src/scope.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
import { NodePath } from "./node-path";
1+
import type { ExpressionKind } from "./gen/kinds";
2+
import type { namedTypes } from "./main";
3+
import type { NodePath } from "./node-path";
24
import { maybeSetModuleExports } from "./shared";
3-
import typesPlugin, { Fork } from "./types";
5+
import type { Fork } from "./types";
6+
import typesPlugin from "./types";
47

58
var hasOwn = Object.prototype.hasOwnProperty;
69

10+
export type ScopeBinding = Record<string, (NodePath | namedTypes.Identifier)[]>;
11+
712
export interface Scope {
813
path: NodePath;
9-
node: any;
14+
node: NodePath['value'];
1015
isGlobal: boolean;
1116
depth: number;
12-
parent: any;
13-
bindings: any;
17+
parent: Scope | null;
18+
bindings: ScopeBinding;
1419
types: any;
1520
didScan: boolean;
16-
declares(name: any): any
17-
declaresType(name: any): any
18-
declareTemporary(prefix?: any): any;
19-
injectTemporary(identifier: any, init: any): any;
20-
scan(force?: any): any;
21-
getBindings(): any;
21+
declares(name: string): boolean
22+
declaresType(name: string): boolean
23+
declareTemporary(prefix?: string): namedTypes.Identifier;
24+
injectTemporary(identifier?: namedTypes.Identifier, init?: ExpressionKind | null): namedTypes.Identifier;
25+
scan(force?: boolean): void;
26+
getBindings(): ScopeBinding;
2227
getTypes(): any;
23-
lookup(name: any): any;
24-
lookupType(name: any): any;
25-
getGlobalScope(): Scope;
28+
lookup(name: string): Scope;
29+
lookupType(name: string): Scope;
30+
getGlobalScope(): Scope | null;
2631
}
2732

2833
export interface ScopeConstructor {
29-
new(path: NodePath, parentScope: any): Scope;
30-
isEstablishedBy(node: any): any;
34+
new(path: NodePath, parentScope?: Scope | null): Scope;
35+
isEstablishedBy(node: NodePath['node']): boolean;
3136
}
3237

3338
export default function scopePlugin(fork: Fork) {
@@ -39,7 +44,7 @@ export default function scopePlugin(fork: Fork) {
3944
var isArray = types.builtInTypes.array;
4045
var b = types.builders;
4146

42-
const Scope = function Scope(this: Scope, path: NodePath, parentScope: unknown) {
47+
const Scope = function Scope(this: Scope, path: NodePath, parentScope?: Scope | null) {
4348
if (!(this instanceof Scope)) {
4449
throw new Error("Scope constructor cannot be invoked without 'new'");
4550
}
@@ -100,7 +105,7 @@ export default function scopePlugin(fork: Fork) {
100105
namedTypes.TSTypeParameter,
101106
);
102107

103-
Scope.isEstablishedBy = function(node) {
108+
Scope.isEstablishedBy = function(node: NodePath['node']) {
104109
return ScopeType.check(node) || TypeParameterScopeType.check(node);
105110
};
106111

@@ -140,10 +145,12 @@ export default function scopePlugin(fork: Fork) {
140145
}
141146

142147
var name = prefix + index;
143-
return this.bindings[name] = types.builders.identifier(name);
148+
var identifier = types.builders.identifier(name);
149+
this.bindings[name] = [identifier];
150+
return identifier;
144151
};
145152

146-
Sp.injectTemporary = function(identifier, init) {
153+
Sp.injectTemporary = function(identifier?: namedTypes.Identifier, init?: ExpressionKind | null) {
147154
identifier || (identifier = this.declareTemporary());
148155

149156
var bodyPath = this.path.get("body");
@@ -186,7 +193,7 @@ export default function scopePlugin(fork: Fork) {
186193
return this.types;
187194
};
188195

189-
function scanScope(path: NodePath, bindings: any, scopeTypes: any) {
196+
function scanScope(path: NodePath, bindings: ScopeBinding, scopeTypes: any) {
190197
var node = path.value;
191198
if (TypeParameterScopeType.check(node)) {
192199
const params = path.get('typeParameters', 'params');
@@ -208,7 +215,7 @@ export default function scopePlugin(fork: Fork) {
208215
}
209216
}
210217

211-
function recursiveScanScope(path: NodePath, bindings: any, scopeTypes: any) {
218+
function recursiveScanScope(path: NodePath, bindings: ScopeBinding, scopeTypes: any) {
212219
var node = path.value;
213220

214221
if (path.parent &&
@@ -286,7 +293,7 @@ export default function scopePlugin(fork: Fork) {
286293
return false;
287294
}
288295

289-
function recursiveScanChild(path: NodePath, bindings: any, scopeTypes: any) {
296+
function recursiveScanChild(path: NodePath, bindings: ScopeBinding, scopeTypes: any) {
290297
var node = path.value;
291298

292299
if (!node || Expression.check(node)) {
@@ -338,7 +345,7 @@ export default function scopePlugin(fork: Fork) {
338345
}
339346
}
340347

341-
function addPattern(patternPath: NodePath, bindings: any) {
348+
function addPattern(patternPath: NodePath, bindings: ScopeBinding) {
342349
var pattern = patternPath.value;
343350
namedTypes.Pattern.assert(pattern);
344351

@@ -357,7 +364,7 @@ export default function scopePlugin(fork: Fork) {
357364
namedTypes.ObjectPattern &&
358365
namedTypes.ObjectPattern.check(pattern)
359366
) {
360-
patternPath.get('properties').each(function(propertyPath: any) {
367+
patternPath.get('properties').each(function(propertyPath: NodePath) {
361368
var property = propertyPath.value;
362369
if (namedTypes.Pattern.check(property)) {
363370
addPattern(propertyPath, bindings);
@@ -379,7 +386,7 @@ export default function scopePlugin(fork: Fork) {
379386
namedTypes.ArrayPattern &&
380387
namedTypes.ArrayPattern.check(pattern)
381388
) {
382-
patternPath.get('elements').each(function(elementPath: any) {
389+
patternPath.get('elements').each(function(elementPath: NodePath) {
383390
var element = elementPath.value;
384391
if (namedTypes.Pattern.check(element)) {
385392
addPattern(elementPath, bindings);
@@ -434,22 +441,22 @@ export default function scopePlugin(fork: Fork) {
434441
}
435442

436443
Sp.lookup = function(name) {
437-
for (var scope = this; scope; scope = scope.parent)
444+
for (var scope: Scope | null = this; scope; scope = scope.parent)
438445
if (scope.declares(name))
439446
break;
440-
return scope;
447+
return scope!;
441448
};
442449

443450
Sp.lookupType = function(name) {
444-
for (var scope = this; scope; scope = scope.parent)
451+
for (var scope: Scope | null = this; scope; scope = scope.parent)
445452
if (scope.declaresType(name))
446453
break;
447-
return scope;
454+
return scope!;
448455
};
449456

450457
Sp.getGlobalScope = function() {
451-
var scope = this;
452-
while (!scope.isGlobal)
458+
var scope: Scope | null = this;
459+
while (scope && !scope.isGlobal)
453460
scope = scope.parent;
454461
return scope;
455462
};

0 commit comments

Comments
 (0)