Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: jsonnetfmt remove newlines after ObjectField id #780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ func runTest(t *testing.T, test *formatterTest, changedGoldensList *ChangedGolde
return
}
if diff, hasDiff := testutils.CompareWithGolden(outData, golden); hasDiff {
t.Error(fmt.Errorf("golden file %v has diff:\n%v", test.input, diff))
if strings.HasPrefix(outData, string(golden)) ||
strings.HasPrefix(string(golden), outData) {
// The difference can be a trailing newline, invisible in text format.
fmt.Printf("actual:\n%v\n", []byte(outData))
fmt.Printf("expected:\n%v\n", golden)
}
t.Error(fmt.Errorf("golden file for %v has diff:\n%v", test.input, diff))
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions formatter/testdata/json-to-xml.fmt.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local c =
import "json2xml.libjsonnet";

local d = {
name: "foo",

children: ["bar", "bam"],

attrs: {
class1: "abc",
numbers: [1, 2, 3, 4],
},
};

{
output: c.manifestXml(d, "elements"),
}
21 changes: 21 additions & 0 deletions formatter/testdata/json-to-xml.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local c =
import "json2xml.libjsonnet";

local d = {
name:

"foo",

children:
["bar", "bam"],

attrs: {
class1: "abc",
numbers: [1, 2, 3, 4],
},
};

{
output:
c.manifestXml(d, "elements"),
}
102 changes: 77 additions & 25 deletions internal/formatter/fix_newlines.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Google Inc. All rights reserved.
Copyright 2019,2024 Google LLC. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,10 +30,12 @@ import (
//
// It only looks shallowly at the AST nodes, so there may be some newlines deeper that
// don't affect expanding. For example:
// [{
// 'a': 'b',
// 'c': 'd',
// }]
//
// [{
// 'a': 'b',
// 'c': 'd',
// }]
//
// The outer array can stay unexpanded, because there are no newlines between
// the square brackets and the braces.
type FixNewlines struct {
Expand Down Expand Up @@ -197,19 +199,26 @@ func (c *FixNewlines) Parens(p pass.ASTPass, parens *ast.Parens, ctx pass.Contex

// Parameters handles parameters
// Example2:
// f(1, 2,
// 3)
//
// f(1, 2,
// 3)
//
// Should be expanded to:
// f(1,
// 2,
// 3)
//
// f(1,
// 2,
// 3)
//
// And:
// foo(
// 1, 2, 3)
//
// foo(
// 1, 2, 3)
//
// Should be expanded to:
// foo(
// 1, 2, 3
// )
//
// foo(
// 1, 2, 3
// )
func (c *FixNewlines) Parameters(p pass.ASTPass, l *ast.Fodder, params *[]ast.Parameter, r *ast.Fodder, ctx pass.Context) {
shouldExpandBetween := false
shouldExpandNearParens := false
Expand Down Expand Up @@ -243,19 +252,26 @@ func (c *FixNewlines) Parameters(p pass.ASTPass, l *ast.Fodder, params *[]ast.Pa

// Arguments handles parameters
// Example2:
// f(1, 2,
// 3)
//
// f(1, 2,
// 3)
//
// Should be expanded to:
// f(1,
// 2,
// 3)
//
// f(1,
// 2,
// 3)
//
// And:
// foo(
// 1, 2, 3)
//
// foo(
// 1, 2, 3)
//
// Should be expanded to:
// foo(
// 1, 2, 3
// )
//
// foo(
// 1, 2, 3
// )
func (c *FixNewlines) Arguments(p pass.ASTPass, l *ast.Fodder, args *ast.Arguments, r *ast.Fodder, ctx pass.Context) {
shouldExpandBetween := false
shouldExpandNearParens := false
Expand Down Expand Up @@ -303,3 +319,39 @@ func (c *FixNewlines) Arguments(p pass.ASTPass, l *ast.Fodder, args *ast.Argumen
}
c.Base.Arguments(p, l, args, r, ctx)
}

// ObjectField eliminates newlines after the colon, before the expression
//
// Example:
//
// local d = {
// name:
//
// "foo",
// children:
// ["bar", "bam"],
// };
//
// should be formatted as:
//
// local d = {
// name: "foo",
// children: ["bar", "bam"],
// };
func (c *FixNewlines) ObjectField(p pass.ASTPass, field *ast.ObjectField, ctx pass.Context) {

switch field.Kind {
case ast.ObjectLocal:

case ast.ObjectFieldID:
removeInitialNewlines(field.Expr2)

case ast.ObjectFieldStr:

case ast.ObjectFieldExpr:

case ast.ObjectAssert:
}

c.Base.ObjectField(p, field, ctx)
}