Skip to content

Commit 80ce6ac

Browse files
sbarzowskisparkprime
authored andcommitted
Don't escape block strings (google#98)
* Don't escape block strings
1 parent c345915 commit 80ce6ac

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

ast/ast.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@ const (
416416
VerbatimStringSingle
417417
)
418418

419+
func (k LiteralStringKind) FullyEscaped() bool {
420+
switch k {
421+
case StringSingle, StringDouble:
422+
return true
423+
case StringBlock, VerbatimStringDouble, VerbatimStringSingle:
424+
return false
425+
}
426+
panic(fmt.Sprintf("Unknown string kind: %v", k))
427+
}
428+
419429
// LiteralString represents a JSON string
420430
type LiteralString struct {
421431
NodeBase

desugarer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,15 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
502502
// Nothing to do.
503503

504504
case *ast.LiteralString:
505-
if node.Kind != ast.VerbatimStringDouble && node.Kind != ast.VerbatimStringSingle {
505+
if node.Kind.FullyEscaped() {
506506
unescaped, err := stringUnescape(node.Loc(), node.Value)
507507
if err != nil {
508508
return err
509509
}
510510
node.Value = unescaped
511-
node.Kind = ast.StringDouble
512-
node.BlockIndent = ""
513511
}
512+
node.Kind = ast.StringDouble
513+
node.BlockIndent = ""
514514
case *ast.Object:
515515
// Hidden variable to allow $ binding.
516516
if objLevel == 0 {

testdata/block_escaping.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"\\n\n\\t\n"

testdata/block_escaping.jsonnet

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
|||
2+
\n
3+
\t
4+
|||

0 commit comments

Comments
 (0)