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

Lines changed: 12 additions & 4 deletions
Large diffs are not rendered by default.

packages/babel-helpers/src/helpers.ts

Lines changed: 0 additions & 25 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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,
Lines changed: 17 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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()"]);

0 commit comments

Comments
 (0)