Skip to content

Commit

Permalink
Fix file helpers, re-enable exec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-mc committed Oct 14, 2018
1 parent 95a5b3f commit dfd0bb1
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 81 deletions.
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -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;
}
`;

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-proposal-class-properties/src/index.js
Expand Up @@ -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"), []);
},
};

Expand Down
@@ -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);
Expand Up @@ -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);
}
Expand Down
@@ -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();
Expand Up @@ -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);
}
};

Expand Down
@@ -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');
Expand Up @@ -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,
};
}
}
Expand Up @@ -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",
Expand All @@ -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
});
};
Expand All @@ -40,7 +40,7 @@ function () {
value: function getFakeStatusFunc() {
return {
status: 'fake-status',
getFakeStatus: babelHelpers.classPrivateMethodGet(this, _getStatus)
getFakeStatus: babelHelpers.classPrivateMethodGet(this, _getStatus, getStatus)
};
}
}]);
Expand Down

0 comments on commit dfd0bb1

Please sign in to comment.