Skip to content

Commit

Permalink
JS: add support for class field definitions, fixes #380
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Mar 1, 2021
1 parent 0d367aa commit 7125880
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/spf13/pflag v1.0.5
github.com/tdewolff/parse/v2 v2.5.11
github.com/tdewolff/parse/v2 v2.5.12
github.com/tdewolff/test v1.0.6
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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.11 h1:Wq0x026IKZh9GPUB5Fp+v5bki/SNmpIkdltcnm6HrO0=
github.com/tdewolff/parse/v2 v2.5.11/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
github.com/tdewolff/parse/v2 v2.5.12 h1:Dxblj0AftHJ3bwBmKdAd7O1wB1+CVFI/BCaYaGuuoSM=
github.com/tdewolff/parse/v2 v2.5.12/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-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
17 changes: 15 additions & 2 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,21 @@ func (m *jsMinifier) minifyClassDecl(decl js.ClassDecl) {
m.minifyExpr(decl.Extends, js.OpLHS)
}
m.write(openBraceBytes)
for _, item := range decl.Methods {
m.minifyMethodDecl(item)
m.needsSemicolon = false
for _, item := range decl.Definitions {
m.writeSemicolon()
m.minifyPropertyName(item.Name)
if item.Init != nil {
m.write(equalBytes)
m.minifyExpr(item.Init, js.OpAssign)
}
m.requireSemicolon()
}
if 0 < len(decl.Methods) {
m.writeSemicolon()
for _, item := range decl.Methods {
m.minifyMethodDecl(item)
}
}
m.write(closeBraceBytes)
m.needsSemicolon = false
Expand Down
1 change: 1 addition & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func TestJS(t *testing.T) {
{`class a{static async*[1](){}}`, `class a{static async*[1](){}}`},
{`class a{"f"(){}}`, `class a{f(){}}`},
{`class a{f(){};g(){}}`, `class a{f(){}g(){}}`},
{`class a{one;#two = 2;f(){}}`, `class a{one;#two=2;f(){}}`},

// dead code
//{`return;a`, `return`},
Expand Down

0 comments on commit 7125880

Please sign in to comment.