Don't escape block strings (#98)

* Don't escape block strings
This commit is contained in:
Stanisław Barzowski 2017-10-03 14:29:12 -04:00 committed by Dave Cunningham
parent c3459153df
commit 80ce6ac112
4 changed files with 18 additions and 3 deletions

View File

@ -416,6 +416,16 @@ const (
VerbatimStringSingle
)
func (k LiteralStringKind) FullyEscaped() bool {
switch k {
case StringSingle, StringDouble:
return true
case StringBlock, VerbatimStringDouble, VerbatimStringSingle:
return false
}
panic(fmt.Sprintf("Unknown string kind: %v", k))
}
// LiteralString represents a JSON string
type LiteralString struct {
NodeBase

View File

@ -502,15 +502,15 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
// Nothing to do.
case *ast.LiteralString:
if node.Kind != ast.VerbatimStringDouble && node.Kind != ast.VerbatimStringSingle {
if node.Kind.FullyEscaped() {
unescaped, err := stringUnescape(node.Loc(), node.Value)
if err != nil {
return err
}
node.Value = unescaped
node.Kind = ast.StringDouble
node.BlockIndent = ""
}
node.Kind = ast.StringDouble
node.BlockIndent = ""
case *ast.Object:
// Hidden variable to allow $ binding.
if objLevel == 0 {

1
testdata/block_escaping.golden vendored Normal file
View File

@ -0,0 +1 @@
"\\n\n\\t\n"

4
testdata/block_escaping.jsonnet vendored Normal file
View File

@ -0,0 +1,4 @@
|||
\n
\t
|||