Skip to content

Commit ad141c1

Browse files
authored
fix(commonjs): try/catch instanceof in getAugmentedNamespace (#1868)
Fix to commonjs plugin when calling getAugmentedNamespace on bind
1 parent e1a5ef9 commit ad141c1

File tree

9 files changed

+134
-22
lines changed

9 files changed

+134
-22
lines changed

packages/commonjs/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,11 @@ For these situations, you can change Rollup's behaviour either globally or per m
303303
var f = n.default;
304304
if (typeof f == 'function') {
305305
var a = function a() {
306-
if (this instanceof a) {
306+
var isInstance = false;
307+
try {
308+
isInstance = this instanceof a;
309+
} catch {}
310+
if (isInstance) {
307311
return Reflect.construct(f, arguments, this.constructor);
308312
}
309313
return f.apply(this, arguments);

packages/commonjs/src/helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export function getAugmentedNamespace(n) {
4646
var f = n.default;
4747
if (typeof f == "function") {
4848
var a = function a () {
49-
if (this instanceof a) {
49+
var isInstance = false;
50+
try {
51+
isInstance = this instanceof a;
52+
} catch {}
53+
if (isInstance) {
5054
return Reflect.construct(f, arguments, this.constructor);
5155
}
5256
return f.apply(this, arguments);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
const bind = Function.prototype.bind;
3+
4+
export { bind as default };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const bind = require('./dep.js');
2+
3+
function f() {
4+
return this || 0;
5+
}
6+
7+
const fBound = Reflect.apply(bind, Function.prototype.call, [f, 42])
8+
9+
module.exports = fBound();

packages/commonjs/test/snapshots/function.js.md

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4214,7 +4214,11 @@ Generated by [AVA](https://avajs.dev).
42144214
var f = n.default;␊
42154215
if (typeof f == "function") {␊
42164216
var a = function a () {␊
4217-
if (this instanceof a) {␊
4217+
var isInstance = false;␊
4218+
try {␊
4219+
isInstance = this instanceof a;␊
4220+
} catch {}␊
4221+
if (isInstance) {␊
42184222
return Reflect.construct(f, arguments, this.constructor);␊
42194223
}␊
42204224
return f.apply(this, arguments);␊
@@ -4312,7 +4316,11 @@ Generated by [AVA](https://avajs.dev).
43124316
var f = n.default;␊
43134317
if (typeof f == "function") {␊
43144318
var a = function a () {␊
4315-
if (this instanceof a) {␊
4319+
var isInstance = false;␊
4320+
try {␊
4321+
isInstance = this instanceof a;␊
4322+
} catch {}␊
4323+
if (isInstance) {␊
43164324
return Reflect.construct(f, arguments, this.constructor);␊
43174325
}␊
43184326
return f.apply(this, arguments);␊
@@ -4404,7 +4412,11 @@ Generated by [AVA](https://avajs.dev).
44044412
var f = n.default;␊
44054413
if (typeof f == "function") {␊
44064414
var a = function a () {␊
4407-
if (this instanceof a) {␊
4415+
var isInstance = false;␊
4416+
try {␊
4417+
isInstance = this instanceof a;␊
4418+
} catch {}␊
4419+
if (isInstance) {␊
44084420
return Reflect.construct(f, arguments, this.constructor);␊
44094421
}␊
44104422
return f.apply(this, arguments);␊
@@ -4540,7 +4552,11 @@ Generated by [AVA](https://avajs.dev).
45404552
var f = n.default;␊
45414553
if (typeof f == "function") {␊
45424554
var a = function a () {␊
4543-
if (this instanceof a) {␊
4555+
var isInstance = false;␊
4556+
try {␊
4557+
isInstance = this instanceof a;␊
4558+
} catch {}␊
4559+
if (isInstance) {␊
45444560
return Reflect.construct(f, arguments, this.constructor);␊
45454561
}␊
45464562
return f.apply(this, arguments);␊
@@ -4644,7 +4660,11 @@ Generated by [AVA](https://avajs.dev).
46444660
var f = n.default;␊
46454661
if (typeof f == "function") {␊
46464662
var a = function a () {␊
4647-
if (this instanceof a) {␊
4663+
var isInstance = false;␊
4664+
try {␊
4665+
isInstance = this instanceof a;␊
4666+
} catch {}␊
4667+
if (isInstance) {␊
46484668
return Reflect.construct(f, arguments, this.constructor);␊
46494669
}␊
46504670
return f.apply(this, arguments);␊
@@ -4713,7 +4733,11 @@ Generated by [AVA](https://avajs.dev).
47134733
var f = n.default;␊
47144734
if (typeof f == "function") {␊
47154735
var a = function a () {␊
4716-
if (this instanceof a) {␊
4736+
var isInstance = false;␊
4737+
try {␊
4738+
isInstance = this instanceof a;␊
4739+
} catch {}␊
4740+
if (isInstance) {␊
47174741
return Reflect.construct(f, arguments, this.constructor);␊
47184742
}␊
47194743
return f.apply(this, arguments);␊
@@ -4982,7 +5006,11 @@ Generated by [AVA](https://avajs.dev).
49825006
var f = n.default;␊
49835007
if (typeof f == "function") {␊
49845008
var a = function a () {␊
4985-
if (this instanceof a) {␊
5009+
var isInstance = false;␊
5010+
try {␊
5011+
isInstance = this instanceof a;␊
5012+
} catch {}␊
5013+
if (isInstance) {␊
49865014
return Reflect.construct(f, arguments, this.constructor);␊
49875015
}␊
49885016
return f.apply(this, arguments);␊
@@ -5271,7 +5299,11 @@ Generated by [AVA](https://avajs.dev).
52715299
var f = n.default;␊
52725300
if (typeof f == "function") {␊
52735301
var a = function a () {␊
5274-
if (this instanceof a) {␊
5302+
var isInstance = false;␊
5303+
try {␊
5304+
isInstance = this instanceof a;␊
5305+
} catch {}␊
5306+
if (isInstance) {␊
52755307
return Reflect.construct(f, arguments, this.constructor);␊
52765308
}␊
52775309
return f.apply(this, arguments);␊
@@ -5394,7 +5426,11 @@ Generated by [AVA](https://avajs.dev).
53945426
var f = n.default;␊
53955427
if (typeof f == "function") {␊
53965428
var a = function a () {␊
5397-
if (this instanceof a) {␊
5429+
var isInstance = false;␊
5430+
try {␊
5431+
isInstance = this instanceof a;␊
5432+
} catch {}␊
5433+
if (isInstance) {␊
53985434
return Reflect.construct(f, arguments, this.constructor);␊
53995435
}␊
54005436
return f.apply(this, arguments);␊
@@ -5624,7 +5660,11 @@ Generated by [AVA](https://avajs.dev).
56245660
var f = n.default;␊
56255661
if (typeof f == "function") {␊
56265662
var a = function a () {␊
5627-
if (this instanceof a) {␊
5663+
var isInstance = false;␊
5664+
try {␊
5665+
isInstance = this instanceof a;␊
5666+
} catch {}␊
5667+
if (isInstance) {␊
56285668
return Reflect.construct(f, arguments, this.constructor);␊
56295669
}␊
56305670
return f.apply(this, arguments);␊
@@ -5974,7 +6014,11 @@ Generated by [AVA](https://avajs.dev).
59746014
var f = n.default;␊
59756015
if (typeof f == "function") {␊
59766016
var a = function a () {␊
5977-
if (this instanceof a) {␊
6017+
var isInstance = false;␊
6018+
try {␊
6019+
isInstance = this instanceof a;␊
6020+
} catch {}␊
6021+
if (isInstance) {␊
59786022
return Reflect.construct(f, arguments, this.constructor);␊
59796023
}␊
59806024
return f.apply(this, arguments);␊
@@ -6089,7 +6133,11 @@ Generated by [AVA](https://avajs.dev).
60896133
var f = n.default;␊
60906134
if (typeof f == "function") {␊
60916135
var a = function a () {␊
6092-
if (this instanceof a) {␊
6136+
var isInstance = false;␊
6137+
try {␊
6138+
isInstance = this instanceof a;␊
6139+
} catch {}␊
6140+
if (isInstance) {␊
60936141
return Reflect.construct(f, arguments, this.constructor);␊
60946142
}␊
60956143
return f.apply(this, arguments);␊
@@ -7539,7 +7587,11 @@ Generated by [AVA](https://avajs.dev).
75397587
var f = n.default;␊
75407588
if (typeof f == "function") {␊
75417589
var a = function a () {␊
7542-
if (this instanceof a) {␊
7590+
var isInstance = false;␊
7591+
try {␊
7592+
isInstance = this instanceof a;␊
7593+
} catch {}␊
7594+
if (isInstance) {␊
75437595
return Reflect.construct(f, arguments, this.constructor);␊
75447596
}␊
75457597
return f.apply(this, arguments);␊
@@ -8285,7 +8337,11 @@ Generated by [AVA](https://avajs.dev).
82858337
var f = n.default;␊
82868338
if (typeof f == "function") {␊
82878339
var a = function a () {␊
8288-
if (this instanceof a) {␊
8340+
var isInstance = false;␊
8341+
try {␊
8342+
isInstance = this instanceof a;␊
8343+
} catch {}␊
8344+
if (isInstance) {␊
82898345
return Reflect.construct(f, arguments, this.constructor);␊
82908346
}␊
82918347
return f.apply(this, arguments);␊
@@ -8348,7 +8404,11 @@ Generated by [AVA](https://avajs.dev).
83488404
var f = n.default;␊
83498405
if (typeof f == "function") {␊
83508406
var a = function a () {␊
8351-
if (this instanceof a) {␊
8407+
var isInstance = false;␊
8408+
try {␊
8409+
isInstance = this instanceof a;␊
8410+
} catch {}␊
8411+
if (isInstance) {␊
83528412
return Reflect.construct(f, arguments, this.constructor);␊
83538413
}␊
83548414
return f.apply(this, arguments);␊
@@ -9594,7 +9654,11 @@ Generated by [AVA](https://avajs.dev).
95949654
var f = n.default;␊
95959655
if (typeof f == "function") {␊
95969656
var a = function a () {␊
9597-
if (this instanceof a) {␊
9657+
var isInstance = false;␊
9658+
try {␊
9659+
isInstance = this instanceof a;␊
9660+
} catch {}␊
9661+
if (isInstance) {␊
95989662
return Reflect.construct(f, arguments, this.constructor);␊
95999663
}␊
96009664
return f.apply(this, arguments);␊
@@ -9652,7 +9716,11 @@ Generated by [AVA](https://avajs.dev).
96529716
var f = n.default;␊
96539717
if (typeof f == "function") {␊
96549718
var a = function a () {␊
9655-
if (this instanceof a) {␊
9719+
var isInstance = false;␊
9720+
try {␊
9721+
isInstance = this instanceof a;␊
9722+
} catch {}␊
9723+
if (isInstance) {␊
96569724
return Reflect.construct(f, arguments, this.constructor);␊
96579725
}␊
96589726
return f.apply(this, arguments);␊
94 Bytes
Binary file not shown.

packages/commonjs/test/snapshots/test.js.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ Generated by [AVA](https://avajs.dev).
4747
var f = n.default;␊
4848
if (typeof f == "function") {␊
4949
var a = function a () {␊
50-
if (this instanceof a) {␊
50+
var isInstance = false;␊
51+
try {␊
52+
isInstance = this instanceof a;␊
53+
} catch {}␊
54+
if (isInstance) {␊
5155
return Reflect.construct(f, arguments, this.constructor);␊
5256
}␊
5357
return f.apply(this, arguments);␊
@@ -256,7 +260,11 @@ Generated by [AVA](https://avajs.dev).
256260
var f = n.default;␊
257261
if (typeof f == "function") {␊
258262
var a = function a () {␊
259-
if (this instanceof a) {␊
263+
var isInstance = false;␊
264+
try {␊
265+
isInstance = this instanceof a;␊
266+
} catch {}␊
267+
if (isInstance) {␊
260268
return Reflect.construct(f, arguments, this.constructor);␊
261269
}␊
262270
return f.apply(this, arguments);␊
@@ -382,7 +390,11 @@ Generated by [AVA](https://avajs.dev).
382390
var f = n.default;␊
383391
if (typeof f == "function") {␊
384392
var a = function a () {␊
385-
if (this instanceof a) {␊
393+
var isInstance = false;␊
394+
try {␊
395+
isInstance = this instanceof a;␊
396+
} catch {}␊
397+
if (isInstance) {␊
386398
return Reflect.construct(f, arguments, this.constructor);␊
387399
}␊
388400
return f.apply(this, arguments);␊
33 Bytes
Binary file not shown.

packages/commonjs/test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,3 +1328,14 @@ test('keep the shebang at the top of the file content', async (t) => {
13281328

13291329
t.is(output[0].code.startsWith('#!/usr/bin/env node\n'), true);
13301330
});
1331+
1332+
test('handles bind when requireReturnsDefault is false', async (t) => {
1333+
const bundle = await rollup({
1334+
input: 'fixtures/samples/bind/main.js',
1335+
plugins: [commonjs()]
1336+
});
1337+
1338+
const result = await executeBundle(bundle, t);
1339+
t.is(result.error, undefined);
1340+
t.is(result.exports, 42);
1341+
});

0 commit comments

Comments
 (0)