Skip to content

Commit

Permalink
fix(template): fix template string escaping (#6705)
Browse files Browse the repository at this point in the history
A follow-up for #6685.
Replays #6408 on top of #6685.

Co-authored-by: Steffen Neubauer <[email protected]>
  • Loading branch information
vvagaytsev and stefreak authored Dec 10, 2024
1 parent 3c65e49 commit 32f8ec6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/template-string/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ TemplateStrings
}

FormatString
= EscapeStart SourceCharacter* FormatEndWithOptional {
= EscapeStart (!FormatEndWithOptional SourceCharacter)* FormatEndWithOptional {
if (options.unescape) {
return new ast.LiteralExpression(location(), text().slice(1))
} else {
Expand Down
54 changes: 54 additions & 0 deletions core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,60 @@ describe("resolveTemplateString", () => {
expect(res).to.equal("${bar}")
})

it("should resolve other template variables after escaped one with unescape=false", () => {
const res = resolveTemplateString({
string: "foo $${} ${bar}",
context: new GenericContext({ bar: "bar" }),
contextOpts: { unescape: false },
})
expect(res).to.equal("foo $${} bar")
})

it("should resolve other template variables after escaped one with unescape=true", () => {
const res = resolveTemplateString({
string: "foo $${} ${bar}",
context: new GenericContext({ bar: "bar" }),
contextOpts: { unescape: true },
})
expect(res).to.equal("foo ${} bar")
})

it("should not unescape inner template variables with unescape=false", () => {
const res = resolveTemplateString({
string: 'foo ${"$${}"} ${bar}',
context: new GenericContext({ bar: "bar" }),
contextOpts: { unescape: false },
})
expect(res).to.equal("foo $${} bar")
})

it("should unescape inner template variables with unescape=true", () => {
const res = resolveTemplateString({
string: 'foo ${"$${}"} ${bar}',
context: new GenericContext({ bar: "bar" }),
contextOpts: { unescape: true },
})
expect(res).to.equal("foo ${} bar")
})

it("should not unescape outer template variables with unescape=false", () => {
const res = resolveTemplateString({
string: 'foo $${"${}"} ${bar}',
context: new GenericContext({ bar: "bar" }),
contextOpts: { unescape: false },
})
expect(res).to.equal('foo $${"${}"} bar')
})

it("should unescape outer template variables with unescape=true", () => {
const res = resolveTemplateString({
string: 'foo $${"${}"} ${bar}',
context: new GenericContext({ bar: "bar" }),
contextOpts: { unescape: true },
})
expect(res).to.equal('foo ${"${}"} bar')
})

it("should unescape a template string with a double $$ prefix if allowPartial=false", () => {
const res = resolveTemplateString({
string: "$${bar}",
Expand Down

0 comments on commit 32f8ec6

Please sign in to comment.