fix: add some boundary checks so the formatter panics less often

This commit is contained in:
Laytan Laats 2023-04-10 02:17:36 +02:00
parent 68ac672368
commit 6a5a06a206

View File

@ -22,6 +22,9 @@ type formatter struct {
lastSemiColon *token.Token
}
// NewFormatter returns a new formatter. This formatter is not great, it can not be customized,
// but most importantly, loses a lot of doc comments and regular comments, so don't use on code
// that wants to keep all comments in tact.
func NewFormatter() *formatter {
return &formatter{}
}
@ -83,6 +86,10 @@ func (f *formatter) newToken(id token.ID, val []byte) *token.Token {
}
func (f *formatter) formatList(nodes []ast.Vertex, separator byte) []*token.Token {
if len(nodes) == 0 {
return nil
}
separatorTkns := make([]*token.Token, len(nodes)-1)
for i, v := range nodes {
v.Accept(f)
@ -844,7 +851,6 @@ func (f *formatter) StmtNamespace(n *ast.StmtNamespace) {
} else {
n.SemiColonTkn = f.newSemicolonTkn()
}
}
func (f *formatter) StmtNop(n *ast.StmtNop) {
@ -1189,8 +1195,10 @@ func (f *formatter) ExprArrayItem(n *ast.ExprArrayItem) {
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
}
if n.Val != nil {
n.Val.Accept(f)
}
}
func (f *formatter) ExprArrowFunction(n *ast.ExprArrowFunction) {
if n.AttrGroups != nil {