Skip to content

Commit

Permalink
minify: shorter printing of ts enum property keys
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 25, 2023
1 parent dc913df commit a4e2a1e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 42 deletions.
42 changes: 42 additions & 0 deletions internal/bundler_tests/bundler_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,48 @@ func TestTSEnumCrossModuleInliningReExport(t *testing.T) {
})
}

func TestTSEnumCrossModuleInliningMinifyIndexIntoDot(t *testing.T) {
ts_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
const enum Foo {
foo1 = 'abc',
foo2 = 'a b c',
}
import { Bar } from './lib'
inlined = [
obj[Foo.foo1],
obj[Bar.bar1],
obj?.[Foo.foo1],
obj?.[Bar.bar1],
obj?.prop[Foo.foo1],
obj?.prop[Bar.bar1],
]
notInlined = [
obj[Foo.foo2],
obj[Bar.bar2],
obj?.[Foo.foo2],
obj?.[Bar.bar2],
obj?.prop[Foo.foo2],
obj?.prop[Bar.bar2],
]
`,
"/lib.ts": `
export const enum Bar {
bar1 = 'xyz',
bar2 = 'x y z',
}
`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
MinifySyntax: true,
},
})
}

func TestTSEnumCrossModuleTreeShaking(t *testing.T) {
ts_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
21 changes: 21 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,27 @@ console.log([
a.non_constant
]);

================================================================================
TestTSEnumCrossModuleInliningMinifyIndexIntoDot
---------- /out.js ----------
// entry.ts
inlined = [
obj.abc,
obj.xyz,
obj?.abc,
obj?.xyz,
obj?.prop.abc,
obj?.prop.xyz
];
notInlined = [
obj["a b c" /* foo2 */],
obj["x y z" /* bar2 */],
obj?.["a b c" /* foo2 */],
obj?.["x y z" /* bar2 */],
obj?.prop["a b c" /* foo2 */],
obj?.prop["x y z" /* bar2 */]
];

================================================================================
TestTSEnumCrossModuleInliningReExport
---------- /out/entry.js ----------
Expand Down
83 changes: 41 additions & 42 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,6 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
}

case *js_ast.EIndex:
wrap := false
if e.OptionalChain == js_ast.OptionalChainNone {
flags |= hasNonOptionalChainParent

Expand All @@ -2533,8 +2532,8 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
}
} else {
if (flags & hasNonOptionalChainParent) != 0 {
wrap = true
p.print("(")
defer p.print(")")
}
flags &= ^hasNonOptionalChainParent
}
Expand All @@ -2551,6 +2550,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
name := p.renamer.NameForSymbol(index.Ref)
p.addSourceMappingForName(e.Index.Loc, name, index.Ref)
p.printIdentifier(name)
return

case *js_ast.EMangledProp:
if name := p.mangledPropName(index.Ref); p.canPrintIdentifier(name) {
Expand All @@ -2559,53 +2559,52 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
}
p.addSourceMappingForName(e.Index.Loc, name, index.Ref)
p.printIdentifier(name)
} else {
isMultiLine := p.willPrintExprCommentsAtLoc(e.Index.Loc) || p.willPrintExprCommentsAtLoc(e.CloseBracketLoc)
p.print("[")
if isMultiLine {
p.printNewline()
p.options.Indent++
p.printIndent()
}
p.printExprCommentsAtLoc(e.Index.Loc)
p.addSourceMapping(e.Index.Loc)
p.printQuotedUTF8(name, true /* allowBacktick */)
if isMultiLine {
p.printNewline()
p.printExprCommentsAfterCloseTokenAtLoc(e.CloseBracketLoc)
p.options.Indent--
p.printIndent()
}
if e.CloseBracketLoc.Start > expr.Loc.Start {
p.addSourceMapping(e.CloseBracketLoc)
}
p.print("]")
return
}

default:
isMultiLine := p.willPrintExprCommentsAtLoc(e.Index.Loc) || p.willPrintExprCommentsAtLoc(e.CloseBracketLoc)
p.print("[")
if isMultiLine {
p.printNewline()
p.options.Indent++
p.printIndent()
}
p.printExpr(e.Index, js_ast.LLowest, 0)
if isMultiLine {
p.printNewline()
p.printExprCommentsAfterCloseTokenAtLoc(e.CloseBracketLoc)
p.options.Indent--
p.printIndent()
case *js_ast.EInlinedEnum:
if p.options.MinifySyntax {
if str, ok := index.Value.Data.(*js_ast.EString); ok && p.canPrintIdentifierUTF16(str.Value) {
if e.OptionalChain != js_ast.OptionalChainStart {
p.print(".")
}
p.addSourceMapping(index.Value.Loc)
p.printIdentifierUTF16(str.Value)
return
}
}
if e.CloseBracketLoc.Start > expr.Loc.Start {
p.addSourceMapping(e.CloseBracketLoc)

case *js_ast.EDot:
if p.options.MinifySyntax {
if value, ok := p.tryToGetImportedEnumValue(index.Target, index.Name); ok && value.String != nil && p.canPrintIdentifierUTF16(value.String) {
if e.OptionalChain != js_ast.OptionalChainStart {
p.print(".")
}
p.addSourceMapping(e.Index.Loc)
p.printIdentifierUTF16(value.String)
return
}
}
p.print("]")
}

if wrap {
p.print(")")
isMultiLine := p.willPrintExprCommentsAtLoc(e.Index.Loc) || p.willPrintExprCommentsAtLoc(e.CloseBracketLoc)
p.print("[")
if isMultiLine {
p.printNewline()
p.options.Indent++
p.printIndent()
}
p.printExpr(e.Index, js_ast.LLowest, 0)
if isMultiLine {
p.printNewline()
p.printExprCommentsAfterCloseTokenAtLoc(e.CloseBracketLoc)
p.options.Indent--
p.printIndent()
}
if e.CloseBracketLoc.Start > expr.Loc.Start {
p.addSourceMapping(e.CloseBracketLoc)
}
p.print("]")

case *js_ast.EIf:
wrap := level >= js_ast.LConditional
Expand Down

0 comments on commit a4e2a1e

Please sign in to comment.