Skip to content

Commit

Permalink
fix(commonjs): preserve this reference in the child class (#1537)
Browse files Browse the repository at this point in the history
* fix(commonjs): preserve this reference in the child class

* update snapshots

* tweak

Co-authored-by: Dasa Paddock <[email protected]>

* test: update snapshots

---------

Co-authored-by: Dasa Paddock <[email protected]>
  • Loading branch information
TrickyPi and dasa authored Jul 15, 2023
1 parent 0e68ff9 commit ee4feb2
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 169 deletions.
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);
}
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

0 comments on commit ee4feb2

Please sign in to comment.