Skip to content

Commit ee4feb2

Browse files
TrickyPidasa
andauthored
fix(commonjs): preserve this reference in the child class (#1537)
* 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]>
1 parent 0e68ff9 commit ee4feb2

File tree

8 files changed

+146
-169
lines changed

8 files changed

+146
-169
lines changed

β€Žpackages/commonjs/src/helpers.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ export function getAugmentedNamespace(n) {
4040
if (typeof f == "function") {
4141
var a = function a () {
4242
if (this instanceof a) {
43-
var args = [null];
44-
args.push.apply(args, arguments);
45-
var Ctor = Function.bind.apply(f, args);
46-
return new Ctor();
43+
return Reflect.construct(f, arguments, this.constructor);
4744
}
4845
return f.apply(this, arguments);
4946
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function Dep(){};
2+
export default Dep;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Dep = require('./dep');
2+
3+
class Main extends Dep {
4+
constructor() {
5+
super();
6+
this.name = this.constructor.name;
7+
}
8+
static name = "main";
9+
}
10+
11+
t.is(new Main().name, "main");

β€Žpackages/commonjs/test/snapshots/function.js.md

+74-60
Original file line numberDiff line numberDiff line change
@@ -3543,10 +3543,7 @@ Generated by [AVA](https://avajs.dev).
35433543
if (typeof f == "function") {␊
35443544
var a = function a () {␊
35453545
if (this instanceof a) {␊
3546-
var args = [null];␊
3547-
args.push.apply(args, arguments);␊
3548-
var Ctor = Function.bind.apply(f, args);␊
3549-
return new Ctor();␊
3546+
return Reflect.construct(f, arguments, this.constructor);␊
35503547
}␊
35513548
return f.apply(this, arguments);␊
35523549
};␊
@@ -3630,10 +3627,7 @@ Generated by [AVA](https://avajs.dev).
36303627
if (typeof f == "function") {␊
36313628
var a = function a () {␊
36323629
if (this instanceof a) {␊
3633-
var args = [null];␊
3634-
args.push.apply(args, arguments);␊
3635-
var Ctor = Function.bind.apply(f, args);␊
3636-
return new Ctor();␊
3630+
return Reflect.construct(f, arguments, this.constructor);␊
36373631
}␊
36383632
return f.apply(this, arguments);␊
36393633
};␊
@@ -3711,10 +3705,7 @@ Generated by [AVA](https://avajs.dev).
37113705
if (typeof f == "function") {␊
37123706
var a = function a () {␊
37133707
if (this instanceof a) {␊
3714-
var args = [null];␊
3715-
args.push.apply(args, arguments);␊
3716-
var Ctor = Function.bind.apply(f, args);␊
3717-
return new Ctor();␊
3708+
return Reflect.construct(f, arguments, this.constructor);␊
37183709
}␊
37193710
return f.apply(this, arguments);␊
37203711
};␊
@@ -3822,10 +3813,7 @@ Generated by [AVA](https://avajs.dev).
38223813
if (typeof f == "function") {␊
38233814
var a = function a () {␊
38243815
if (this instanceof a) {␊
3825-
var args = [null];␊
3826-
args.push.apply(args, arguments);␊
3827-
var Ctor = Function.bind.apply(f, args);␊
3828-
return new Ctor();␊
3816+
return Reflect.construct(f, arguments, this.constructor);␊
38293817
}␊
38303818
return f.apply(this, arguments);␊
38313819
};␊
@@ -3915,10 +3903,7 @@ Generated by [AVA](https://avajs.dev).
39153903
if (typeof f == "function") {␊
39163904
var a = function a () {␊
39173905
if (this instanceof a) {␊
3918-
var args = [null];␊
3919-
args.push.apply(args, arguments);␊
3920-
var Ctor = Function.bind.apply(f, args);␊
3921-
return new Ctor();␊
3906+
return Reflect.construct(f, arguments, this.constructor);␊
39223907
}␊
39233908
return f.apply(this, arguments);␊
39243909
};␊
@@ -4104,10 +4089,7 @@ Generated by [AVA](https://avajs.dev).
41044089
if (typeof f == "function") {␊
41054090
var a = function a () {␊
41064091
if (this instanceof a) {␊
4107-
var args = [null];␊
4108-
args.push.apply(args, arguments);␊
4109-
var Ctor = Function.bind.apply(f, args);␊
4110-
return new Ctor();␊
4092+
return Reflect.construct(f, arguments, this.constructor);␊
41114093
}␊
41124094
return f.apply(this, arguments);␊
41134095
};␊
@@ -4341,10 +4323,7 @@ Generated by [AVA](https://avajs.dev).
43414323
if (typeof f == "function") {␊
43424324
var a = function a () {␊
43434325
if (this instanceof a) {␊
4344-
var args = [null];␊
4345-
args.push.apply(args, arguments);␊
4346-
var Ctor = Function.bind.apply(f, args);␊
4347-
return new Ctor();␊
4326+
return Reflect.construct(f, arguments, this.constructor);␊
43484327
}␊
43494328
return f.apply(this, arguments);␊
43504329
};␊
@@ -4444,10 +4423,7 @@ Generated by [AVA](https://avajs.dev).
44444423
if (typeof f == "function") {␊
44454424
var a = function a () {␊
44464425
if (this instanceof a) {␊
4447-
var args = [null];␊
4448-
args.push.apply(args, arguments);␊
4449-
var Ctor = Function.bind.apply(f, args);␊
4450-
return new Ctor();␊
4426+
return Reflect.construct(f, arguments, this.constructor);␊
44514427
}␊
44524428
return f.apply(this, arguments);␊
44534429
};␊
@@ -4638,10 +4614,7 @@ Generated by [AVA](https://avajs.dev).
46384614
if (typeof f == "function") {␊
46394615
var a = function a () {␊
46404616
if (this instanceof a) {␊
4641-
var args = [null];␊
4642-
args.push.apply(args, arguments);␊
4643-
var Ctor = Function.bind.apply(f, args);␊
4644-
return new Ctor();␊
4617+
return Reflect.construct(f, arguments, this.constructor);␊
46454618
}␊
46464619
return f.apply(this, arguments);␊
46474620
};␊
@@ -4917,10 +4890,7 @@ Generated by [AVA](https://avajs.dev).
49174890
if (typeof f == "function") {␊
49184891
var a = function a () {␊
49194892
if (this instanceof a) {␊
4920-
var args = [null];␊
4921-
args.push.apply(args, arguments);␊
4922-
var Ctor = Function.bind.apply(f, args);␊
4923-
return new Ctor();␊
4893+
return Reflect.construct(f, arguments, this.constructor);␊
49244894
}␊
49254895
return f.apply(this, arguments);␊
49264896
};␊
@@ -5012,10 +4982,7 @@ Generated by [AVA](https://avajs.dev).
50124982
if (typeof f == "function") {␊
50134983
var a = function a () {␊
50144984
if (this instanceof a) {␊
5015-
var args = [null];␊
5016-
args.push.apply(args, arguments);␊
5017-
var Ctor = Function.bind.apply(f, args);␊
5018-
return new Ctor();␊
4985+
return Reflect.construct(f, arguments, this.constructor);␊
50194986
}␊
50204987
return f.apply(this, arguments);␊
50214988
};␊
@@ -5899,6 +5866,65 @@ Generated by [AVA](https://avajs.dev).
58995866
`,
59005867
}
59015868

5869+
## preserv-this-reference-in-the-child-class
5870+
5871+
> Snapshot 1
5872+
5873+
{
5874+
'main.js': `'use strict';␊
5875+
␊
5876+
function getAugmentedNamespace(n) {␊
5877+
if (n.__esModule) return n;␊
5878+
var f = n.default;␊
5879+
if (typeof f == "function") {␊
5880+
var a = function a () {␊
5881+
if (this instanceof a) {␊
5882+
return Reflect.construct(f, arguments, this.constructor);␊
5883+
}␊
5884+
return f.apply(this, arguments);␊
5885+
};␊
5886+
a.prototype = f.prototype;␊
5887+
} else a = {};␊
5888+
Object.defineProperty(a, '__esModule', {value: true});␊
5889+
Object.keys(n).forEach(function (k) {␊
5890+
var d = Object.getOwnPropertyDescriptor(n, k);␊
5891+
Object.defineProperty(a, k, d.get ? d : {␊
5892+
enumerable: true,␊
5893+
get: function () {␊
5894+
return n[k];␊
5895+
}␊
5896+
});␊
5897+
});␊
5898+
return a;␊
5899+
}␊
5900+
␊
5901+
var main = {};␊
5902+
␊
5903+
function Dep$1(){}␊
5904+
␊
5905+
var dep = /*#__PURE__*/Object.freeze({␊
5906+
__proto__: null,␊
5907+
default: Dep$1␊
5908+
});␊
5909+
␊
5910+
var require$$0 = /*@__PURE__*/getAugmentedNamespace(dep);␊
5911+
␊
5912+
const Dep = require$$0;␊
5913+
␊
5914+
class Main extends Dep {␊
5915+
constructor() {␊
5916+
super();␊
5917+
this.name = this.constructor.name;␊
5918+
}␊
5919+
static name = "main";␊
5920+
}␊
5921+
␊
5922+
t.is(new Main().name, "main");␊
5923+
␊
5924+
module.exports = main;␊
5925+
`,
5926+
}
5927+
59025928
## preserve-modules
59035929

59045930
> Snapshot 1
@@ -6370,10 +6396,7 @@ Generated by [AVA](https://avajs.dev).
63706396
if (typeof f == "function") {␊
63716397
var a = function a () {␊
63726398
if (this instanceof a) {␊
6373-
var args = [null];␊
6374-
args.push.apply(args, arguments);␊
6375-
var Ctor = Function.bind.apply(f, args);␊
6376-
return new Ctor();␊
6399+
return Reflect.construct(f, arguments, this.constructor);␊
63776400
}␊
63786401
return f.apply(this, arguments);␊
63796402
};␊
@@ -6422,10 +6445,7 @@ Generated by [AVA](https://avajs.dev).
64226445
if (typeof f == "function") {␊
64236446
var a = function a () {␊
64246447
if (this instanceof a) {␊
6425-
var args = [null];␊
6426-
args.push.apply(args, arguments);␊
6427-
var Ctor = Function.bind.apply(f, args);␊
6428-
return new Ctor();␊
6448+
return Reflect.construct(f, arguments, this.constructor);␊
64296449
}␊
64306450
return f.apply(this, arguments);␊
64316451
};␊
@@ -7435,10 +7455,7 @@ Generated by [AVA](https://avajs.dev).
74357455
if (typeof f == "function") {␊
74367456
var a = function a () {␊
74377457
if (this instanceof a) {␊
7438-
var args = [null];␊
7439-
args.push.apply(args, arguments);␊
7440-
var Ctor = Function.bind.apply(f, args);␊
7441-
return new Ctor();␊
7458+
return Reflect.construct(f, arguments, this.constructor);␊
74427459
}␊
74437460
return f.apply(this, arguments);␊
74447461
};␊
@@ -7488,10 +7505,7 @@ Generated by [AVA](https://avajs.dev).
74887505
if (typeof f == "function") {␊
74897506
var a = function a () {␊
74907507
if (this instanceof a) {␊
7491-
var args = [null];␊
7492-
args.push.apply(args, arguments);␊
7493-
var Ctor = Function.bind.apply(f, args);␊
7494-
return new Ctor();␊
7508+
return Reflect.construct(f, arguments, this.constructor);␊
74957509
}␊
74967510
return f.apply(this, arguments);␊
74977511
};␊
Binary file not shown.

β€Žpackages/commonjs/test/snapshots/test.js.md

+3-12
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ Generated by [AVA](https://avajs.dev).
3939
if (typeof f == "function") {␊
4040
var a = function a () {␊
4141
if (this instanceof a) {␊
42-
var args = [null];␊
43-
args.push.apply(args, arguments);␊
44-
var Ctor = Function.bind.apply(f, args);␊
45-
return new Ctor();␊
42+
return Reflect.construct(f, arguments, this.constructor);␊
4643
}␊
4744
return f.apply(this, arguments);␊
4845
};␊
@@ -199,10 +196,7 @@ Generated by [AVA](https://avajs.dev).
199196
if (typeof f == "function") {␊
200197
var a = function a () {␊
201198
if (this instanceof a) {␊
202-
var args = [null];␊
203-
args.push.apply(args, arguments);␊
204-
var Ctor = Function.bind.apply(f, args);␊
205-
return new Ctor();␊
199+
return Reflect.construct(f, arguments, this.constructor);␊
206200
}␊
207201
return f.apply(this, arguments);␊
208202
};␊
@@ -294,10 +288,7 @@ Generated by [AVA](https://avajs.dev).
294288
if (typeof f == "function") {␊
295289
var a = function a () {␊
296290
if (this instanceof a) {␊
297-
var args = [null];␊
298-
args.push.apply(args, arguments);␊
299-
var Ctor = Function.bind.apply(f, args);␊
300-
return new Ctor();␊
291+
return Reflect.construct(f, arguments, this.constructor);␊
301292
}␊
302293
return f.apply(this, arguments);␊
303294
};␊
-23 Bytes
Binary file not shown.

0 commit comments

Comments
Β (0)