jsonnetfmt: Add plus objects instead of removing them

This commit is contained in:
Simon Let 2021-01-22 12:17:20 +01:00 committed by Stanisław Barzowski
parent 74b3a1b669
commit be7a92aa5c

View File

@ -21,28 +21,20 @@ import (
"github.com/google/go-jsonnet/internal/pass" "github.com/google/go-jsonnet/internal/pass"
) )
// FixPlusObject is a formatter pass that replaces ((e)) with (e). // FixPlusObject is a formatter pass that replaces e {} with e + {}.
type FixPlusObject struct { type FixPlusObject struct {
pass.Base pass.Base
} }
// Visit replaces e + { ... } with an ApplyBrace in some situations. // Visit replaces ApplyBrace with Binary node.
func (c *FixPlusObject) Visit(p pass.ASTPass, node *ast.Node, ctx pass.Context) { func (c *FixPlusObject) Visit(p pass.ASTPass, node *ast.Node, ctx pass.Context) {
binary, ok := (*node).(*ast.Binary) applyBrace, ok := (*node).(*ast.ApplyBrace)
if ok { if ok {
// Could relax this to allow more ASTs on the LHS but this seems OK for now. *node = &ast.Binary{
_, leftIsVar := binary.Left.(*ast.Var) NodeBase: applyBrace.NodeBase,
_, leftIsIndex := binary.Left.(*ast.Index) Left: applyBrace.Left,
if leftIsVar || leftIsIndex { Op: ast.BopPlus,
rhs, ok := binary.Right.(*ast.Object) Right: applyBrace.Right,
if ok && binary.Op == ast.BopPlus {
ast.FodderMoveFront(&rhs.Fodder, &binary.OpFodder)
*node = &ast.ApplyBrace{
NodeBase: binary.NodeBase,
Left: binary.Left,
Right: rhs,
}
}
} }
} }
c.Base.Visit(p, node, ctx) c.Base.Visit(p, node, ctx)