Skip to content

Commit

Permalink
Fix missing ClassDeclaration references after scope#crawl (#11011)
Browse files Browse the repository at this point in the history
* Bug replication test

* Simplify test

* Fixes #10896

* Merge path.crawl `ClassDeclaration` and `Declaration` visitors

Merged to avoid subtle assumption that `Declaration` is called before `ClassDeclaration`

* Move registartion of class id in crawl to BlockScoped

* Add some assertions to crawl test
  • Loading branch information
regiontog authored and JLHwung committed Jan 20, 2020
1 parent 2b23c28 commit 740064c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/babel-traverse/src/scope/index.js
Expand Up @@ -128,15 +128,17 @@ const collectorVisitor = {
BlockScoped(path) {
let scope = path.scope;
if (scope.path === path) scope = scope.parent;
scope.getBlockParent().registerDeclaration(path);
},

ClassDeclaration(path) {
const id = path.node.id;
if (!id) return;
const parent = scope.getBlockParent();
parent.registerDeclaration(path);

// Register class identifier in class' scope if this is a class declaration.
if (path.isClassDeclaration() && path.node.id) {
const id = path.node.id;
const name = id.name;

const name = id.name;
path.scope.bindings[name] = path.scope.getBinding(name);
path.scope.bindings[name] = path.scope.parent.getBinding(name);
}
},

Block(path) {
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-traverse/test/scope.js
Expand Up @@ -289,6 +289,20 @@ describe("scope", () => {
column: 32,
});
});

it("class identifier available in class scope after crawl", function() {
const path = getPath("class a { build() { return new a(); } }");

path.scope.crawl();

let referencePaths = path.scope.bindings.a.referencePaths;
expect(referencePaths).toHaveLength(1);

referencePaths = path.get("body[0]").scope.bindings.a.referencePaths;
expect(referencePaths).toHaveLength(1);

expect(path.scope.bindings.a).toBe(path.get("body[0]").scope.bindings.a);
});
});

describe("duplicate bindings", () => {
Expand Down

0 comments on commit 740064c

Please sign in to comment.