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(commonjs): preserve this reference in the child class #1537

Merged
merged 4 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export function getAugmentedNamespace(n) {
if (typeof f == "function") {
var a = function a () {
if (this instanceof a) {
var args = [null];
args.push.apply(args, arguments);
var Ctor = Function.bind.apply(f, args);
return new Ctor();
return Reflect.construct(f, [...arguments], this.constructor);
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved
}
return f.apply(this, arguments);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function Dep(){};
export default Dep;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Dep = require('./dep');

class Main extends Dep {
constructor() {
super();
this.name = this.constructor.name;
}
static name = "main";
}

t.is(new Main().name, "main");
134 changes: 74 additions & 60 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -3543,10 +3543,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -3630,10 +3627,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -3711,10 +3705,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -3822,10 +3813,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -3915,10 +3903,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -4104,10 +4089,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -4341,10 +4323,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -4444,10 +4423,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -4638,10 +4614,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -4917,10 +4890,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -5012,10 +4982,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -5899,6 +5866,65 @@ Generated by [AVA](https://avajs.dev).
`,
}

## preserv-this-reference-in-the-child-class

> Snapshot 1

{
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
} else a = {};␊
Object.defineProperty(a, '__esModule', {value: true});␊
Object.keys(n).forEach(function (k) {␊
var d = Object.getOwnPropertyDescriptor(n, k);␊
Object.defineProperty(a, k, d.get ? d : {␊
enumerable: true,␊
get: function () {␊
return n[k];␊
}␊
});␊
});␊
return a;␊
}␊
var main = {};␊
function Dep$1(){}␊
var dep = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
default: Dep$1␊
});␊
var require$$0 = /*@__PURE__*/getAugmentedNamespace(dep);␊
const Dep = require$$0;␊
class Main extends Dep {␊
constructor() {␊
super();␊
this.name = this.constructor.name;␊
}␊
static name = "main";␊
}␊
t.is(new Main().name, "main");␊
module.exports = main;␊
`,
}

## preserve-modules

> Snapshot 1
Expand Down Expand Up @@ -6370,10 +6396,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -6422,10 +6445,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -7435,10 +7455,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -7488,10 +7505,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
15 changes: 3 additions & 12 deletions packages/commonjs/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -199,10 +196,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down Expand Up @@ -294,10 +288,7 @@ Generated by [AVA](https://avajs.dev).
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
return Reflect.construct(f, [...arguments], this.constructor);␊
}␊
return f.apply(this, arguments);␊
};␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/test.js.snap
Binary file not shown.
Loading
Loading