Skip to content

Commit

Permalink
Add loose mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-mc committed Jul 19, 2019
1 parent 7d8100b commit 14f480d
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 9 deletions.
48 changes: 44 additions & 4 deletions packages/babel-helper-create-class-features-plugin/src/fields.js
Expand Up @@ -471,7 +471,47 @@ function buildPublicFieldInitSpec(ref, prop, state) {
}

function buildPrivateStaticMethodInitLoose(ref, prop, state, privateNamesMap) {
const { id, methodId } = privateNamesMap.get(prop.node.key.id.name);
const privateName = privateNamesMap.get(prop.node.key.id.name);
const { id, methodId, getId, setId, initAdded } = privateName;
if (initAdded) return;

if (getId || setId) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
initAdded: true,
});

if (getId && setId) {
return template.statement.ast`
Object.defineProperty(${ref}, ${id}, {
// configurable is false by default
// enumerable is false by default
// writable is false by default
get: ${getId.name},
set: ${setId.name}
})
`;
} else if (getId && !setId) {
return template.statement.ast`
Object.defineProperty(${ref}, ${id}, {
// configurable is false by default
// enumerable is false by default
// writable is false by default
get: ${getId.name},
})
`;
} else if (!getId && setId) {
return template.statement.ast`
Object.defineProperty(${ref}, ${id}, {
// configurable is false by default
// enumerable is false by default
// writable is false by default
set: ${setId.name},
})
`;
}
}

return template.statement.ast`
Object.defineProperty(${ref}, ${id}, {
// configurable is false by default
Expand Down Expand Up @@ -665,9 +705,6 @@ export function buildFieldsInitNodes(
break;
case isStatic && isPrivate && isMethod && loose:
needsClassRef = true;
staticNodes.push(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
staticNodes.push(
buildPrivateStaticMethodInitLoose(
t.cloneNode(ref),
Expand All @@ -676,6 +713,9 @@ export function buildFieldsInitNodes(
privateNamesMap,
),
);
staticNodes.unshift(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
case isInstance && isPublic && isField && loose:
instanceNodes.push(buildPublicFieldInitLoose(t.thisExpression(), prop));
Expand Down
Expand Up @@ -20,18 +20,17 @@ var _getA = babelHelpers.classPrivateFieldLooseKey("getA");

var _getB = babelHelpers.classPrivateFieldLooseKey("getB");

var _getB2 = function _getB2() {
return this.b;
};

var _getA2 = function _getA2() {
return A.a;
};

Object.defineProperty(B, _getA, {
value: _getA2
});

var _getB2 = function _getB2() {
return this.b;
};

Object.defineProperty(B, _getB, {
value: _getB2
});
Expand Down
@@ -0,0 +1,23 @@
class Cl {
static #PRIVATE_STATIC_FIELD = "top secret string";

static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
}

static set #privateStaticFieldValue(newValue) {
Cl.#PRIVATE_STATIC_FIELD = `Updated: ${newValue}`;
}

static getValue() {
return Cl.#privateStaticFieldValue;
}

static setValue() {
Cl.#privateStaticFieldValue = "dank";
}
}

expect(Cl.getValue()).toEqual("top secret string");
Cl.setValue();
expect(Cl.getValue()).toEqual("Updated: dank");
@@ -0,0 +1,19 @@
class Cl {
static #PRIVATE_STATIC_FIELD = "top secret string";

static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
}

static set #privateStaticFieldValue(newValue) {
Cl.#PRIVATE_STATIC_FIELD = `Updated: ${newValue}`;
}

static getValue() {
return Cl.#privateStaticFieldValue;
}

static setValue() {
Cl.#privateStaticFieldValue = "dank";
}
}
@@ -0,0 +1,31 @@
class Cl {
static getValue() {
return babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticFieldValue)[_privateStaticFieldValue];
}

static setValue() {
babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticFieldValue)[_privateStaticFieldValue] = "dank";
}

}

var _PRIVATE_STATIC_FIELD = babelHelpers.classPrivateFieldLooseKey("PRIVATE_STATIC_FIELD");

var _privateStaticFieldValue = babelHelpers.classPrivateFieldLooseKey("privateStaticFieldValue");

var _set_privateStaticFieldValue = function (newValue) {
babelHelpers.classPrivateFieldLooseBase(Cl, _PRIVATE_STATIC_FIELD)[_PRIVATE_STATIC_FIELD] = `Updated: ${newValue}`;
};

var _get_privateStaticFieldValue = function () {
return babelHelpers.classPrivateFieldLooseBase(Cl, _PRIVATE_STATIC_FIELD)[_PRIVATE_STATIC_FIELD];
};

Object.defineProperty(Cl, _PRIVATE_STATIC_FIELD, {
writable: true,
value: "top secret string"
});
Object.defineProperty(Cl, _privateStaticFieldValue, {
get: _get_privateStaticFieldValue,
set: _set_privateStaticFieldValue
});
@@ -0,0 +1,13 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;

static set #privateStaticFieldValue(newValue) {
Cl.#PRIVATE_STATIC_FIELD = newValue;
}

static getPrivateStaticFieldValue() {
return Cl.#privateStaticFieldValue;
}
}

expect(Cl.getPrivateStaticFieldValue()).toBeUndefined();
@@ -0,0 +1,11 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;

static set #privateStaticFieldValue(newValue) {
Cl.#PRIVATE_STATIC_FIELD = newValue;
}

static getPrivateStaticFieldValue() {
return Cl.#privateStaticFieldValue;
}
}
@@ -0,0 +1,22 @@
class Cl {
static getPrivateStaticFieldValue() {
return babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticFieldValue)[_privateStaticFieldValue];
}

}

var _PRIVATE_STATIC_FIELD = babelHelpers.classPrivateFieldLooseKey("PRIVATE_STATIC_FIELD");

var _privateStaticFieldValue = babelHelpers.classPrivateFieldLooseKey("privateStaticFieldValue");

var _set_privateStaticFieldValue = function (newValue) {
babelHelpers.classPrivateFieldLooseBase(Cl, _PRIVATE_STATIC_FIELD)[_PRIVATE_STATIC_FIELD] = newValue;
};

Object.defineProperty(Cl, _PRIVATE_STATIC_FIELD, {
writable: true,
value: 0
});
Object.defineProperty(Cl, _privateStaticFieldValue, {
set: _set_privateStaticFieldValue
});
@@ -0,0 +1,14 @@
{
"plugins": [
[
"external-helpers",
{
"helperVersion": "7.1000.0"
}
],
["proposal-private-methods", { "loose": true }],
["proposal-class-properties", { "loose": true }],
"transform-block-scoping",
"syntax-class-properties"
]
}
@@ -0,0 +1,13 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;

static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
}

static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
}
}

expect(() => Cl.setPrivateStaticFieldValue()).toThrow(TypeError);
@@ -0,0 +1,11 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;

static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
}

static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
}
}
@@ -0,0 +1,22 @@
class Cl {
static setPrivateStaticFieldValue() {
babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticFieldValue)[_privateStaticFieldValue] = 1;
}

}

var _PRIVATE_STATIC_FIELD = babelHelpers.classPrivateFieldLooseKey("PRIVATE_STATIC_FIELD");

var _privateStaticFieldValue = babelHelpers.classPrivateFieldLooseKey("privateStaticFieldValue");

var _get_privateStaticFieldValue = function () {
return babelHelpers.classPrivateFieldLooseBase(Cl, _PRIVATE_STATIC_FIELD)[_PRIVATE_STATIC_FIELD];
};

Object.defineProperty(Cl, _PRIVATE_STATIC_FIELD, {
writable: true,
value: 0
});
Object.defineProperty(Cl, _privateStaticFieldValue, {
get: _get_privateStaticFieldValue
});
@@ -0,0 +1,46 @@
class Cl {
static #privateField = "top secret string";
static publicField = "not secret string";

static get #privateFieldValue() {
return Cl.#privateField;
}

static set #privateFieldValue(newValue) {
Cl.#privateField = newValue;
}

static publicGetPrivateField() {
return Cl.#privateFieldValue;
}

static publicSetPrivateField(newValue) {
Cl.#privateFieldValue = newValue;
}

static get publicFieldValue() {
return Cl.publicField;
}

static set publicFieldValue(newValue) {
Cl.publicField = newValue;
}

static testUpdates() {
Cl.#privateField = 0;
Cl.publicField = 0;
Cl.#privateFieldValue = Cl.#privateFieldValue++;
Cl.publicFieldValue = Cl.publicFieldValue++;
expect(Cl.#privateField).toEqual(Cl.publicField);

++Cl.#privateFieldValue;
++Cl.publicFieldValue;
expect(Cl.#privateField).toEqual(Cl.publicField);

Cl.#privateFieldValue += 1;
Cl.publicFieldValue += 1;
expect(Cl.#privateField).toEqual(Cl.publicField);
}
}

Cl.testUpdates();
@@ -0,0 +1,44 @@
class Cl {
static #privateField = "top secret string";
static publicField = "not secret string";

static get #privateFieldValue() {
return Cl.#privateField;
}

static set #privateFieldValue(newValue) {
Cl.#privateField = newValue;
}

static publicGetPrivateField() {
return Cl.#privateFieldValue;
}

static publicSetPrivateField(newValue) {
Cl.#privateFieldValue = newValue;
}

static get publicFieldValue() {
return Cl.publicField;
}

static set publicFieldValue(newValue) {
Cl.publicField = newValue;
}

static testUpdates() {
Cl.#privateField = 0;
Cl.publicField = 0;
Cl.#privateFieldValue = Cl.#privateFieldValue++;
Cl.publicFieldValue = Cl.publicFieldValue++;

++Cl.#privateFieldValue;
++Cl.publicFieldValue;

Cl.#privateFieldValue += 1;
Cl.publicFieldValue += 1;

Cl.#privateFieldValue = -(Cl.#privateFieldValue ** Cl.#privateFieldValue);
Cl.publicFieldValue = -(Cl.publicFieldValue ** Cl.publicFieldValue);
}
}

0 comments on commit 14f480d

Please sign in to comment.