Skip to content

Commit

Permalink
Merge branch 'main' into wmalik-tmc-previews-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
wmalik authored Mar 1, 2024
2 parents 5ce6b9e + 52780d8 commit 00aef9b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:

### Fixed

- Fix a panic in language server with a project caontaining errors on root directory
- Fix a panic in language server with a project containing errors on root directory
- Fix the execution order when using `tag` filter in `after/before` in conjunction with implicit order for nested stacks. (BREAKING CHANGE)
- Fix escape sequences being interpreted in heredocs (issue #1449)

## v0.4.6

Expand Down
63 changes: 36 additions & 27 deletions hcl/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package ast

import (
"fmt"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/ext/customdecode"
Expand Down Expand Up @@ -449,32 +448,42 @@ func isHeredoc(bytes []byte) bool {
}

func renderString(bytes []byte) []byte {
type replace struct {
old string
new string
}
str := string(bytes)
for _, r := range []replace{
{
old: "\\\\",
new: "\\",
},
{
old: "\\t",
new: "\t",
},
{
old: "\\n",
new: "\n",
},
{
old: `\"`,
new: `"`,
},
} {
str = strings.ReplaceAll(str, r.old, r.new)
}
return []byte(str)
length := len(bytes)
if length < 2 {
return bytes
}
// a new slice is needed to avoid modifying the original bytes.
out := make([]byte, 0, length)
for i := 0; i < length; i++ {
r := bytes[i]
if r != '\\' {
out = append(out, r)
continue
}

if i == length-1 {
out = append(out, '\\')
break
}

i++
r = bytes[i]

switch r {
default:
out = append(out, '\\')
out = append(out, r)
case '\\':
out = append(out, '\\')
case 'n':
out = append(out, '\n')
case 't':
out = append(out, '\t')
case '"':
out = append(out, '"')
}
}
return out
}

func obrace() *hclwrite.Token {
Expand Down
36 changes: 36 additions & 0 deletions hcl/ast/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ EOT
name: "oneline heredocs",
expr: `<<-EOT
EOT
`,
},
{
name: "heredocs with newlines",
expr: `<<-EOT
Line 1
Line 2. I want the following \ and n to be in the file: \n
Line 3
EOT
`,
},
Expand Down Expand Up @@ -180,6 +189,11 @@ EOT
expr: `"test\u1000"`,
want: `"testက"`,
},
{
name: "rendered printable unicodes are also rendered when in quoted string",
expr: `"testက"`,
want: `"testက"`,
},
{
name: "single nl returns heredocs",
expr: `"\n"`,
Expand Down Expand Up @@ -235,6 +249,10 @@ EOT
name: "strings with nl but not ending with nl returns plain string",
expr: `"${a}\ntest\n${global.a}"`,
},
{
name: "escaping carriege returns",
expr: `"str \r str"`,
},
{
name: "render string when generating heredoc",
expr: `"\t${a}\n\ttest\n\t${global.a}\n"`,
Expand All @@ -249,6 +267,24 @@ EOT
expr: `"\n${a}${b}\t${b}\n\ntest\n\t${global.a}\n"`,
want: "<<-EOT\n\n${a}${b}\t${b}\n\ntest\n\t${global.a}\nEOT\n",
},
{
name: "render quotes",
expr: `"this is a \"test\" string"`,
},
{
name: "render quotes in heredocs",
expr: `<<-EOT
this is a "test" string
EOT
`,
},
{
name: "render escaped quotes in heredocs",
expr: `<<-EOT
this is a \"test\" string
EOT
`,
},
{
name: "utf-8",
expr: `"伊亜希"`,
Expand Down

0 comments on commit 00aef9b

Please sign in to comment.