-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
00bdf18
commit afcb5a8
Showing
180 changed files
with
3,769 additions
and
103 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* @minVersion 7.1.5 */ | ||
|
||
// https://tc39.es/ecma262/#sec-toprimitive | ||
export default function toPrimitive( | ||
input: unknown, | ||
hint: "default" | "string" | "number" | void, | ||
) { | ||
if (typeof input !== "object" || !input) return input; | ||
// @ts-expect-error Symbol.toPrimitive might not index {} | ||
var prim = input[Symbol.toPrimitive]; | ||
if (prim !== undefined) { | ||
var res = prim.call(input, hint || "default"); | ||
if (typeof res !== "object") return res; | ||
throw new TypeError("@@toPrimitive must return a primitive value."); | ||
} | ||
return (hint === "string" ? String : Number)(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* @minVersion 7.1.5 */ | ||
|
||
// https://tc39.es/ecma262/#sec-topropertykey | ||
|
||
// @ts-expect-error helper | ||
import toPrimitive from "toPrimitive"; | ||
|
||
export default function toPropertyKey(arg: unknown) { | ||
var key = toPrimitive(arg, "string"); | ||
return typeof key === "symbol" ? key : String(key); | ||
} |
4 changes: 2 additions & 2 deletions
4
...es/babel-helpers/test/fixtures/misc/declaration-name-conflict-helper-entrypoint/output.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
...lugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/context-name/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const logs = []; | ||
const dec = (value, context) => { logs.push(context.name) }; | ||
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; }; | ||
class Foo { | ||
@dec static accessor a; | ||
@dec static accessor #a; | ||
|
||
@dec static accessor "b" | ||
@dec static accessor ["c"]; | ||
|
||
@dec static accessor 0; | ||
@dec static accessor [1]; | ||
|
||
@dec static accessor 2n; | ||
@dec static accessor [3n]; | ||
|
||
@dec static accessor [f()]; | ||
} | ||
|
||
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); |
20 changes: 20 additions & 0 deletions
20
...ugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/context-name/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const logs = []; | ||
const dec = (value, context) => { logs.push(context.name) }; | ||
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; }; | ||
class Foo { | ||
@dec static accessor a; | ||
@dec static accessor #a; | ||
|
||
@dec static accessor "b" | ||
@dec static accessor ["c"]; | ||
|
||
@dec static accessor 0; | ||
@dec static accessor [1]; | ||
|
||
@dec static accessor 2n; | ||
@dec static accessor [3n]; | ||
|
||
@dec static accessor [f()]; | ||
} | ||
|
||
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); |
3 changes: 3 additions & 0 deletions
3
...-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/context-name/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"minNodeVersion": "14.6.0" | ||
} |
124 changes: 124 additions & 0 deletions
124
...gin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/context-name/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
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; | ||
const logs = []; | ||
const dec = (value, context) => { | ||
logs.push(context.name); | ||
}; | ||
const f = () => { | ||
logs.push("computing f"); | ||
return { | ||
[Symbol.toPrimitive]: () => "f()" | ||
}; | ||
}; | ||
_computedKey = "c"; | ||
_computedKey2 = 1; | ||
_computedKey3 = 3n; | ||
_computedKey4 = f(); | ||
var _a = /*#__PURE__*/new WeakMap(); | ||
class Foo { | ||
constructor() { | ||
babelHelpers.classPrivateFieldInitSpec(this, _a, { | ||
get: _get_a2, | ||
set: _set_a2 | ||
}); | ||
} | ||
static get a() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); | ||
} | ||
static set a(v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); | ||
} | ||
static get "b"() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C); | ||
} | ||
static set "b"(v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v); | ||
} | ||
static get [_computedKey]() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _D); | ||
} | ||
static set [_computedKey](v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _D, v); | ||
} | ||
static get 0() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _E); | ||
} | ||
static set 0(v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _E, v); | ||
} | ||
static get [_computedKey2]() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _F); | ||
} | ||
static set [_computedKey2](v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _F, v); | ||
} | ||
static get 2n() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _G); | ||
} | ||
static set 2n(v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _G, v); | ||
} | ||
static get [_computedKey3]() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _H); | ||
} | ||
static set [_computedKey3](v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _H, v); | ||
} | ||
static get [_computedKey4]() { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _I); | ||
} | ||
static set [_computedKey4](v) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _I, v); | ||
} | ||
} | ||
_class = Foo; | ||
function _set_a2(v) { | ||
_set_a(this, v); | ||
} | ||
function _get_a2() { | ||
return _get_a(this); | ||
} | ||
(() => { | ||
[_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 () { | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, _class, _B); | ||
}, function (value) { | ||
babelHelpers.classStaticPrivateFieldSpecSet(this, _class, _B, value); | ||
}], [dec, 6, "b"], [dec, 6, _computedKey], [dec, 6, 0], [dec, 6, _computedKey2], [dec, 6, 2n], [dec, 6, _computedKey3], [dec, 6, _computedKey4]], []); | ||
_initStatic(_class); | ||
})(); | ||
var _A = { | ||
writable: true, | ||
value: _init_a(_class) | ||
}; | ||
var _B = { | ||
writable: true, | ||
value: _init_a2(_class) | ||
}; | ||
var _C = { | ||
writable: true, | ||
value: _init_computedKey(_class) | ||
}; | ||
var _D = { | ||
writable: true, | ||
value: _init_computedKey2(_class) | ||
}; | ||
var _E = { | ||
writable: true, | ||
value: _init_computedKey3(_class) | ||
}; | ||
var _F = { | ||
writable: true, | ||
value: _init_computedKey4(_class) | ||
}; | ||
var _G = { | ||
writable: true, | ||
value: _init_computedKey5(_class) | ||
}; | ||
var _H = { | ||
writable: true, | ||
value: _init_computedKey6(_class) | ||
}; | ||
var _I = { | ||
writable: true, | ||
value: _init_computedKey7(_class) | ||
}; | ||
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); |
20 changes: 20 additions & 0 deletions
20
...ges/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/context-name/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const logs = []; | ||
const dec = (value, context) => { logs.push(context.name) }; | ||
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; }; | ||
class Foo { | ||
@dec static accessor a; | ||
@dec static accessor #a; | ||
|
||
@dec static accessor "b" | ||
@dec static accessor ["c"]; | ||
|
||
@dec static accessor 0; | ||
@dec static accessor [1]; | ||
|
||
@dec static accessor 2n; | ||
@dec static accessor [3n]; | ||
|
||
@dec static accessor [f()]; | ||
} | ||
|
||
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); |
20 changes: 20 additions & 0 deletions
20
...es/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/context-name/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const logs = []; | ||
const dec = (value, context) => { logs.push(context.name) }; | ||
const f = () => { logs.push("computing f"); return { [Symbol.toPrimitive]: () => "f()" }; }; | ||
class Foo { | ||
@dec static accessor a; | ||
@dec static accessor #a; | ||
|
||
@dec static accessor "b" | ||
@dec static accessor ["c"]; | ||
|
||
@dec static accessor 0; | ||
@dec static accessor [1]; | ||
|
||
@dec static accessor 2n; | ||
@dec static accessor [3n]; | ||
|
||
@dec static accessor [f()]; | ||
} | ||
|
||
expect(logs).toStrictEqual(["computing f", "a", "#a", "b", "c", "0", "1", "2", "3", "f()"]); |
3 changes: 3 additions & 0 deletions
3
...abel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/context-name/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"minNodeVersion": "16.11.0" | ||
} |
Oops, something went wrong.