Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve this for super.* template tags #15043

Merged
merged 1 commit into from Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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