Skip to content

Commit 24ff9cf

Browse files
authored
fix: return the raw strings when formatting (#7525)
prevent `\u0000` from being changed to `\x00` Signed-off-by: sspaink <[email protected]>
1 parent 254f3bf commit 24ff9cf

8 files changed

+124
-3
lines changed

v1/format/format.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -1207,10 +1207,29 @@ func (w *writer) writeTermParens(parens bool, term *ast.Term, comments []*ast.Co
12071207
case ast.String:
12081208
if term.Location.Text[0] == '`' {
12091209
// To preserve raw strings, we need to output the original text,
1210-
// not what x.String() would give us.
12111210
w.write(string(term.Location.Text))
12121211
} else {
1213-
w.write(x.String())
1212+
// x.String() cannot be used by default because it can change the input string "\u0000" to "\x00"
1213+
var after, quote string
1214+
var found bool
1215+
// term.Location.Text could contain the prefix `else :=`, remove it
1216+
switch term.Location.Text[len(term.Location.Text)-1] {
1217+
case '"':
1218+
quote = "\""
1219+
_, after, found = strings.Cut(string(term.Location.Text), quote)
1220+
case '`':
1221+
quote = "`"
1222+
_, after, found = strings.Cut(string(term.Location.Text), quote)
1223+
}
1224+
1225+
if !found {
1226+
// If no quoted string was found, that means it is a key being formatted to a string
1227+
// e.g. partial_set.y to partial_set["y"]
1228+
w.write(x.String())
1229+
} else {
1230+
w.write(quote + after)
1231+
}
1232+
12141233
}
12151234
case ast.Var:
12161235
w.write(w.formatVar(x))

v1/format/format_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestFormatNilLocationEmptyBody(t *testing.T) {
6969
func TestFormatNilLocationFunctionArgs(t *testing.T) {
7070
b := ast.NewBody()
7171
s := ast.StringTerm(" ")
72-
s.SetLocation(location.NewLocation([]byte("foo"), "p.rego", 2, 2))
72+
s.SetLocation(location.NewLocation([]byte("\" \""), "p.rego", 2, 2))
7373
b.Append(ast.Split.Expr(ast.NewTerm(ast.Var("__local1__")), s, ast.NewTerm(ast.Var("__local2__"))))
7474
exp := "split(__local1__, \" \", __local2__)\n"
7575
bs, err := Ast(b)
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
x := "\u0000"
4+
x := "\u0000 \""
5+
6+
_fg := {
7+
"black": "\u001b[30m",
8+
"red": "\u001b[31m",
9+
"green": "\u001b[32m",
10+
"yellow": "\u001b[33m",
11+
"blue": "\u001b[34m",
12+
"magenta": "\u001b[35m",
13+
"cyan": "\u001b[36m",
14+
"white": "\u001b[37m",
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test
2+
3+
x := "\u0000"
4+
5+
x := "\u0000 \""
6+
7+
_fg := {
8+
"black": "\u001b[30m",
9+
"red": "\u001b[31m",
10+
"green": "\u001b[32m",
11+
"yellow": "\u001b[33m",
12+
"blue": "\u001b[34m",
13+
"magenta": "\u001b[35m",
14+
"cyan": "\u001b[36m",
15+
"white": "\u001b[37m",
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test
2+
3+
a := "foo" if {
4+
false
5+
} else := `{"foo":"bar"}`
6+
7+
8+
a := "foo" if {
9+
false
10+
} else := "`{\"foo\":\"bar\"}`"
11+
12+
a := "foo" if {
13+
false
14+
} else := "foo"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test
2+
3+
a := "foo" if {
4+
false
5+
} else := `{"foo":"bar"}`
6+
7+
a := "foo" if {
8+
false
9+
} else := "`{\"foo\":\"bar\"}`"
10+
11+
a := "foo" if {
12+
false
13+
} else := "foo"
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test
2+
3+
x := "\u0000"
4+
x := "\u0000 \""
5+
6+
authorize = "\u0000" if {
7+
input.user == "\u0000"
8+
} else = "\u0000" if {
9+
input.path[0] == "\u0000"
10+
input.source_network == "\u0000"
11+
}
12+
13+
_fg := {
14+
"black": "\u001b[30m",
15+
"red": "\u001b[31m",
16+
"green": "\u001b[32m",
17+
"yellow": "\u001b[33m",
18+
"blue": "\u001b[34m",
19+
"magenta": "\u001b[35m",
20+
"cyan": "\u001b[36m",
21+
"white": "\u001b[37m",
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test
2+
3+
x := "\u0000"
4+
x := "\u0000 \""
5+
6+
authorize := "\u0000" if {
7+
input.user == "\u0000"
8+
} else := "\u0000" if {
9+
input.path[0] == "\u0000"
10+
input.source_network == "\u0000"
11+
}
12+
13+
_fg := {
14+
"black": "\u001b[30m",
15+
"red": "\u001b[31m",
16+
"green": "\u001b[32m",
17+
"yellow": "\u001b[33m",
18+
"blue": "\u001b[34m",
19+
"magenta": "\u001b[35m",
20+
"cyan": "\u001b[36m",
21+
"white": "\u001b[37m",
22+
}

0 commit comments

Comments
 (0)