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: False positive TDZ check in class computed property keys #16429

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ function handleClassTDZ(

const classFieldDefinitionEvaluationTDZVisitor: Visitor<HandleClassTDZState> = {
ReferencedIdentifier: handleClassTDZ,
Function(path) {
const { parentPath } = path;
if (
!(parentPath.isCallExpression() && parentPath.node.callee === path.node)
) {
path.skip();
}
},
};

interface RenamerState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@
expect(errs.map(e => e.constructor)).toEqual([E, E, E]);

const C = K;
// expect(fns.map(fn => fn())).toEqual([C, C, C]);
// todo: remove these three and enable the assertions above when we properly handle class tdz
expect(fns[0]()).toEqual(C);
expect(fns[1]).toThrow(E);
expect(fns[2]).toThrow(E);
expect(fns.map(fn => fn())).toEqual([C, C, C]);

K = null;

// expect(fns.map(fn => fn())).toEqual([null, C, C]);
// todo: remove these three and enable the assertions above when we properly handle class tdz
expect(fns[0]()).toEqual(null);
expect(fns[1]).toThrow(E);
expect(fns[2]).toThrow(E);
expect(fns[1]()).toEqual(null);
expect(fns[2]()).toEqual(null);
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
};
capture(() => K);
assertUninitialized(() => K);
_ref = (_computedKeyDecs = [capture(() => (babelHelpers.classNameTDZError("K"), K)), assertUninitialized(() => (babelHelpers.classNameTDZError("K"), K))], _computedKey = babelHelpers.toPropertyKey((capture(() => (babelHelpers.classNameTDZError("K"), K)), assertUninitialized(() => (babelHelpers.classNameTDZError("K"), K)))));
_ref = (_computedKeyDecs = [capture(() => K), assertUninitialized(() => K)], _computedKey = babelHelpers.toPropertyKey((capture(() => K), assertUninitialized(() => K))));
class K {
constructor() {
babelHelpers.defineProperty(this, _ref, _init_computedKey(this));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let foo;
class A {
static x = 42;
static [foo = () => A.x];
}
expect(foo()).toBe(42);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let foo;
class A {
static x = 42;
static [foo = () => A.x];
}
expect(foo()).toBe(42);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-class-properties", "transform-classes"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let _foo;
let foo;
_foo = foo = () => A.x;
let A = /*#__PURE__*/babelHelpers.createClass(function A() {
"use strict";

babelHelpers.classCallCheck(this, A);
});
babelHelpers.defineProperty(A, "x", 42);
babelHelpers.defineProperty(A, _foo, void 0);
expect(foo()).toBe(42);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
expect(() => {
class A {
static x = 42;
static [(() => A.x)()];
}
}).toThrow(ReferenceError);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
expect(() => {
class A {
static x = 42;
static [(() => A.x)()];
}
}).toThrow(ReferenceError);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-class-properties", "transform-classes"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
expect(() => {
let _ref;
_ref = (() => (babelHelpers.classNameTDZError("A"), A).x)();
let A = /*#__PURE__*/babelHelpers.createClass(function A() {
"use strict";

babelHelpers.classCallCheck(this, A);
});
babelHelpers.defineProperty(A, "x", 42);
babelHelpers.defineProperty(A, _ref, void 0);
}).toThrow(ReferenceError);