Skip to content
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

Unfold nested blocks when minifySyntax is enabled #3158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 6 additions & 8 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5281,14 +5281,12 @@ foo();
TestStrictModeNestedFnDeclKeepNamesVariableInliningIssue1552
---------- /out/entry.js ----------
export function outer() {
{
let inner = function() {
return Math.random();
};
__name(inner, "inner");
const x = inner();
console.log(x);
}
let inner = function() {
return Math.random();
};
__name(inner, "inner");
const x = inner();
console.log(x);
}
__name(outer, "outer"), outer();

Expand Down
20 changes: 20 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10275,6 +10275,12 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_
}
}

if p.options.minifySyntax && len(s.Fn.Body.Block.Stmts) == 1 {
if block, ok := s.Fn.Body.Block.Stmts[0].Data.(*js_ast.SBlock); ok {
s.Fn.Body.Block = *block
}
}

// Handle exporting this function from a namespace
if s.IsExport && p.enclosingNamespaceArgRef != nil {
s.IsExport = false
Expand Down Expand Up @@ -11027,6 +11033,11 @@ func (p *parser) visitClass(nameScopeLoc logger.Loc, class *js_ast.Class, defaul
if p.options.minifySyntax && len(property.ClassStaticBlock.Block.Stmts) == 0 {
continue
}
if p.options.minifySyntax && len(property.ClassStaticBlock.Block.Stmts) == 1 {
if block, ok := property.ClassStaticBlock.Block.Stmts[0].Data.(*js_ast.SBlock); ok {
property.ClassStaticBlock.Block = *block
}
}

// Keep this property
class.Properties[end] = *property
Expand Down Expand Up @@ -14353,6 +14364,9 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
e.PreferExpr = true
}
}
if s, ok := e.Body.Block.Stmts[0].Data.(*js_ast.SBlock); ok {
e.Body.Block = *s
}
}

p.fnOnlyDataVisit.isInsideAsyncArrowFn = oldInsideAsyncArrowFn
Expand Down Expand Up @@ -14399,6 +14413,12 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
}
}

if p.options.minifySyntax && len(e.Fn.Body.Block.Stmts) == 1 {
if s, ok := e.Fn.Body.Block.Stmts[0].Data.(*js_ast.SBlock); ok {
e.Fn.Body.Block = *s
}
}

case *js_ast.EClass:
// Check for a propagated name to keep from the parent context
var nameToKeep string
Expand Down
10 changes: 10 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,16 @@ func TestMangleBlock(t *testing.T) {
expectPrintedMangle(t, "while(1) { function* x() {} }", "for (; ; ) {\n function* x() {\n }\n}\n")
expectPrintedMangle(t, "while(1) { async function x() {} }", "for (; ; ) {\n async function x() {\n }\n}\n")
expectPrintedMangle(t, "while(1) { async function* x() {} }", "for (; ; ) {\n async function* x() {\n }\n}\n")

expectPrintedMangle(t, "function f() {{ let a = ''; console.log(a); console.log(a); }}", "function f() {\n let a = \"\";\n console.log(a), console.log(a);\n}\n")
expectPrintedMangle(t, "const f = () => {{ let a = ''; console.log(a); console.log(a); }}", "const f = () => {\n let a = \"\";\n console.log(a), console.log(a);\n};\n")
expectPrintedMangle(t, "const f = function() {{ let a = ''; console.log(a); console.log(a); }}", "const f = function() {\n let a = \"\";\n console.log(a), console.log(a);\n};\n")
expectPrintedMangle(t, "const obj = { m() {{ let a = ''; console.log(a); console.log(a); }} }", "const obj = { m() {\n let a = \"\";\n console.log(a), console.log(a);\n} };\n")
expectPrintedMangle(t, "class F { static {{ let a = ''; console.log(a); console.log(a); }} }", "class F {\n static {\n let a = \"\";\n console.log(a), console.log(a);\n }\n}\n")
expectPrintedMangle(t, "class F { m() {{ let a = ''; console.log(a); console.log(a); }} }", "class F {\n m() {\n let a = \"\";\n console.log(a), console.log(a);\n }\n}\n")
expectPrintedMangle(t, "function f() {if (true) { let a = ''; console.log(a); console.log(a); }}", "function f() {\n let a = \"\";\n console.log(a), console.log(a);\n}\n")
expectPrintedMangle(t, "function f() {if (true) { let a = ''; console.log(a); console.log(a); } else { console.log() }}", "function f() {\n let a = \"\";\n console.log(a), console.log(a);\n}\n")
expectPrintedMangle(t, "function f() {{ let a = ''; console.log(a); console.log(a); }; let b = '';}", "function f() {\n {\n let a = \"\";\n console.log(a), console.log(a);\n }\n let b = \"\";\n}\n")
}

func TestMangleSwitch(t *testing.T) {
Expand Down