Skip to content

Commit

Permalink
class static blocks are a parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 2, 2021
1 parent e79ea6f commit 5366809
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/js_ast/js_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,7 @@ const (
ScopeEntry // This is a module, TypeScript enum, or TypeScript namespace
ScopeFunctionArgs
ScopeFunctionBody
ScopeClassStaticInit
)

func (kind ScopeKind) StopsHoisting() bool {
Expand Down
24 changes: 24 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ type fnOrArrowDataParse struct {
isTopLevel bool
isConstructor bool
isTypeScriptDeclare bool
isClassStaticInit bool

// In TypeScript, forward declarations of functions have no bodies
allowMissingBodyForTypeScript bool
Expand Down Expand Up @@ -1941,6 +1942,26 @@ func (p *parser) parseProperty(kind js_ast.PropertyKind, opts propertyOpts, erro
return p.parseProperty(kind, opts, nil)
}
}
} else if p.lexer.Token == js_lexer.TOpenBrace && name == "static" {
p.log.AddRangeError(&p.tracker, p.lexer.Range(), "Class static blocks are not supported yet")

loc := p.lexer.Loc()
p.lexer.Next()

oldIsClassStaticInit := p.fnOrArrowDataParse.isClassStaticInit
oldAwait := p.fnOrArrowDataParse.await
p.fnOrArrowDataParse.isClassStaticInit = true
p.fnOrArrowDataParse.await = forbidAll

scopeIndex := p.pushScopeForParsePass(js_ast.ScopeClassStaticInit, loc)
p.parseStmtsUpTo(js_lexer.TCloseBrace, parseStmtOpts{})
p.popAndDiscardScope(scopeIndex)

p.fnOrArrowDataParse.isClassStaticInit = oldIsClassStaticInit
p.fnOrArrowDataParse.await = oldAwait

p.lexer.Expect(js_lexer.TCloseBrace)

}
}

Expand Down Expand Up @@ -6349,6 +6370,9 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
return js_ast.Stmt{Loc: loc, Data: &js_ast.SContinue{Label: name}}

case js_lexer.TReturn:
if p.fnOrArrowDataParse.isClassStaticInit {
p.log.AddRangeError(&p.tracker, p.lexer.Range(), "A return statement cannot be used inside a class static block")
}
p.lexer.Next()
var value js_ast.Expr
if p.lexer.Token != js_lexer.TSemicolon &&
Expand Down

0 comments on commit 5366809

Please sign in to comment.