Skip to content

Commit 34ae281

Browse files
authored
fix: preserve this for super.* template tags (#15043)
1 parent 0b0f083 commit 34ae281

5 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
async function test() {
2+
class Foo { foo() { return this } }
3+
class Bar extends Foo {
4+
a = async () => super.foo``
5+
b = async () => super['foo']``
6+
c = async (foo) => super[foo]``
7+
}
8+
const bar = new Bar
9+
expect(await bar.a()).toEqual(bar);
10+
expect(await bar.b()).toEqual(bar);
11+
expect(await bar.c('foo')).toEqual(bar);
12+
}
13+
test()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This should print "true true true"
2+
async function test() {
3+
class Foo { foo() { return this } }
4+
class Bar extends Foo {
5+
a = async () => super.foo``
6+
b = async () => super['foo']``
7+
c = async (foo) => super[foo]``
8+
}
9+
const bar = new Bar
10+
console.log(
11+
(await bar.a()) === bar,
12+
(await bar.b()) === bar,
13+
(await bar.c('foo')) === bar,
14+
)
15+
}
16+
test()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"targets": "chrome 50",
3+
"presets": ["env"]
4+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This should print "true true true"
2+
function test() {
3+
return _test.apply(this, arguments);
4+
}
5+
function _test() {
6+
_test = babelHelpers.asyncToGenerator(function* () {
7+
class Foo {
8+
foo() {
9+
return this;
10+
}
11+
}
12+
class Bar extends Foo {
13+
constructor(...args) {
14+
var _superprop_getFoo = () => super.foo,
15+
_this,
16+
_superprop_get = _prop => super[_prop];
17+
super(...args);
18+
_this = this;
19+
babelHelpers.defineProperty(this, "a", /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
20+
return _superprop_getFoo().bind(_this)``;
21+
}));
22+
babelHelpers.defineProperty(this, "b", /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
23+
return _superprop_get('foo').bind(_this)``;
24+
}));
25+
babelHelpers.defineProperty(this, "c", /*#__PURE__*/function () {
26+
var _ref3 = babelHelpers.asyncToGenerator(function* (foo) {
27+
return _superprop_get(foo).bind(_this)``;
28+
});
29+
return function (_x) {
30+
return _ref3.apply(this, arguments);
31+
};
32+
}());
33+
}
34+
}
35+
const bar = new Bar();
36+
console.log((yield bar.a()) === bar, (yield bar.b()) === bar, (yield bar.c('foo')) === bar);
37+
});
38+
return _test.apply(this, arguments);
39+
}
40+
test();

packages/babel-traverse/src/path/conversion.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ function hoistFunctionEnvironment(
368368
const isCall = superParentPath.isCallExpression({
369369
callee: superProp.node,
370370
});
371+
const isTaggedTemplate = superParentPath.isTaggedTemplateExpression({
372+
tag: superProp.node,
373+
});
371374
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
372375

373376
const args: t.Expression[] = [];
@@ -393,6 +396,16 @@ function hoistFunctionEnvironment(
393396
} else if (isAssignment) {
394397
// Replace not only the super.prop, but the whole assignment
395398
superParentPath.replaceWith(call);
399+
} else if (isTaggedTemplate) {
400+
superProp.replaceWith(
401+
callExpression(memberExpression(call, identifier("bind"), false), [
402+
thisExpression(),
403+
]),
404+
);
405+
406+
thisPaths.push(
407+
superProp.get("arguments.0") as NodePath<t.ThisExpression>,
408+
);
396409
} else {
397410
superProp.replaceWith(call);
398411
}

0 commit comments

Comments
 (0)