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

Evaluate object and initializer when setting a private method #12707

Merged
merged 5 commits into from Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -299,8 +299,12 @@ const privateNameHandlerSpec = {
value,
]);
}
return t.callExpression(file.addHelper("readOnlyError"), [
t.stringLiteral(`#${name}`),
return t.sequenceExpression([
this.receiver(member),
value,
t.callExpression(file.addHelper("readOnlyError"), [
t.stringLiteral(`#${name}`),
]),
]);
}
return t.callExpression(file.addHelper("classPrivateFieldSet"), [
Expand Down
@@ -0,0 +1,13 @@
let counter = 0;
class Foo {
constructor() {
this.#privateFieldValue = ++counter;
}

get #privateFieldValue() {
return 42;
}
}

expect(() => new Foo).toThrow();
expect(counter).toBe(1);
@@ -0,0 +1,10 @@
let counter = 0;
class Foo {
constructor() {
this.#privateFieldValue = ++counter;
}

get #privateFieldValue() {
return 42;
}
}
@@ -0,0 +1,18 @@
var counter = 0;

var _privateFieldValue = babelHelpers.classPrivateFieldLooseKey("privateFieldValue");

class Foo {
constructor() {
Object.defineProperty(this, _privateFieldValue, {
get: _get_privateFieldValue,
set: void 0
});
babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue] = ++counter;
}

}

var _get_privateFieldValue = function () {
return 42;
};
@@ -0,0 +1,18 @@
let results = [];
class Foo {
constructor() {
this.self.#privateFieldValue = results.push(2);
}

get self() {
results.push(1);
return this;
}

get #privateFieldValue() {
return 42;
}
}

expect(() => new Foo).toThrow(TypeError);
expect(results).toStrictEqual([1, 2]);
@@ -0,0 +1,15 @@
let results = [];
class Foo {
constructor() {
this.self.#privateFieldValue = results.push(2);
}

get self() {
results.push(1);
return this;
}

get #privateFieldValue() {
return 42;
}
}
@@ -0,0 +1,24 @@
var results = [];

var _privateFieldValue = new WeakMap();

class Foo {
constructor() {
_privateFieldValue.set(this, {
get: _get_privateFieldValue,
set: void 0
});

this.self, results.push(2), babelHelpers.readOnlyError("#privateFieldValue");
}

get self() {
results.push(1);
return this;
}

}

var _get_privateFieldValue = function () {
return 42;
};
Expand Up @@ -14,7 +14,7 @@ class Cl {
value: 0
});

babelHelpers.readOnlyError("#privateFieldValue");
this, 1, babelHelpers.readOnlyError("#privateFieldValue");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a few more unnecessary bytes, but this path will always result in an error so it's highly improbable that it will happen in production code.

}

}
Expand Down
@@ -0,0 +1,13 @@
let counter = 0;
class Foo {
constructor() {
this.#privateMethod = ++counter;
}

#privateMethod() {
return 42;
}
}

expect(() => new Foo).toThrow();
expect(counter).toBe(1);
@@ -0,0 +1,10 @@
let counter = 0;
class Foo {
constructor() {
this.#privateMethod = ++counter;
}

#privateMethod() {
return 42;
}
}
@@ -0,0 +1,17 @@
var counter = 0;

var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod");

class Foo {
constructor() {
Object.defineProperty(this, _privateMethod, {
value: _privateMethod2
});
babelHelpers.classPrivateFieldLooseBase(this, _privateMethod)[_privateMethod] = ++counter;
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
}

}

var _privateMethod2 = function _privateMethod2() {
return 42;
};
Expand Up @@ -6,7 +6,7 @@ class A {
}

run() {
babelHelpers.readOnlyError("#method");
this, 2, babelHelpers.readOnlyError("#method");
}

}
Expand Down
@@ -0,0 +1,18 @@
let results = [];
class Foo {
constructor() {
this.self.#privateFieldValue = results.push(2);
}

get self() {
results.push(1);
return this;
}

#privateFieldValue() {
return 42;
}
}

expect(() => new Foo).toThrow(TypeError);
expect(results).toStrictEqual([1, 2]);
@@ -0,0 +1,15 @@
let results = [];
class Foo {
constructor() {
this.self.#privateFieldValue = results.push(2);
}

get self() {
results.push(1);
return this;
}

#privateFieldValue() {
return 42;
}
}
@@ -0,0 +1,21 @@
var results = [];

var _privateFieldValue = new WeakSet();

class Foo {
constructor() {
_privateFieldValue.add(this);

this.self, results.push(2), babelHelpers.readOnlyError("#privateFieldValue");
}

get self() {
results.push(1);
return this;
}

}

var _privateFieldValue2 = function _privateFieldValue2() {
return 42;
};