From be7a92aa5c834d1d091ab0a800e742a624f278a9 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Fri, 22 Jan 2021 12:17:20 +0100 Subject: [PATCH] jsonnetfmt: Add plus objects instead of removing them --- internal/formatter/fix_plus_object.go | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/internal/formatter/fix_plus_object.go b/internal/formatter/fix_plus_object.go index e379b9a..cb2add6 100644 --- a/internal/formatter/fix_plus_object.go +++ b/internal/formatter/fix_plus_object.go @@ -21,28 +21,20 @@ import ( "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 { 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) { - binary, ok := (*node).(*ast.Binary) + applyBrace, ok := (*node).(*ast.ApplyBrace) if ok { - // Could relax this to allow more ASTs on the LHS but this seems OK for now. - _, leftIsVar := binary.Left.(*ast.Var) - _, leftIsIndex := binary.Left.(*ast.Index) - if leftIsVar || leftIsIndex { - rhs, ok := binary.Right.(*ast.Object) - if ok && binary.Op == ast.BopPlus { - ast.FodderMoveFront(&rhs.Fodder, &binary.OpFodder) - *node = &ast.ApplyBrace{ - NodeBase: binary.NodeBase, - Left: binary.Left, - Right: rhs, - } - } + *node = &ast.Binary{ + NodeBase: applyBrace.NodeBase, + Left: applyBrace.Left, + Op: ast.BopPlus, + Right: applyBrace.Right, } } c.Base.Visit(p, node, ctx)