mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-29 01:11:02 +02:00
jsonnetfmt: Add plus objects instead of removing them
This commit is contained in:
parent
74b3a1b669
commit
be7a92aa5c
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user