Permalink
Browse files

Fix getBindingIdentifiers in babel-types (#5068)

* Added getBindingIdentifier tests

* Added failing test for getBindingIdentifiers

* Fix babel-types getBindingIdentifiers
  • Loading branch information...
1 parent dc61712 commit 39d18679e1e6f50b7141473d9bf404c0e73bb8fd @rtsao rtsao committed with hzoo Jan 9, 2017
Showing with 29 additions and 2 deletions.
  1. +2 −2 packages/babel-types/src/retrievers.js
  2. +27 −0 packages/babel-types/test/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;
}
@@ -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"]);
+ });
+ });
+});

0 comments on commit 39d1867

Please sign in to comment.