Socket
Socket
Sign inDemoInstall

ast-types

Package Overview
Dependencies
11
Maintainers
1
Versions
172
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.21 to 0.2.22

145

lib/traverse.js
var assert = require("assert");
var types = require("./types");
var Node = types.namedTypes.Node;
var Type = types.Type;
var namedTypes = types.namedTypes;
var Node = namedTypes.Node;
var isArray = types.builtInTypes.array;
var hasOwn = Object.prototype.hasOwnProperty;

@@ -17,8 +20,146 @@ function Path(node, parentPath) {

var parentScope = parentPath && parentPath.scope;
var scope = null;
if (Scope.isEstablishedBy(node)) {
scope = new Scope(node, parentScope);
} else if (parentScope) {
scope = parentScope;
}
Object.defineProperties(this, {
node: { value: node },
parent: { value: parentPath }
parent: { value: parentPath },
scope: { value: scope }
});
}
function Scope(node, parentScope) {
assert.ok(Scope.isEstablishedBy(node));
if (parentScope) {
assert.ok(parentScope instanceof Scope);
} else {
parentScope = null;
}
Object.defineProperties(this, {
node: { value: node },
isGlobal: { value: !parentScope, enumerable: true },
parent: { value: parentScope },
bindings: { value: {} }
});
}
var scopeTypes = [
// 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
];
if (namedTypes.ModuleDeclaration) {
// Include ModuleDeclaration only if it exists (ES6).
scopeTypes.push(namedTypes.ModuleDeclaration);
}
var ScopeType = Type.or.apply(Type, scopeTypes);
Scope.isEstablishedBy = function(node) {
return ScopeType.check(node);
};
var Sp = Scope.prototype;
// Will be overridden after an instance lazily calls scanScope.
Sp.didScan = false;
Sp.declares = function(name) {
if (!this.didScan) {
for (var name in this.bindings) {
// Empty out this.bindings, just in cases.
delete this.bindings[name];
}
scanScope(this.node, this.bindings);
this.didScan = true;
}
return hasOwn.call(this.bindings, name);
};
function scanScope(node, bindings) {
if (isArray.check(node)) {
node.forEach(function(child) {
scanChild(child, bindings);
});
} else if (namedTypes.Function.check(node)) {
node.params.map(function(param) {
addPattern(param, bindings);
});
scanChild(node.body, bindings);
} else if (namedTypes.VariableDeclarator.check(node)) {
addPattern(node.id, bindings);
scanChild(node.init, bindings);
} else if (namedTypes.ImportSpecifier &&
namedTypes.ImportSpecifier.check(node)) {
addPattern(node.name || node.id);
} else if (Node.check(node)) {
types.eachField(node, function(name, child) {
scanChild(child, bindings);
});
}
}
function scanChild(node, bindings) {
if (namedTypes.FunctionDeclaration.check(node)) {
addPattern(node.id, bindings);
} else if (namedTypes.ClassDeclaration &&
namedTypes.ClassDeclaration.check(node)) {
addPattern(node.id, bindings);
} else if (Scope.isEstablishedBy(node)) {
// Don't descend into nested scopes.
} else {
scanScope(node, bindings);
}
}
function addPattern(pattern, bindings) {
namedTypes.Pattern.assert(pattern);
if (namedTypes.Identifier.check(pattern)) {
bindings[pattern.name] = pattern;
} else if (namedTypes.SpreadElement &&
namedTypes.SpreadElement.check(pattern)) {
addPattern(pattern.argument, bindings);
}
}
Sp.lookup = function(name) {
for (var scope = this; scope; scope = scope.parent)
if (scope.declares(name))
break;
return scope;
};
Sp.getGlobalScope = function() {
var scope = this;
while (!scope.isGlobal)
scope = scope.parent;
return scope;
};
module.exports = function(node, callback) {

@@ -25,0 +166,0 @@ function traverse(node, parentPath) {

2

package.json

@@ -21,3 +21,3 @@ {

],
"version": "0.2.21",
"version": "0.2.22",
"homepage": "http://github.com/benjamn/ast-types",

@@ -24,0 +24,0 @@ "repository": {

@@ -324,1 +324,43 @@ var types = require("../main");

};
var scope = [
"var foo = 42;",
"function bar(baz) {",
" return baz + foo;",
"}"
];
exports.testGlobalScope = function(t, assert) {
var traverse = types.traverse;
var ast = parse(scope.join("\n"));
var globalScope;
traverse(ast, function(node) {
if (n.Program.check(node)) {
assert.strictEqual(this.scope.isGlobal, true);
globalScope = this.scope;
} else if (n.FunctionDeclaration.check(node)) {
assert.strictEqual(this.scope.isGlobal, false);
assert.strictEqual(node.id.name, "bar");
assert.notStrictEqual(this.scope, globalScope);
assert.strictEqual(this.scope.isGlobal, false);
assert.strictEqual(this.scope.parent, globalScope);
assert.strictEqual(this.scope.getGlobalScope(), globalScope);
assert.ok(globalScope.declares("foo"));
assert.ok(globalScope.declares("bar"));
assert.strictEqual(this.scope.lookup("foo"), globalScope);
assert.strictEqual(this.scope.lookup("bar"), globalScope);
assert.ok(this.scope.declares("baz"));
assert.strictEqual(this.scope.lookup("baz"), this.scope);
assert.strictEqual(this.scope.lookup("qux"), null);
assert.strictEqual(globalScope.lookup("baz"), null);
}
});
t.finish();
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc