From 3ccb3ba1b098804c7cd7fc06f978b7785d9a3f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 23 Mar 2020 23:09:58 +0100 Subject: [PATCH] Fixed issue with programPath.scope.references not being registered back correctly after scope re-crawling --- packages/babel-traverse/src/scope/index.js | 4 ++-- packages/babel-traverse/test/scope.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index df8f81f3560d..c4d8a707541c 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -609,6 +609,8 @@ export default class Scope { const ids = path.getOuterBindingIdentifiers(true); for (const name of Object.keys(ids)) { + parent.references[name] = true; + for (const id of (ids[name]: Array)) { const local = this.getOwnBinding(name); @@ -620,8 +622,6 @@ export default class Scope { this.checkBlockScopedCollisions(local, kind, name, id); } - parent.references[name] = true; - // A redeclaration of an existing variable is a modification if (local) { this.registerConstantViolation(bindingPath); diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js index 26efc491409e..5c48be4c046c 100644 --- a/packages/babel-traverse/test/scope.js +++ b/packages/babel-traverse/test/scope.js @@ -303,6 +303,24 @@ describe("scope", () => { expect(path.scope.bindings.a).toBe(path.get("body[0]").scope.bindings.a); }); + + it("references after re-crawling", function() { + const path = getPath("function Foo() { var _jsx; }"); + + path.scope.crawl(); + path.scope.crawl(); + + expect(path.scope.references._jsx).toBeTruthy(); + }); + + test("generateUid collision check after re-crawling", function() { + const path = getPath("function Foo() { var _jsx; }"); + + path.scope.crawl(); + path.scope.crawl(); + + expect(path.scope.generateUid("jsx")).toBe("_jsx2"); + }); }); describe("duplicate bindings", () => {