From 1bf97e65a0ff1b6f274fee09717e3cab80c84f6c Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Thu, 20 Aug 2020 16:49:11 -0700 Subject: [PATCH 1/2] Add failing test case --- internal/bundler/bundler_default_test.go | 21 +++++++++++++++++++ .../bundler/snapshots/snapshots_default.txt | 13 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/internal/bundler/bundler_default_test.go b/internal/bundler/bundler_default_test.go index 82e6b8f7142..855abfd9bec 100644 --- a/internal/bundler/bundler_default_test.go +++ b/internal/bundler/bundler_default_test.go @@ -79,6 +79,27 @@ func TestNestedCommonJS(t *testing.T) { }) } +// This test makes sure that NewExpressions containing require() calls aren't +// broken. +func TestNewExpressionCommonJS(t *testing.T) { + default_suite.expectBundled(t, bundled{ + files: map[string]string{ + "/entry.js": ` + new (require("./foo.js")).Foo(); + `, + "/foo.js": ` + class Foo {} + module.exports = {Foo}; + `, + }, + entryPaths: []string{"/entry.js"}, + options: config.Options{ + IsBundling: true, + AbsOutputFile: "/out.js", + }, + }) +} + func TestCommonJSFromES6(t *testing.T) { default_suite.expectBundled(t, bundled{ files: map[string]string{ diff --git a/internal/bundler/snapshots/snapshots_default.txt b/internal/bundler/snapshots/snapshots_default.txt index fab18086c42..97e0fb025f1 100644 --- a/internal/bundler/snapshots/snapshots_default.txt +++ b/internal/bundler/snapshots/snapshots_default.txt @@ -1203,6 +1203,19 @@ TestNestedScopeBug a(); })(); +================================================================================ +TestNewExpressionCommonJS +---------- /out.js ---------- +// /foo.js +var require_foo = __commonJS((exports, module) => { + class Foo { + } + module.exports = {Foo}; +}); + +// /entry.js +new (require_foo()).Foo(); + ================================================================================ TestNodeModules ---------- /Users/user/project/out.js ---------- From e5c3190da7030c6c8cd1e0354adbbf2a179c8199 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Thu, 20 Aug 2020 18:16:39 -0700 Subject: [PATCH 2/2] Fix broken NewExpressions containing requires --- internal/printer/printer.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/printer/printer.go b/internal/printer/printer.go index cbd445246d3..122245429ea 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -1226,6 +1226,7 @@ const ( forbidCall = 1 << iota forbidIn hasNonOptionalChainParent + wrapRequireCall ) func (p *printer) printUndefined(level ast.L) { @@ -1290,7 +1291,7 @@ func (p *printer) printExpr(expr ast.Expr, level ast.L, flags int) { p.printSpaceBeforeIdentifier() p.print("new") p.printSpace() - p.printExpr(e.Target, ast.LNew, forbidCall) + p.printExpr(e.Target, ast.LNew, forbidCall|wrapRequireCall) // TODO: Omit this while minifying p.print("(") @@ -1363,7 +1364,7 @@ func (p *printer) printExpr(expr ast.Expr, level ast.L, flags int) { } case *ast.ERequire: - wrap := level >= ast.LNew + wrap := level >= ast.LNew || (flags&wrapRequireCall) != 0 if wrap { p.print("(") }