Skip to content

Commit

Permalink
Set function name for decorated private non-field elements (#16144)
Browse files Browse the repository at this point in the history
* introduce setFunctionName helper

* call setFunctionName for decorated private functions

* Add test cases

* Update packages/babel-helpers/src/helpers/setFunctionName.ts

Co-authored-by: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com>

* update generated helpers

* make eslint happy

---------

Co-authored-by: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com>
  • Loading branch information
JLHwung and liuxingbaoyu committed Nov 30, 2023
1 parent 57535c3 commit f3d70d5
Show file tree
Hide file tree
Showing 91 changed files with 206 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Makefile.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Makefile.source.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,14 @@ target["bootstrap-flow"] = function () {

target["new-version-checklist"] = function () {
// eslint-disable-next-line no-constant-condition
if (0) {
if (1) {
console.log(
`
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!! !!!!!!
- packages/babel-helpers/src/helpers/setFunctionName.ts
Bump minVersion
!!!!!! Write any important message here, and change the !!!!!!
!!!!!! if (0) above to if (1) !!!!!!
!!!!!! !!!!!!
Expand Down
12 changes: 8 additions & 4 deletions packages/babel-helpers/src/helpers-generated.ts

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion packages/babel-helpers/src/helpers/applyDecs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @minVersion 7.17.8 */

import setFunctionName from "setFunctionName";
import toPropertyKey from "toPropertyKey";
/**
* NOTE: This is an old version of the helper, used for 2021-12 decorators.
Expand Down Expand Up @@ -316,27 +317,36 @@ function old_applyMemberDec(
) {
var decs = decInfo[0];

var desc, initializer, value;
var desc, initializer, prefix, value;

if (isPrivate) {
if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
desc = {
get: decInfo[3],
set: decInfo[4],
};
prefix = "get";
} else if (kind === 3 /* GETTER */) {
desc = {
get: decInfo[3],
};
prefix = "get";
} else if (kind === 4 /* SETTER */) {
desc = {
set: decInfo[3],
};
prefix = "set";
} else {
desc = {
value: decInfo[3],
};
}
if (kind !== 0 /* FIELD */) {
if (kind === 1 /* ACCESSOR */) {
setFunctionName(decInfo[4], "#" + name, "set");
}
setFunctionName(decInfo[3], "#" + name, prefix);
}
} else if (kind !== 0 /* FIELD */) {
desc = Object.getOwnPropertyDescriptor(base, name);
}
Expand Down
12 changes: 11 additions & 1 deletion packages/babel-helpers/src/helpers/applyDecs2203R.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @minVersion 7.20.0 */

import setFunctionName from "setFunctionName";
import toPropertyKey from "toPropertyKey";

/**
Expand Down Expand Up @@ -174,27 +175,36 @@ function applyDecs2203RFactory() {
) {
var decs = decInfo[0];

var desc, init, value;
var desc, init, prefix, value;

if (isPrivate) {
if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
desc = {
get: decInfo[3],
set: decInfo[4],
};
prefix = "get";
} else if (kind === 3 /* GETTER */) {
desc = {
get: decInfo[3],
};
prefix = "get";
} else if (kind === 4 /* SETTER */) {
desc = {
set: decInfo[3],
};
prefix = "set";
} else {
desc = {
value: decInfo[3],
};
}
if (kind !== 0 /* FIELD */) {
if (kind === 1 /* ACCESSOR */) {
setFunctionName(decInfo[4], "#" + name, "set");
}
setFunctionName(decInfo[3], "#" + name, prefix);
}
} else if (kind !== 0 /* FIELD */) {
desc = Object.getOwnPropertyDescriptor(base, name);
}
Expand Down
12 changes: 11 additions & 1 deletion packages/babel-helpers/src/helpers/applyDecs2301.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @minVersion 7.21.0 */

import checkInRHS from "checkInRHS";
import setFunctionName from "setFunctionName";
import toPropertyKey from "toPropertyKey";

/**
Expand Down Expand Up @@ -218,29 +219,38 @@ function applyDecs2301Factory() {
) {
var decs = decInfo[0];

var desc, init, value;
var desc, init, prefix, value;

if (isPrivate) {
if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
desc = {
get: curryThis1(decInfo[3]),
set: curryThis2(decInfo[4]),
};
prefix = "get";
} else {
if (kind === 3 /* GETTER */) {
desc = {
get: decInfo[3],
};
prefix = "get";
} else if (kind === 4 /* SETTER */) {
desc = {
set: decInfo[3],
};
prefix = "set";
} else {
desc = {
value: decInfo[3],
};
}
}
if (kind !== 0 /* FIELD */) {
if (kind === 1 /* ACCESSOR */) {
setFunctionName(desc.set, "#" + name, "set");
}
setFunctionName(desc[prefix || "value"], "#" + name, prefix);
}
} else if (kind !== 0 /* FIELD */) {
desc = Object.getOwnPropertyDescriptor(base, name);
}
Expand Down
16 changes: 15 additions & 1 deletion packages/babel-helpers/src/helpers/applyDecs2305.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// @ts-expect-error helper
import checkInRHS from "checkInRHS";
// @ts-expect-error helper
import setFunctionName from "setFunctionName";
// @ts-expect-error helper
import toPropertyKey from "toPropertyKey";

/**
Expand Down Expand Up @@ -205,7 +207,10 @@ function applyMemberDec(
decs = [decs];
}

var desc: PropertyDescriptor, init, value: any;
var desc: PropertyDescriptor,
init,
prefix: "get" | "set" | undefined,
value: any;

if (isPrivate) {
if (kind === PROP_KIND.FIELD || kind === PROP_KIND.ACCESSOR) {
Expand All @@ -217,21 +222,30 @@ function applyMemberDec(
decInfo[4](this, value);
},
};
prefix = "get";
} else {
if (kind === PROP_KIND.GETTER) {
desc = {
get: decVal,
};
prefix = "get";
} else if (kind === PROP_KIND.SETTER) {
desc = {
set: decVal,
};
prefix = "set";
} else {
desc = {
value: decVal,
};
}
}
if (kind !== PROP_KIND.FIELD) {
if (kind === PROP_KIND.ACCESSOR) {
setFunctionName(desc.set, "#" + name, "set");
}
setFunctionName(desc[prefix || "value"], "#" + name, prefix);
}
} else if (kind !== PROP_KIND.FIELD) {
desc = Object.getOwnPropertyDescriptor(base, name);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/babel-helpers/src/helpers/setFunctionName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* @minVersion 7.23.5 */

// https://tc39.es/ecma262/#sec-setfunctionname
export default function setFunctionName(
fn: Function,
name: symbol | string,
prefix?: string,
) {
if (typeof name === "symbol") {
name = name.description;
name = name ? "[" + (name as string) + "]" : "";
}
// In some older browsers .name was non-configurable, here we catch any
// errors thrown by defineProperty.
try {
Object.defineProperty(fn, "name", {
configurable: true,
value: prefix ? prefix + " " + name : name,
});
} catch (_) {}
return fn;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ function dec({ get, set }, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
expect(set.name).toEqual("set " + context.name);
return {
get() {
return get.call(this) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ function dec({ get, set }, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
expect(set.name).toEqual("set " + context.name);
return {
get() {
return get.call(this) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ function dec({ get, set }, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
expect(set.name).toEqual("set " + context.name);
return {
get() {
return get.call(this) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ function dec({ get, set }, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
expect(set.name).toEqual("set " + context.name);
return {
get() {
return get.call(this) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(get, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
return function () {
return get.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(get, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
return function () {
return get.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(get, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
return function () {
return get.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(get, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
return function () {
return get.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(fn, context) {
this[context.name + 'Context'] = context;
});

expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function dec(fn, context) {
expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(fn, context) {
this[context.name + 'Context'] = context;
});

expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function dec(fn, context) {
expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(fn, context) {
this[context.name + 'Context'] = context;
});

expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function dec(fn, context) {
expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(fn, context) {
this[context.name + 'Context'] = context;
});

expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function dec(fn, context) {
expect(fn.name).toEqual(context.name);
return function () {
return fn.call(this) + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(set, context) {
this[context.name + 'Context'] = context;
});

expect(set.name).toEqual("set " + context.name);
return function (v) {
return set.call(this, v + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(set, context) {
this[context.name + 'Context'] = context;
});

expect(set.name).toEqual("set " + context.name);
return function (v) {
return set.call(this, v + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(set, context) {
this[context.name + 'Context'] = context;
});

expect(set.name).toEqual("set " + context.name);
return function (v) {
return set.call(this, v + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function dec(set, context) {
this[context.name + 'Context'] = context;
});

expect(set.name).toEqual("set " + context.name);
return function (v) {
return set.call(this, v + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ function dec({ get, set }, context) {
this[context.name + 'Context'] = context;
});

expect(get.name).toEqual("get " + context.name);
expect(set.name).toEqual("set " + context.name);
return {
get() {
return get.call(this) + 1;
Expand Down

0 comments on commit f3d70d5

Please sign in to comment.