From e72d4508deffd834b6296a91dccc7cf3ab1c74f1 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 3 Nov 2015 03:00:27 +0000 Subject: [PATCH] only use outer bindings when registering a binding to prevent retreiving function params - fixes #2615 --- packages/babel-traverse/src/scope/index.js | 4 +-- packages/babel-traverse/test/scope.js | 38 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 packages/babel-traverse/test/scope.js diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index 033eb52048..aa1b398273 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -402,7 +402,7 @@ export default class Scope { if (path.isLabeledStatement()) { this.registerBinding("label", path); } else if (path.isFunctionDeclaration()) { - this.registerBinding("hoisted", path.get("id")); + this.registerBinding("hoisted", path); } else if (path.isVariableDeclaration()) { let declarations = path.get("declarations"); for (let declar of (declarations: Array)) { @@ -453,7 +453,7 @@ export default class Scope { } let parent = this.getProgramParent(); - let ids = path.getBindingIdentifiers(true); + let ids = path.getOuterBindingIdentifiers(true); for (let name in ids) { for (let id of (ids[name]: Array)) { diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js new file mode 100644 index 0000000000..d58ccc1ae3 --- /dev/null +++ b/packages/babel-traverse/test/scope.js @@ -0,0 +1,38 @@ +var traverse = require("../lib").default; +var assert = require("assert"); +var parse = require("babylon").parse; + +function getPath(code) { + var ast = parse(code); + var path; + traverse(ast, { + Program: function (_path) { + path = _path; + _path.stop(); + } + }); + return path; +} + +suite("scope", function () { + suite("binding paths", function () { + test("function declaration id", function () { + assert.ok(getPath("function foo() {}").scope.getBinding("foo").path.type === "FunctionDeclaration"); + }); + + test("function expression id", function () { + assert.ok(getPath("(function foo() {})").get("body")[0].get("expression").scope.getBinding("foo").path.type === "FunctionExpression"); + }); + + test("function param", function () { + assert.ok(getPath("(function (foo) {})").get("body")[0].get("expression").scope.getBinding("foo").path.type === "Identifier"); + }); + + test("variable declaration", function () { + assert.ok(getPath("var foo = null;").scope.getBinding("foo").path.type === "VariableDeclarator"); + assert.ok(getPath("var { foo } = null;").scope.getBinding("foo").path.type === "VariableDeclarator"); + assert.ok(getPath("var [ foo ] = null;").scope.getBinding("foo").path.type === "VariableDeclarator"); + assert.ok(getPath("var { bar: [ foo ] } = null;").scope.getBinding("foo").path.type === "VariableDeclarator"); + }); + }); +});