Skip to content

fix(commonjs): try/catch instanceof in getAugmentedNamespace #1868

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

Merged
merged 1 commit into from
Jun 14, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/commonjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ For these situations, you can change Rollup's behaviour either globally or per m
var f = n.default;
if (typeof f == 'function') {
var a = function a() {
if (this instanceof a) {
var isInstance = false;
try {
isInstance = this instanceof a;
} catch {}
if (isInstance) {
return Reflect.construct(f, arguments, this.constructor);
}
return f.apply(this, arguments);
Expand Down
6 changes: 5 additions & 1 deletion packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export function getAugmentedNamespace(n) {
var f = n.default;
if (typeof f == "function") {
var a = function a () {
if (this instanceof a) {
var isInstance = false;
try {
isInstance = this instanceof a;
} catch {}
if (isInstance) {
return Reflect.construct(f, arguments, this.constructor);
}
return f.apply(this, arguments);
Expand Down
4 changes: 4 additions & 0 deletions packages/commonjs/test/fixtures/samples/bind/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

const bind = Function.prototype.bind;

export { bind as default };
9 changes: 9 additions & 0 deletions packages/commonjs/test/fixtures/samples/bind/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const bind = require('./dep.js');

function f() {
return this || 0;
}

const fBound = Reflect.apply(bind, Function.prototype.call, [f, 42])

module.exports = fBound();
102 changes: 85 additions & 17 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4214,7 +4214,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -4312,7 +4316,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -4404,7 +4412,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -4540,7 +4552,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -4644,7 +4660,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -4713,7 +4733,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -4982,7 +5006,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -5271,7 +5299,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -5394,7 +5426,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -5624,7 +5660,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -5974,7 +6014,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -6089,7 +6133,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -7539,7 +7587,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -8285,7 +8337,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -8348,7 +8404,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -9594,7 +9654,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -9652,7 +9716,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
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.
18 changes: 15 additions & 3 deletions packages/commonjs/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -256,7 +260,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
return Reflect.construct(f, arguments, this.constructor);␊
}␊
return f.apply(this, arguments);␊
Expand Down Expand Up @@ -382,7 +390,11 @@ Generated by [AVA](https://avajs.dev).
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
if (this instanceof a) {␊
var isInstance = false;␊
try {␊
isInstance = this instanceof a;␊
} catch {}␊
if (isInstance) {␊
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.
11 changes: 11 additions & 0 deletions packages/commonjs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,3 +1328,14 @@ test('keep the shebang at the top of the file content', async (t) => {

t.is(output[0].code.startsWith('#!/usr/bin/env node\n'), true);
});

test('handles bind when requireReturnsDefault is false', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/bind/main.js',
plugins: [commonjs()]
});

const result = await executeBundle(bundle, t);
t.is(result.error, undefined);
t.is(result.exports, 42);
});