Skip to content

Commit

Permalink
Expose .access for public class elements
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 8, 2022
1 parent 7bbb3f9 commit 5a16f8c
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts

Large diffs are not rendered by default.

58 changes: 33 additions & 25 deletions packages/babel-helpers/src/helpers/applyDecs2203.js
Expand Up @@ -182,36 +182,44 @@ function memberDec(
if (isPrivate) {
metadataKind = 2 /* PRIVATE */;
metadataName = Symbol(name);
} else {
metadataKind = 1 /* PUBLIC */;
metadataName = name;
}

var access = {};

if (kind === 0 /* FIELD */) {
access.get = desc.get;
access.set = desc.set;
} else if (kind === 2 /* METHOD */) {
access.get = function () {
return desc.value;
};
var get, set;
if (kind === 0 /* FIELD */) {
if (isPrivate) {
get = desc.get;
set = desc.set;
} else {
// replace with values that will go through the final getter and setter
if (kind === 1 /* ACCESSOR */ || kind === 3 /* GETTER */) {
access.get = function () {
return desc.get.call(this);
};
}

if (kind === 1 /* ACCESSOR */ || kind === 4 /* SETTER */) {
access.set = function (v) {
desc.set.call(this, v);
};
}
get = function () {
return this[name];
};
set = function (v) {
this[name] = v;
};
}

ctx.access = access;
} else if (kind === 2 /* METHOD */) {
get = function () {
return desc.value;
};
} else {
metadataKind = 1 /* PUBLIC */;
metadataName = name;
// replace with values that will go through the final getter and setter
if (kind === 1 /* ACCESSOR */ || kind === 3 /* GETTER */) {
get = function () {
return desc.get.call(this);
};
}

if (kind === 1 /* ACCESSOR */ || kind === 4 /* SETTER */) {
set = function (v) {
desc.set.call(this, v);
};
}
}
ctx.access =
get && set ? { get: get, set: set } : get ? { get: get } : { set: set };

try {
return dec(
Expand Down
Expand Up @@ -36,8 +36,13 @@ const bContext = foo['bContext'];
const cContext = foo['cContext'];

expect(foo.a).toBe(2);
expect(aContext.access.get.call(foo)).toBe(2);
foo.a = 123;
expect(foo.a).toBe(125);
expect(aContext.access.get.call(foo)).toBe(125);
aContext.access.set.call(foo, 456);
expect(foo.a).toBe(458);
expect(aContext.access.get.call(foo)).toBe(458);
expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('accessor');
expect(aContext.static).toBe(false);
Expand Down
Expand Up @@ -34,8 +34,13 @@ const bContext = Foo['bContext'];
const cContext = Foo['cContext'];

expect(Foo.a).toBe(2);
expect(aContext.access.get.call(Foo)).toBe(2);
Foo.a = 123;
expect(Foo.a).toBe(125);
expect(aContext.access.get.call(Foo)).toBe(125);
aContext.access.set.call(Foo, 456);
expect(Foo.a).toBe(458);
expect(aContext.access.get.call(Foo)).toBe(458);
expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('accessor');
expect(aContext.static).toBe(true);
Expand Down
Expand Up @@ -23,8 +23,13 @@ const bContext = foo['bContext'];
const cContext = foo['cContext'];

expect(foo.a).toBe(2);
expect(aContext.access.get.call(foo)).toBe(2);
foo.a = 123;
expect(foo.a).toBe(123);
expect(aContext.access.get.call(foo)).toBe(123);
aContext.access.set.call(foo, 456);
expect(foo.a).toBe(456);
expect(aContext.access.get.call(foo)).toBe(456);
expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('field');
expect(aContext.static).toBe(false);
Expand Down
Expand Up @@ -21,8 +21,13 @@ const bContext = Foo['bContext'];
const cContext = Foo['cContext'];

expect(Foo.a).toBe(2);
expect(aContext.access.get.call(Foo)).toBe(2);
Foo.a = 123;
expect(Foo.a).toBe(123);
expect(aContext.access.get.call(Foo)).toBe(123);
aContext.access.set.call(Foo, 456);
expect(Foo.a).toBe(456);
expect(aContext.access.get.call(Foo)).toBe(456);
expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('field');
expect(aContext.static).toBe(true);
Expand Down
Expand Up @@ -29,9 +29,13 @@ const bContext = foo['bContext'];

expect(foo.a).toBe(2);
expect(foo.b).toBe(2);
expect(aContext.access.get.call(foo)).toBe(2);
expect(bContext.access.get.call(foo)).toBe(2);
foo.value = 123;
expect(foo.a).toBe(124);
expect(foo.b).toBe(124);
expect(aContext.access.get.call(foo)).toBe(124);
expect(bContext.access.get.call(foo)).toBe(124);

expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('getter');
Expand Down
Expand Up @@ -27,9 +27,13 @@ const bContext = Foo['bContext'];

expect(Foo.a).toBe(2);
expect(Foo.b).toBe(2);
expect(aContext.access.get.call(Foo)).toBe(2);
expect(bContext.access.get.call(Foo)).toBe(2);
Foo.value = 123;
expect(Foo.a).toBe(124);
expect(Foo.b).toBe(124);
expect(aContext.access.get.call(Foo)).toBe(124);
expect(bContext.access.get.call(Foo)).toBe(124);

expect(aContext.name).toBe('a');
expect(aContext.kind).toBe('getter');
Expand Down
Expand Up @@ -48,12 +48,23 @@ const b_setterContext = foo['b_setterContext'];

expect(foo.a).toBe(2);
expect(foo.b).toBe(2);
expect(a_getterContext.access.get.call(foo)).toBe(2);
expect(b_getterContext.access.get.call(foo)).toBe(2);
foo.a = 123;
expect(foo.a).toBe(125);
expect(foo.b).toBe(125);
expect(a_getterContext.access.get.call(foo)).toBe(125);
expect(b_getterContext.access.get.call(foo)).toBe(125);
foo.b = 456;
expect(foo.a).toBe(458);
expect(foo.b).toBe(458);
expect(a_getterContext.access.get.call(foo)).toBe(458);
expect(b_getterContext.access.get.call(foo)).toBe(458);
a_setterContext.access.set.call(foo, 789);
expect(foo.a).toBe(791);
expect(foo.b).toBe(791);
expect(a_getterContext.access.get.call(foo)).toBe(791);
expect(b_getterContext.access.get.call(foo)).toBe(791);

expect(a_getterContext.name).toBe('a');
expect(a_getterContext.kind).toBe('getter');
Expand Down
Expand Up @@ -46,12 +46,23 @@ const b_setterContext = Foo['b_setterContext'];

expect(Foo.a).toBe(2);
expect(Foo.b).toBe(2);
expect(a_getterContext.access.get.call(Foo)).toBe(2);
expect(b_getterContext.access.get.call(Foo)).toBe(2);
Foo.a = 123;
expect(Foo.a).toBe(125);
expect(Foo.b).toBe(125);
expect(a_getterContext.access.get.call(Foo)).toBe(125);
expect(b_getterContext.access.get.call(Foo)).toBe(125);
Foo.b = 456;
expect(Foo.a).toBe(458);
expect(Foo.b).toBe(458);
expect(a_getterContext.access.get.call(Foo)).toBe(458);
expect(b_getterContext.access.get.call(Foo)).toBe(458);
a_setterContext.access.set.call(Foo, 789);
expect(Foo.a).toBe(791);
expect(Foo.b).toBe(791);
expect(a_getterContext.access.get.call(Foo)).toBe(791);
expect(b_getterContext.access.get.call(Foo)).toBe(791);

expect(a_getterContext.name).toBe('a');
expect(a_getterContext.kind).toBe('getter');
Expand Down
Expand Up @@ -28,9 +28,13 @@ const aContext = foo['aContext'];
const bContext = foo['bContext'];

expect(foo.a()).toBe(2);
expect(aContext.access.get.call(foo).call(foo)).toBe(2);
expect(foo.b()).toBe(2);
expect(bContext.access.get.call(foo).call(foo)).toBe(2);
foo.value = 123;
expect(aContext.access.get.call(foo).call(foo)).toBe(124);
expect(foo.a()).toBe(124);
expect(bContext.access.get.call(foo).call(foo)).toBe(124);
expect(foo.b()).toBe(124);

expect(aContext.name).toBe('a');
Expand Down
Expand Up @@ -26,9 +26,13 @@ const aContext = Foo['aContext'];
const bContext = Foo['bContext'];

expect(Foo.a()).toBe(2);
expect(aContext.access.get.call(Foo).call(Foo)).toBe(2);
expect(Foo.b()).toBe(2);
expect(bContext.access.get.call(Foo).call(Foo)).toBe(2);
Foo.value = 123;
expect(aContext.access.get.call(Foo).call(Foo)).toBe(124);
expect(Foo.a()).toBe(124);
expect(bContext.access.get.call(Foo).call(Foo)).toBe(124);
expect(Foo.b()).toBe(124);

expect(aContext.name).toBe('a');
Expand Down
Expand Up @@ -30,7 +30,7 @@ const bContext = foo['bContext'];
expect(foo.value).toBe(1);
foo.a = 123;
expect(foo.value).toBe(124);
foo.a = 456;
aContext.access.set.call(foo, 456);
expect(foo.value).toBe(457);

expect(aContext.name).toBe('a');
Expand Down
Expand Up @@ -29,7 +29,7 @@ const bContext = Foo['bContext'];
expect(Foo.value).toBe(1);
Foo.a = 123;
expect(Foo.value).toBe(124);
Foo.a = 456;
aContext.access.set.call(Foo, 456);
expect(Foo.value).toBe(457);

expect(aContext.name).toBe('a');
Expand Down

0 comments on commit 5a16f8c

Please sign in to comment.