Skip to content

Commit afcb5a8

Browse files
Apply toPropertyKey on decorator context name (#16139)
* refactor: extract toPrimitive and toProeprtyKey to single helpers * update test fixtures * apply toPropertyKey for decorator context name * fix: allow BigIntLiteral in ClassMethod.key * Add 2021-12 test cases * copy paste to other versions * Suppress TS error * Update packages/babel-helpers/src/helpers/toPrimitive.ts Co-authored-by: liuxingbaoyu <[email protected]> * update generated helpers * update test fixtures --------- Co-authored-by: liuxingbaoyu <[email protected]>
1 parent 00bdf18 commit afcb5a8

File tree

180 files changed

+3769
-103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+3769
-103
lines changed

packages/babel-helpers/src/helpers-generated.ts

+12-4
Large diffs are not rendered by default.

packages/babel-helpers/src/helpers.ts

-25
Original file line numberDiff line numberDiff line change
@@ -865,31 +865,6 @@ helpers.skipFirstGeneratorNext = helper("7.0.0-beta.0")`
865865
}
866866
`;
867867

868-
helpers.toPrimitive = helper("7.1.5")`
869-
export default function _toPrimitive(
870-
input,
871-
hint /*: "default" | "string" | "number" | void */
872-
) {
873-
if (typeof input !== "object" || input === null) return input;
874-
var prim = input[Symbol.toPrimitive];
875-
if (prim !== undefined) {
876-
var res = prim.call(input, hint || "default");
877-
if (typeof res !== "object") return res;
878-
throw new TypeError("@@toPrimitive must return a primitive value.");
879-
}
880-
return (hint === "string" ? String : Number)(input);
881-
}
882-
`;
883-
884-
helpers.toPropertyKey = helper("7.1.5")`
885-
import toPrimitive from "toPrimitive";
886-
887-
export default function _toPropertyKey(arg) {
888-
var key = toPrimitive(arg, "string");
889-
return typeof key === "symbol" ? key : String(key);
890-
}
891-
`;
892-
893868
/**
894869
* Add a helper that will throw a useful error if the transform fails to detect the class
895870
* property assignment, so users know something failed.

packages/babel-helpers/src/helpers/applyDecs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @minVersion 7.17.8 */
22

3+
import toPropertyKey from "toPropertyKey";
34
/**
45
* NOTE: This is an old version of the helper, used for 2021-12 decorators.
56
* Updates should be done in applyDecs2203R.js.
@@ -168,7 +169,7 @@ function old_memberDec(
168169

169170
var ctx = {
170171
kind: kindStr,
171-
name: isPrivate ? "#" + name : name,
172+
name: isPrivate ? "#" + name : toPropertyKey(name),
172173
isStatic: isStatic,
173174
isPrivate: isPrivate,
174175
};

packages/babel-helpers/src/helpers/applyDecs2203R.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* @minVersion 7.20.0 */
22

3+
import toPropertyKey from "toPropertyKey";
4+
35
/**
46
Enums are used in this file, but not assigned to vars to avoid non-hoistable values
57
@@ -58,7 +60,7 @@ function applyDecs2203RFactory() {
5860

5961
var ctx = {
6062
kind: kindStr,
61-
name: isPrivate ? "#" + name : name,
63+
name: isPrivate ? "#" + name : toPropertyKey(name),
6264
static: isStatic,
6365
private: isPrivate,
6466
};

packages/babel-helpers/src/helpers/applyDecs2301.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @minVersion 7.21.0 */
22

33
import checkInRHS from "checkInRHS";
4+
import toPropertyKey from "toPropertyKey";
45

56
/**
67
Enums are used in this file, but not assigned to vars to avoid non-hoistable values
@@ -69,7 +70,7 @@ function applyDecs2301Factory() {
6970

7071
var ctx = {
7172
kind: kindStr,
72-
name: isPrivate ? "#" + name : name,
73+
name: isPrivate ? "#" + name : toPropertyKey(name),
7374
static: isStatic,
7475
private: isPrivate,
7576
};

packages/babel-helpers/src/helpers/applyDecs2305.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// @ts-expect-error helper
44
import checkInRHS from "checkInRHS";
5+
// @ts-expect-error helper
6+
import toPropertyKey from "toPropertyKey";
57

68
/**
79
Enums are used in this file, but not assigned to vars to avoid non-hoistable values
@@ -94,7 +96,7 @@ function memberDec(
9496
kind
9597
] as any,
9698

97-
name: isPrivate ? "#" + name : name,
99+
name: isPrivate ? "#" + name : toPropertyKey(name),
98100
static: isStatic,
99101
private: isPrivate,
100102
metadata: metadata,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* @minVersion 7.1.5 */
2+
3+
// https://tc39.es/ecma262/#sec-toprimitive
4+
export default function toPrimitive(
5+
input: unknown,
6+
hint: "default" | "string" | "number" | void,
7+
) {
8+
if (typeof input !== "object" || !input) return input;
9+
// @ts-expect-error Symbol.toPrimitive might not index {}
10+
var prim = input[Symbol.toPrimitive];
11+
if (prim !== undefined) {
12+
var res = prim.call(input, hint || "default");
13+
if (typeof res !== "object") return res;
14+
throw new TypeError("@@toPrimitive must return a primitive value.");
15+
}
16+
return (hint === "string" ? String : Number)(input);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* @minVersion 7.1.5 */
2+
3+
// https://tc39.es/ecma262/#sec-topropertykey
4+
5+
// @ts-expect-error helper
6+
import toPrimitive from "toPrimitive";
7+
8+
export default function toPropertyKey(arg: unknown) {
9+
var key = toPrimitive(arg, "string");
10+
return typeof key === "symbol" ? key : String(key);
11+
}

packages/babel-helpers/test/fixtures/misc/declaration-name-conflict-helper-entrypoint/output.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const logs = [];
2+
const dec = (value, context) => { logs.push(context.name) };
3+
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; };
4+
class Foo {
5+
@dec static accessor a;
6+
@dec static accessor #a;
7+
8+
@dec static accessor "b"
9+
@dec static accessor ["c"];
10+
11+
@dec static accessor 0;
12+
@dec static accessor [1];
13+
14+
@dec static accessor 2n;
15+
@dec static accessor [3n];
16+
17+
@dec static accessor [f()];
18+
}
19+
20+
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const logs = [];
2+
const dec = (value, context) => { logs.push(context.name) };
3+
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; };
4+
class Foo {
5+
@dec static accessor a;
6+
@dec static accessor #a;
7+
8+
@dec static accessor "b"
9+
@dec static accessor ["c"];
10+
11+
@dec static accessor 0;
12+
@dec static accessor [1];
13+
14+
@dec static accessor 2n;
15+
@dec static accessor [3n];
16+
17+
@dec static accessor [f()];
18+
}
19+
20+
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"minNodeVersion": "14.6.0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var _init_a, _init_a2, _get_a, _set_a, _init_computedKey, _computedKey, _init_computedKey2, _init_computedKey3, _computedKey2, _init_computedKey4, _init_computedKey5, _computedKey3, _init_computedKey6, _computedKey4, _init_computedKey7, _initStatic, _class;
2+
const logs = [];
3+
const dec = (value, context) => {
4+
logs.push(context.name);
5+
};
6+
const f = () => {
7+
logs.push("computing f");
8+
return {
9+
[Symbol.toPrimitive]: () => "f()"
10+
};
11+
};
12+
_computedKey = "c";
13+
_computedKey2 = 1;
14+
_computedKey3 = 3n;
15+
_computedKey4 = f();
16+
var _a = /*#__PURE__*/new WeakMap();
17+
class Foo {
18+
constructor() {
19+
babelHelpers.classPrivateFieldInitSpec(this, _a, {
20+
get: _get_a2,
21+
set: _set_a2
22+
});
23+
}
24+
static get a() {
25+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A);
26+
}
27+
static set a(v) {
28+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v);
29+
}
30+
static get "b"() {
31+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C);
32+
}
33+
static set "b"(v) {
34+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v);
35+
}
36+
static get [_computedKey]() {
37+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _D);
38+
}
39+
static set [_computedKey](v) {
40+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _D, v);
41+
}
42+
static get 0() {
43+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _E);
44+
}
45+
static set 0(v) {
46+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _E, v);
47+
}
48+
static get [_computedKey2]() {
49+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _F);
50+
}
51+
static set [_computedKey2](v) {
52+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _F, v);
53+
}
54+
static get 2n() {
55+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _G);
56+
}
57+
static set 2n(v) {
58+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _G, v);
59+
}
60+
static get [_computedKey3]() {
61+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _H);
62+
}
63+
static set [_computedKey3](v) {
64+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _H, v);
65+
}
66+
static get [_computedKey4]() {
67+
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _I);
68+
}
69+
static set [_computedKey4](v) {
70+
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _I, v);
71+
}
72+
}
73+
_class = Foo;
74+
function _set_a2(v) {
75+
_set_a(this, v);
76+
}
77+
function _get_a2() {
78+
return _get_a(this);
79+
}
80+
(() => {
81+
[_init_a, _init_a2, _get_a, _set_a, _init_computedKey, _init_computedKey2, _init_computedKey3, _init_computedKey4, _init_computedKey5, _init_computedKey6, _init_computedKey7, _initStatic] = babelHelpers.applyDecs(_class, [[dec, 6, "a"], [dec, 6, "a", function () {
82+
return babelHelpers.classStaticPrivateFieldSpecGet(this, _class, _B);
83+
}, function (value) {
84+
babelHelpers.classStaticPrivateFieldSpecSet(this, _class, _B, value);
85+
}], [dec, 6, "b"], [dec, 6, _computedKey], [dec, 6, 0], [dec, 6, _computedKey2], [dec, 6, 2n], [dec, 6, _computedKey3], [dec, 6, _computedKey4]], []);
86+
_initStatic(_class);
87+
})();
88+
var _A = {
89+
writable: true,
90+
value: _init_a(_class)
91+
};
92+
var _B = {
93+
writable: true,
94+
value: _init_a2(_class)
95+
};
96+
var _C = {
97+
writable: true,
98+
value: _init_computedKey(_class)
99+
};
100+
var _D = {
101+
writable: true,
102+
value: _init_computedKey2(_class)
103+
};
104+
var _E = {
105+
writable: true,
106+
value: _init_computedKey3(_class)
107+
};
108+
var _F = {
109+
writable: true,
110+
value: _init_computedKey4(_class)
111+
};
112+
var _G = {
113+
writable: true,
114+
value: _init_computedKey5(_class)
115+
};
116+
var _H = {
117+
writable: true,
118+
value: _init_computedKey6(_class)
119+
};
120+
var _I = {
121+
writable: true,
122+
value: _init_computedKey7(_class)
123+
};
124+
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const logs = [];
2+
const dec = (value, context) => { logs.push(context.name) };
3+
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; };
4+
class Foo {
5+
@dec static accessor a;
6+
@dec static accessor #a;
7+
8+
@dec static accessor "b"
9+
@dec static accessor ["c"];
10+
11+
@dec static accessor 0;
12+
@dec static accessor [1];
13+
14+
@dec static accessor 2n;
15+
@dec static accessor [3n];
16+
17+
@dec static accessor [f()];
18+
}
19+
20+
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const logs = [];
2+
const dec = (value, context) => { logs.push(context.name) };
3+
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; };
4+
class Foo {
5+
@dec static accessor a;
6+
@dec static accessor #a;
7+
8+
@dec static accessor "b"
9+
@dec static accessor ["c"];
10+
11+
@dec static accessor 0;
12+
@dec static accessor [1];
13+
14+
@dec static accessor 2n;
15+
@dec static accessor [3n];
16+
17+
@dec static accessor [f()];
18+
}
19+
20+
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"minNodeVersion": "16.11.0"
3+
}

0 commit comments

Comments
 (0)