Skip to content

Commit

Permalink
fix: preserve this for super.* template tags (#15043)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Oct 16, 2022
1 parent 0b0f083 commit 34ae281
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
@@ -0,0 +1,13 @@
async function test() {
class Foo { foo() { return this } }
class Bar extends Foo {
a = async () => super.foo``
b = async () => super['foo']``
c = async (foo) => super[foo]``
}
const bar = new Bar
expect(await bar.a()).toEqual(bar);
expect(await bar.b()).toEqual(bar);
expect(await bar.c('foo')).toEqual(bar);
}
test()
@@ -0,0 +1,16 @@
// This should print "true true true"
async function test() {
class Foo { foo() { return this } }
class Bar extends Foo {
a = async () => super.foo``
b = async () => super['foo']``
c = async (foo) => super[foo]``
}
const bar = new Bar
console.log(
(await bar.a()) === bar,
(await bar.b()) === bar,
(await bar.c('foo')) === bar,
)
}
test()
@@ -0,0 +1,4 @@
{
"targets": "chrome 50",
"presets": ["env"]
}
@@ -0,0 +1,40 @@
// This should print "true true true"
function test() {
return _test.apply(this, arguments);
}
function _test() {
_test = babelHelpers.asyncToGenerator(function* () {
class Foo {
foo() {
return this;
}
}
class Bar extends Foo {
constructor(...args) {
var _superprop_getFoo = () => super.foo,
_this,
_superprop_get = _prop => super[_prop];
super(...args);
_this = this;
babelHelpers.defineProperty(this, "a", /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
return _superprop_getFoo().bind(_this)``;
}));
babelHelpers.defineProperty(this, "b", /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
return _superprop_get('foo').bind(_this)``;
}));
babelHelpers.defineProperty(this, "c", /*#__PURE__*/function () {
var _ref3 = babelHelpers.asyncToGenerator(function* (foo) {
return _superprop_get(foo).bind(_this)``;
});
return function (_x) {
return _ref3.apply(this, arguments);
};
}());
}
}
const bar = new Bar();
console.log((yield bar.a()) === bar, (yield bar.b()) === bar, (yield bar.c('foo')) === bar);
});
return _test.apply(this, arguments);
}
test();
13 changes: 13 additions & 0 deletions packages/babel-traverse/src/path/conversion.ts
Expand Up @@ -368,6 +368,9 @@ function hoistFunctionEnvironment(
const isCall = superParentPath.isCallExpression({
callee: superProp.node,
});
const isTaggedTemplate = superParentPath.isTaggedTemplateExpression({
tag: superProp.node,
});
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);

const args: t.Expression[] = [];
Expand All @@ -393,6 +396,16 @@ function hoistFunctionEnvironment(
} else if (isAssignment) {
// Replace not only the super.prop, but the whole assignment
superParentPath.replaceWith(call);
} else if (isTaggedTemplate) {
superProp.replaceWith(
callExpression(memberExpression(call, identifier("bind"), false), [
thisExpression(),
]),
);

thisPaths.push(
superProp.get("arguments.0") as NodePath<t.ThisExpression>,
);
} else {
superProp.replaceWith(call);
}
Expand Down

0 comments on commit 34ae281

Please sign in to comment.