Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reference to class expression in private method #13429

Merged
merged 6 commits into from Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -669,6 +669,16 @@ const thisContextVisitor = traverse.visitors.merge([
environmentVisitor,
]);

const innerReferencesVisitor = {
ReferencedIdentifier(path, state) {
const { name } = path.node;
if (path.scope.bindingIdentifierEquals(name, state.innerBinding)) {
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
state.needsClassRef = true;
path.replaceWith(t.cloneNode(state.classRef));
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
}
},
};

function replaceThisContext(
path,
ref,
Expand All @@ -677,7 +687,8 @@ function replaceThisContext(
isStaticBlock,
constantSuper,
) {
const state = { classRef: ref, needsClassRef: false };
const { innerBinding, ...classRef } = ref;
const state = { classRef, needsClassRef: false, innerBinding };

const replacer = new ReplaceSupers({
methodPath: path,
Expand All @@ -696,6 +707,11 @@ function replaceThisContext(
if (isStaticBlock || path.isProperty()) {
path.traverse(thisContextVisitor, state);
}

if (state.classRef?.name !== innerBinding?.name) {
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
path.traverse(innerReferencesVisitor, state);
}

return state.needsClassRef;
}

Expand Down
Expand Up @@ -168,15 +168,18 @@ export function createClassFeaturePlugin({

if (!props.length && !isDecorated) return;

let ref;

let ref = undefined;
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
if (path.isClassExpression() || !path.node.id) {
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
nameFunction(path);
ref = path.scope.generateUidIdentifier("class");
} else {
ref = t.cloneNode(path.node.id);
}

if (ref !== undefined) {
ref.innerBinding = path.node.id;
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
lala7573 marked this conversation as resolved.
Show resolved Hide resolved
}

// NODE: These three functions don't support decorators yet,
// but verifyUsedFeatures throws if there are both
// decorators and private fields.
Expand Down
@@ -0,0 +1,19 @@
function wrapper(wc) {
return wc
}

const f = wrapper(class Foo {
static #x = Foo;
static y = Foo;

static extract() {
return {
x: Foo.#x,
y: Foo.y,
}
}
});

const { x, y } = f.extract();
expect(x).toBe(f)
expect(y).toBe(f)
@@ -0,0 +1,8 @@
function wrapper(wc) {
return wc
}
lala7573 marked this conversation as resolved.
Show resolved Hide resolved

const f = wrapper(class Foo {
static #x = Foo;
static y = Foo;
});
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-class-properties", "transform-block-scoping"]
}
@@ -0,0 +1,10 @@
var _class, _temp, _x;

function wrapper(wc) {
return wc;
}

var f = wrapper((_temp = _class = class Foo {}, _x = {
writable: true,
value: _class
}, babelHelpers.defineProperty(_class, "y", _class), _temp));
@@ -0,0 +1,35 @@
function wrapper(wc) {
return wc
}

const f = wrapper(class Foo {
static #bar() {
return Foo;
}

static #method() {
return function inner() {
return Foo;
};
}
static #method_shadowed() {
new Foo();
return function inner() {
let Foo = 3;
return Foo;
}
}

static extract() {
return {
bar: Foo.#bar,
method: Foo.#method,
method_shadowed: Foo.#method_shadowed
}
}
});

const { bar, method, method_shadowed } = f.extract();
expect(bar()).toBe(f)
expect(method()()).toBe(f)
expect(method_shadowed()()).toBe(3)
@@ -0,0 +1,22 @@
function wrapper(wc) {
return wc
}

wrapper(class Foo {
static #bar() {
return Foo;
}

static #method() {
return function inner() {
return Foo;
};
}
static #method_shadowed() {
new Foo();
return function inner() {
let Foo = 3;
return Foo;
}
}
});
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-class-properties", "transform-block-scoping"]
}
@@ -0,0 +1,25 @@
var _class;

function wrapper(wc) {
return wc;
}

wrapper(_class = class Foo {});

function _bar() {
return _class;
}

function _method() {
return function inner() {
return _class;
};
}

function _method_shadowed() {
new _class();
return function inner() {
var Foo = 3;
return Foo;
};
}