From 0204bd5d5fbe7e3b74d1a1ea50413148978660a5 Mon Sep 17 00:00:00 2001 From: Timothy McClure Date: Sun, 14 Oct 2018 15:50:52 -0400 Subject: [PATCH] Fix file helpers, re-enable exec tests --- packages/babel-helpers/src/helpers.js | 4 +- .../src/index.js | 3 +- .../test/fixtures/private/assignment/exec.js | 32 ++++---- .../fixtures/private/assignment/output.js | 2 +- .../private/method-exfiltrated/exec.js | 22 ++--- .../private/method-exfiltrated/output.js | 2 +- .../test/fixtures/private/method/exec.js | 82 +++++++++---------- .../test/fixtures/private/method/input.js | 8 +- .../test/fixtures/private/method/output.js | 8 +- 9 files changed, 82 insertions(+), 81 deletions(-) diff --git a/packages/babel-helpers/src/helpers.js b/packages/babel-helpers/src/helpers.js index 8bcb77162a7c..4bb576dd9031 100644 --- a/packages/babel-helpers/src/helpers.js +++ b/packages/babel-helpers/src/helpers.js @@ -1719,11 +1719,11 @@ helpers.decorate = helper("7.0.2")` `; helpers.classPrivateMethodGet = helper("7.0.0-beta.0")` - export default function _classPrivateMethodGet(receiver, privateSet) { + export default function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } - return privateSet.get(receiver); + return fn; } `; diff --git a/packages/babel-plugin-proposal-class-properties/src/index.js b/packages/babel-plugin-proposal-class-properties/src/index.js index be4b8d377a18..9c9233c00fd9 100644 --- a/packages/babel-plugin-proposal-class-properties/src/index.js +++ b/packages/babel-plugin-proposal-class-properties/src/index.js @@ -197,12 +197,13 @@ export default declare((api, options) => { return t.callExpression(file.addHelper("classPrivateMethodGet"), [ this.receiver(member), t.cloneNode(map), + t.identifier(this.name), ]); }, set() { const { file } = this; - return t.callExpression(file.addHelper("classPrivateMethodSet")); + return t.callExpression(file.addHelper("classPrivateMethodSet"), []); }, }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/exec.js index 0b86fe4c9243..f1ec41338b6a 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/exec.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/exec.js @@ -1,19 +1,19 @@ -// class Foo { -// #foo = 0; -// constructor() { -// this.publicField = this.#privateMethod(); -// } +class Foo { + #foo = 0; + constructor() { + this.publicField = this.#privateMethod(); + } -// test(other) { -// this.#foo += 1; -// this.#foo = 2; -// other.obj.#foo += 1; -// other.obj.#foo = 2; -// } + test(other) { + this.#foo += 1; + this.#foo = 2; + other.obj.#foo += 1; + other.obj.#foo = 2; + } -// #privateMethod() { -// return 42; -// } -// } + #privateMethod() { + return 42; + } +} -// expect((new Foo).publicField).toEqual(42); +expect((new Foo).publicField).toEqual(42); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/output.js index 2d87c0634d5c..11c910f337f3 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/assignment/output.js @@ -15,7 +15,7 @@ function () { value: 0 }); - babelHelpers.defineProperty(this, "publicField", babelHelpers.classPrivateMethodGet(this, _privateMethod).call(this)); + babelHelpers.defineProperty(this, "publicField", babelHelpers.classPrivateMethodGet(this, _privateMethod, privateMethod).call(this)); _privateMethod.add(this); } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/exec.js index 06c88436fedc..9a79802b0a14 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/exec.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/exec.js @@ -1,13 +1,13 @@ -// let exfiltrated; -// class Foo { -// #privateMethod() {} +let exfiltrated; +class Foo { + #privateMethod() {} -// constructor() { -// if (exfiltrated === undefined) { -// exfiltrated = this.#privateMethod; -// } -// expect(exfiltrated).toStrictEqual(this.#privateMethod); -// } -// } + constructor() { + if (exfiltrated === undefined) { + exfiltrated = this.#privateMethod; + } + expect(exfiltrated).toStrictEqual(this.#privateMethod); + } +} -// new Foo(); +new Foo(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/output.js index cc5a16241acc..19ef3b366693 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method-exfiltrated/output.js @@ -10,7 +10,7 @@ var Foo = function Foo() { _privateMethod.add(this); if (exfiltrated === undefined) { - exfiltrated = babelHelpers.classPrivateMethodGet(this, _privateMethod); + exfiltrated = babelHelpers.classPrivateMethodGet(this, _privateMethod, privateMethod); } }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/exec.js index f96ef569fa0e..7bc5545a23f9 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/exec.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/exec.js @@ -1,41 +1,41 @@ -// class Foo { -// constructor(status) { -// this.status = status; -// expect(() => this.#getStatus = null).toThrow(TypeError); -// } - -// #getStatus() { -// return this.status; -// } - -// getCurrentStatus() { -// return this.#getStatus(); -// } - -// setCurrentStatus(newStatus) { -// this.status = newStatus; -// } - -// getFakeStatus(fakeStatus) { -// const getStatus = this.#getStatus; -// return function() { -// return getStatus.call({ status: fakeStatus }); -// }; -// } - -// getFakeStatusFunc() { -// return { -// status: 'fake-status', -// getFakeStatus: this.#getStatus, -// }; -// } -// } - -// const f = new Foo('inactive'); -// expect(f.getCurrentStatus()).toBe('inactive'); - -// f.setCurrentStatus('new-status'); -// expect(f.getCurrentStatus()).toBe('new-status'); - -// expect(f.getFakeStatus('fake')()).toBe('fake'); -// expect(f.getFakeStatusFunc().getFakeStatus()).toBe('fake-status'); +class Foo { + constructor(status) { + this.status = status; + expect(() => this.#getStatus = null).toThrow(TypeError); + } + + #getStatus() { + return this.status; + } + + getCurrentStatus() { + return this.#getStatus(); + } + + setCurrentStatus(newStatus) { + this.status = newStatus; + } + + getFakeStatus(fakeStatus) { + const fakeGetStatus = this.#getStatus; + return function () { + return fakeGetStatus.call({ status: fakeStatus }); + }; + } + + getFakeStatusFunc() { + return { + status: 'fake-status', + getFakeStatus: this.#getStatus, + }; + } +} + +const f = new Foo('inactive'); +expect(f.getCurrentStatus()).toBe('inactive'); + +f.setCurrentStatus('new-status'); +expect(f.getCurrentStatus()).toBe('new-status'); + +expect(f.getFakeStatus('fake')()).toBe('fake'); +expect(f.getFakeStatusFunc().getFakeStatus()).toBe('fake-status'); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/input.js index b16ab33b1858..67a8ab68a682 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/input.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/input.js @@ -16,16 +16,16 @@ class Foo { } getFakeStatus(fakeStatus) { - const getStatus = this.#getStatus; + const fakeGetStatus = this.#getStatus; return function() { - return getStatus.call({ status: fakeStatus }); + return fakeGetStatus.call({ status: fakeStatus }); }; } getFakeStatusFunc() { return { - status: 'fake-status', - getFakeStatus: this.#getStatus, + status: 'fake-status', + getFakeStatus: this.#getStatus, }; } } \ No newline at end of file diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/output.js index b59d75195f6e..e155a4f09e5c 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/method/output.js @@ -18,7 +18,7 @@ function () { babelHelpers.createClass(Foo, [{ key: "getCurrentStatus", value: function getCurrentStatus() { - return babelHelpers.classPrivateMethodGet(this, _getStatus).call(this); + return babelHelpers.classPrivateMethodGet(this, _getStatus, getStatus).call(this); } }, { key: "setCurrentStatus", @@ -28,9 +28,9 @@ function () { }, { key: "getFakeStatus", value: function getFakeStatus(fakeStatus) { - var getStatus = babelHelpers.classPrivateMethodGet(this, _getStatus); + var fakeGetStatus = babelHelpers.classPrivateMethodGet(this, _getStatus, getStatus); return function () { - return getStatus.call({ + return fakeGetStatus.call({ status: fakeStatus }); }; @@ -40,7 +40,7 @@ function () { value: function getFakeStatusFunc() { return { status: 'fake-status', - getFakeStatus: babelHelpers.classPrivateMethodGet(this, _getStatus) + getFakeStatus: babelHelpers.classPrivateMethodGet(this, _getStatus, getStatus) }; } }]);