From 39d18679e1e6f50b7141473d9bf404c0e73bb8fd Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Mon, 9 Jan 2017 06:08:07 -0800 Subject: [PATCH] Fix getBindingIdentifiers in babel-types (#5068) * Added getBindingIdentifier tests * Added failing test for getBindingIdentifiers * Fix babel-types getBindingIdentifiers --- packages/babel-types/src/retrievers.js | 4 ++-- packages/babel-types/test/retrievers.js | 27 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 packages/babel-types/test/retrievers.js diff --git a/packages/babel-types/src/retrievers.js b/packages/babel-types/src/retrievers.js index 12bba815b0..c4d7647b4a 100644 --- a/packages/babel-types/src/retrievers.js +++ b/packages/babel-types/src/retrievers.js @@ -29,8 +29,8 @@ export function getBindingIdentifiers( } if (t.isExportDeclaration(id)) { - if (t.isDeclaration(node.declaration)) { - search.push(node.declaration); + if (t.isDeclaration(id.declaration)) { + search.push(id.declaration); } continue; } diff --git a/packages/babel-types/test/retrievers.js b/packages/babel-types/test/retrievers.js new file mode 100644 index 0000000000..68eb34c4f3 --- /dev/null +++ b/packages/babel-types/test/retrievers.js @@ -0,0 +1,27 @@ +import * as t from "../lib"; +import assert from "assert"; +import { parse } from "babylon"; + +function getBody(program) { + return parse(program, {sourceType: "module"}).program.body; +} + +describe("retrievers", function () { + describe("getBindingIdentifiers", function () { + it("variable declarations", function () { + const program = "var a = 1; let b = 2; const c = 3;"; + const ids = t.getBindingIdentifiers(getBody(program)); + assert.deepEqual(Object.keys(ids), ["a", "b", "c"]); + }); + it("function declarations", function () { + const program = "var foo = 1; function bar() { var baz = 2; }"; + const ids = t.getBindingIdentifiers(getBody(program)); + assert.deepEqual(Object.keys(ids), ["bar", "foo"]); + }); + it("export named declarations", function () { + const program = "export const foo = 'foo';"; + const ids = t.getBindingIdentifiers(getBody(program)); + assert.deepEqual(Object.keys(ids), ["foo"]); + }); + }); +});