Codebase list node-ast-types / debian/latest lib / scope.ts
debian/latest

Tree @debian/latest (Download .tar.gz)

scope.ts @debian/latestraw · history · blame

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { Fork } from "../types";
import { NodePath } from "./node-path";
import typesPlugin from "./types";

var hasOwn = Object.prototype.hasOwnProperty;

export interface Scope {
  path: NodePath;
  node: any;
  isGlobal: boolean;
  depth: number;
  parent: any;
  bindings: any;
  types: any;
  didScan: boolean;
  declares(name: any): any
  declaresType(name: any): any
  declareTemporary(prefix?: any): any;
  injectTemporary(identifier: any, init: any): any;
  scan(force?: any): any;
  getBindings(): any;
  getTypes(): any;
  lookup(name: any): any;
  lookupType(name: any): any;
  getGlobalScope(): Scope;
}

export interface ScopeConstructor {
  new(path: NodePath, parentScope: any): Scope;
  isEstablishedBy(node: any): any;
}

export default function scopePlugin(fork: Fork) {
  var types = fork.use(typesPlugin);
  var Type = types.Type;
  var namedTypes = types.namedTypes;
  var Node = namedTypes.Node;
  var Expression = namedTypes.Expression;
  var isArray = types.builtInTypes.array;
  var b = types.builders;

  const Scope = function Scope(this: Scope, path: NodePath, parentScope: unknown) {
    if (!(this instanceof Scope)) {
      throw new Error("Scope constructor cannot be invoked without 'new'");
    }
    if (!TypeParameterScopeType.check(path.value)) {
      ScopeType.assert(path.value);
    }

    var depth: number;

    if (parentScope) {
      if (!(parentScope instanceof Scope)) {
        throw new Error("");
      }
      depth = (parentScope as Scope).depth + 1;
    } else {
      parentScope = null;
      depth = 0;
    }

    Object.defineProperties(this, {
      path: { value: path },
      node: { value: path.value },
      isGlobal: { value: !parentScope, enumerable: true },
      depth: { value: depth },
      parent: { value: parentScope },
      bindings: { value: {} },
      types: { value: {} },
    });
  } as any as ScopeConstructor;

  var ScopeType = Type.or(
    // Program nodes introduce global scopes.
    namedTypes.Program,

    // Function is the supertype of FunctionExpression,
    // FunctionDeclaration, ArrowExpression, etc.
    namedTypes.Function,

    // In case you didn't know, the caught parameter shadows any variable
    // of the same name in an outer scope.
    namedTypes.CatchClause
  );

  // These types introduce scopes that are restricted to type parameters in
  // Flow (this doesn't apply to ECMAScript).
  var TypeParameterScopeType = Type.or(
    namedTypes.Function,
    namedTypes.ClassDeclaration,
    namedTypes.ClassExpression,
    namedTypes.InterfaceDeclaration,
    namedTypes.TSInterfaceDeclaration,
    namedTypes.TypeAlias,
    namedTypes.TSTypeAliasDeclaration,
  );

  var FlowOrTSTypeParameterType = Type.or(
    namedTypes.TypeParameter,
    namedTypes.TSTypeParameter,
  );

  Scope.isEstablishedBy = function(node) {
    return ScopeType.check(node) || TypeParameterScopeType.check(node);
  };

  var Sp: Scope = Scope.prototype;

// Will be overridden after an instance lazily calls scanScope.
  Sp.didScan = false;

  Sp.declares = function(name) {
    this.scan();
    return hasOwn.call(this.bindings, name);
  };

  Sp.declaresType = function(name) {
    this.scan();
    return hasOwn.call(this.types, name);
  };

  Sp.declareTemporary = function(prefix) {
    if (prefix) {
      if (!/^[a-z$_]/i.test(prefix)) {
        throw new Error("");
      }
    } else {
      prefix = "t$";
    }

    // Include this.depth in the name to make sure the name does not
    // collide with any variables in nested/enclosing scopes.
    prefix += this.depth.toString(36) + "$";

    this.scan();

    var index = 0;
    while (this.declares(prefix + index)) {
      ++index;
    }

    var name = prefix + index;
    return this.bindings[name] = types.builders.identifier(name);
  };

  Sp.injectTemporary = function(identifier, init) {
    identifier || (identifier = this.declareTemporary());

    var bodyPath = this.path.get("body");
    if (namedTypes.BlockStatement.check(bodyPath.value)) {
      bodyPath = bodyPath.get("body");
    }

    bodyPath.unshift(
      b.variableDeclaration(
      "var",
      [b.variableDeclarator(identifier, init || null)]
      )
    );

    return identifier;
  };

  Sp.scan = function(force) {
    if (force || !this.didScan) {
      for (var name in this.bindings) {
        // Empty out this.bindings, just in cases.
        delete this.bindings[name];
      }
      for (var name in this.types) {
        // Empty out this.types, just in cases.
        delete this.types[name];
      }
      scanScope(this.path, this.bindings, this.types);
      this.didScan = true;
    }
  };

  Sp.getBindings = function () {
    this.scan();
    return this.bindings;
  };

  Sp.getTypes = function () {
    this.scan();
    return this.types;
  };

  function scanScope(path: NodePath, bindings: any, scopeTypes: any) {
    var node = path.value;
    if (TypeParameterScopeType.check(node)) {
      const params = path.get('typeParameters', 'params');
      if (isArray.check(params.value)) {
        params.each((childPath: NodePath) => {
          addTypeParameter(childPath, scopeTypes);
        });
      }
    }
    if (ScopeType.check(node)) {
      if (namedTypes.CatchClause.check(node)) {
        // A catch clause establishes a new scope but the only variable
        // bound in that scope is the catch parameter. Any other
        // declarations create bindings in the outer scope.
        addPattern(path.get("param"), bindings);
      } else {
        recursiveScanScope(path, bindings, scopeTypes);
      }
    }
  }

  function recursiveScanScope(path: NodePath, bindings: any, scopeTypes: any) {
    var node = path.value;

    if (path.parent &&
      namedTypes.FunctionExpression.check(path.parent.node) &&
      path.parent.node.id) {
      addPattern(path.parent.get("id"), bindings);
    }

    if (!node) {
      // None of the remaining cases matter if node is falsy.

    } else if (isArray.check(node)) {
      path.each((childPath: NodePath) => {
        recursiveScanChild(childPath, bindings, scopeTypes);
      });

    } else if (namedTypes.Function.check(node)) {
      path.get("params").each((paramPath: NodePath) => {
        addPattern(paramPath, bindings);
      });

      recursiveScanChild(path.get("body"), bindings, scopeTypes);
      recursiveScanScope(path.get("typeParameters"), bindings, scopeTypes);

    } else if (
      (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) ||
      (namedTypes.InterfaceDeclaration && namedTypes.InterfaceDeclaration.check(node)) ||
      (namedTypes.TSTypeAliasDeclaration && namedTypes.TSTypeAliasDeclaration.check(node)) ||
      (namedTypes.TSInterfaceDeclaration && namedTypes.TSInterfaceDeclaration.check(node))
    ) {
      addTypePattern(path.get("id"), scopeTypes);

    } else if (namedTypes.VariableDeclarator.check(node)) {
      addPattern(path.get("id"), bindings);
      recursiveScanChild(path.get("init"), bindings, scopeTypes);

    } else if (node.type === "ImportSpecifier" ||
      node.type === "ImportNamespaceSpecifier" ||
      node.type === "ImportDefaultSpecifier") {
      addPattern(
        // Esprima used to use the .name field to refer to the local
        // binding identifier for ImportSpecifier nodes, but .id for
        // ImportNamespaceSpecifier and ImportDefaultSpecifier nodes.
        // ESTree/Acorn/ESpree use .local for all three node types.
        path.get(node.local ? "local" :
        node.name ? "name" : "id"),
        bindings
      );

    } else if (Node.check(node) && !Expression.check(node)) {
      types.eachField(node, function(name: any, child: any) {
        var childPath = path.get(name);
        if (!pathHasValue(childPath, child)) {
          throw new Error("");
        }
        recursiveScanChild(childPath, bindings, scopeTypes);
      });
    }
  }

  function pathHasValue(path: NodePath, value: any) {
    if (path.value === value) {
      return true;
    }

    // Empty arrays are probably produced by defaults.emptyArray, in which
    // case is makes sense to regard them as equivalent, if not ===.
    if (Array.isArray(path.value) &&
      path.value.length === 0 &&
      Array.isArray(value) &&
      value.length === 0) {
      return true;
    }

    return false;
  }

  function recursiveScanChild(path: NodePath, bindings: any, scopeTypes: any) {
    var node = path.value;

    if (!node || Expression.check(node)) {
      // Ignore falsy values and Expressions.

    } else if (namedTypes.FunctionDeclaration.check(node) &&
           node.id !== null) {
      addPattern(path.get("id"), bindings);

    } else if (namedTypes.ClassDeclaration &&
      namedTypes.ClassDeclaration.check(node) &&
      node.id !== null) {
      addPattern(path.get("id"), bindings);
      recursiveScanScope(path.get("typeParameters"), bindings, scopeTypes);

    } else if (
      (namedTypes.InterfaceDeclaration &&
       namedTypes.InterfaceDeclaration.check(node)) ||
      (namedTypes.TSInterfaceDeclaration &&
       namedTypes.TSInterfaceDeclaration.check(node))
    ) {
      addTypePattern(path.get("id"), scopeTypes);

    } else if (ScopeType.check(node)) {
      if (
        namedTypes.CatchClause.check(node) &&
        // TODO Broaden this to accept any pattern.
        namedTypes.Identifier.check(node.param)
      ) {
        var catchParamName = node.param.name;
        var hadBinding = hasOwn.call(bindings, catchParamName);

        // Any declarations that occur inside the catch body that do
        // not have the same name as the catch parameter should count
        // as bindings in the outer scope.
        recursiveScanScope(path.get("body"), bindings, scopeTypes);

        // If a new binding matching the catch parameter name was
        // created while scanning the catch body, ignore it because it
        // actually refers to the catch parameter and not the outer
        // scope that we're currently scanning.
        if (!hadBinding) {
          delete bindings[catchParamName];
        }
      }

    } else {
      recursiveScanScope(path, bindings, scopeTypes);
    }
  }

  function addPattern(patternPath: NodePath, bindings: any) {
    var pattern = patternPath.value;
    namedTypes.Pattern.assert(pattern);

    if (namedTypes.Identifier.check(pattern)) {
      if (hasOwn.call(bindings, pattern.name)) {
        bindings[pattern.name].push(patternPath);
      } else {
        bindings[pattern.name] = [patternPath];
      }

    } else if (namedTypes.AssignmentPattern &&
      namedTypes.AssignmentPattern.check(pattern)) {
      addPattern(patternPath.get('left'), bindings);

    } else if (
      namedTypes.ObjectPattern &&
      namedTypes.ObjectPattern.check(pattern)
    ) {
      patternPath.get('properties').each(function(propertyPath: any) {
        var property = propertyPath.value;
        if (namedTypes.Pattern.check(property)) {
          addPattern(propertyPath, bindings);
        } else if (
          namedTypes.Property.check(property) ||
          (namedTypes.ObjectProperty &&
           namedTypes.ObjectProperty.check(property))
        ) {
          addPattern(propertyPath.get('value'), bindings);
        } else if (
          namedTypes.SpreadProperty &&
          namedTypes.SpreadProperty.check(property)
        ) {
          addPattern(propertyPath.get('argument'), bindings);
        }
      });

    } else if (
      namedTypes.ArrayPattern &&
      namedTypes.ArrayPattern.check(pattern)
    ) {
      patternPath.get('elements').each(function(elementPath: any) {
        var element = elementPath.value;
        if (namedTypes.Pattern.check(element)) {
          addPattern(elementPath, bindings);
        } else if (
          namedTypes.SpreadElement &&
          namedTypes.SpreadElement.check(element)
        ) {
          addPattern(elementPath.get("argument"), bindings);
        }
      });

    } else if (
      namedTypes.PropertyPattern &&
      namedTypes.PropertyPattern.check(pattern)
    ) {
      addPattern(patternPath.get('pattern'), bindings);

    } else if (
      (namedTypes.SpreadElementPattern &&
       namedTypes.SpreadElementPattern.check(pattern)) ||
      (namedTypes.RestElement &&
       namedTypes.RestElement.check(pattern)) ||
      (namedTypes.SpreadPropertyPattern &&
       namedTypes.SpreadPropertyPattern.check(pattern))
    ) {
      addPattern(patternPath.get('argument'), bindings);
    }
  }

  function addTypePattern(patternPath: NodePath, types: any) {
    var pattern = patternPath.value;
    namedTypes.Pattern.assert(pattern);

    if (namedTypes.Identifier.check(pattern)) {
      if (hasOwn.call(types, pattern.name)) {
        types[pattern.name].push(patternPath);
      } else {
        types[pattern.name] = [patternPath];
      }
    }
  }

  function addTypeParameter(parameterPath: NodePath, types: any) {
    var parameter = parameterPath.value;
    FlowOrTSTypeParameterType.assert(parameter);

    if (hasOwn.call(types, parameter.name)) {
      types[parameter.name].push(parameterPath);
    } else {
      types[parameter.name] = [parameterPath];
    }
  }

  Sp.lookup = function(name) {
    for (var scope = this; scope; scope = scope.parent)
      if (scope.declares(name))
        break;
    return scope;
  };

  Sp.lookupType = function(name) {
    for (var scope = this; scope; scope = scope.parent)
      if (scope.declaresType(name))
        break;
    return scope;
  };

  Sp.getGlobalScope = function() {
    var scope = this;
    while (!scope.isGlobal)
      scope = scope.parent;
    return scope;
  };

  return Scope;
};