Skip to content

Commit

Permalink
JS: keep order of declaration for variable renaming of function argum…
Browse files Browse the repository at this point in the history
…ents, improves GZIP compression
  • Loading branch information
tdewolff committed May 28, 2022
1 parent a086566 commit 0be056c
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ require (
github.com/fsnotify/fsnotify v1.5.4
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/spf13/pflag v1.0.5
github.com/tdewolff/parse/v2 v2.5.31
github.com/tdewolff/parse/v2 v2.5.32
github.com/tdewolff/test v1.0.6
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 h1:JAEbJn3j/FrhdWA9jW8
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/tdewolff/parse/v2 v2.5.31 h1:PrJN/trWTUYaYxg7jRpKnKXpBK4kBjT9rAFhFAh2Lus=
github.com/tdewolff/parse/v2 v2.5.31/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
github.com/tdewolff/parse/v2 v2.5.32 h1:paypOpwqGqCjzm/QpgZj8+J4O4glJpe2qCtzM9zffgY=
github.com/tdewolff/parse/v2 v2.5.32/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
github.com/tdewolff/test v1.0.6 h1:76mzYJQ83Op284kMT+63iCNCI7NEERsIN8dLM+RiKr4=
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
Expand Down
2 changes: 1 addition & 1 deletion js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (m *jsMinifier) minifyBlockAsStmt(blockStmt *js.BlockStmt) {
// minify block when statement is expected, i.e. semicolon if empty or remove braces for single statement
// assume we already renamed the scope
hasLexicalVars := false
for _, v := range blockStmt.Scope.Declared[blockStmt.Scope.NumForDeclared:] {
for _, v := range blockStmt.Scope.Declared[blockStmt.Scope.NumForDecls:] {
if v.Decl == js.LexicalDecl {
hasLexicalVars = true
break
Expand Down
2 changes: 1 addition & 1 deletion js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ func TestJSVarRenaming(t *testing.T) {
{`let a=0;switch(a){case 0:let b=1;case 1:let c=2}`, `let a=0;switch(a){case 0:let a=1;case 1:let b=2}`},
{`({a:b=1}={})=>b`, `({a=1}={})=>a`}, // #422
{`()=>{var a;if(x){const b=0;while(true);}}`, `()=>{if(x){const b=0;for(var a;!0;);}}`},
{`(e,s)=>{e=>0,s(e(s))}`, `(b,a)=>{a=>0,a(b(a))}`}, // #469
{`(e,s)=>{e=>0,s(e(s))}`, `(a,b)=>{a=>0,b(a(b))}`}, // #469
}

m := minify.New()
Expand Down
5 changes: 3 additions & 2 deletions js/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func (r *renamer) renameScope(scope js.Scope) {
}

i := 0
sort.Sort(js.VarsByUses(scope.Declared))
// keep function argument declaration order to improve GZIP compression
sort.Sort(js.VarsByUses(scope.Declared[scope.NumFuncArgs:]))
for _, v := range scope.Declared {
v.Data = r.getName(v.Data, i)
i++
Expand Down Expand Up @@ -111,8 +112,8 @@ func (r *renamer) getName(name []byte, index int) []byte {
name[0] = r.identStart[index]
return name[:1]
}

index -= len(r.identStart)

n := 2
for {
offset := len(r.identStart)
Expand Down

0 comments on commit 0be056c

Please sign in to comment.