fix(compiler-ssr): should escape template string interpolation chars in generated code

This commit is contained in:
Evan You 2020-05-26 14:27:01 -04:00
parent 181541045b
commit 5f15d9aa4b
2 changed files with 16 additions and 1 deletions

View File

@ -891,7 +891,7 @@ function genTemplateLiteral(node: TemplateLiteral, context: CodegenContext) {
for (let i = 0; i < l; i++) {
const e = node.elements[i]
if (isString(e)) {
push(e.replace(/`/g, '\\`'))
push(e.replace(/(`|\$|\\)/g, '\\$1'))
} else {
push('${')
if (multilines) indent()

View File

@ -6,6 +6,21 @@ describe('ssr: text', () => {
expect(getCompiledString(`foo`)).toMatchInlineSnapshot(`"\`foo\`"`)
})
test('static text with template string special chars', () => {
expect(getCompiledString(`\`\${foo}\``)).toMatchInlineSnapshot(
`"\`\\\\\`\\\\\${foo}\\\\\`\`"`
)
})
test('static text with char escape', () => {
// the desired generated code should be `\\\$foo`
// snapshot -> inline snapshot goes through two escapes
// so that makes a total of 3 * 2 * 2 = 12 back slashes
expect(getCompiledString(`\\$foo`)).toMatchInlineSnapshot(
`"\`\\\\\\\\\\\\$foo\`"`
)
})
test('comments', () => {
expect(getCompiledString(`<!--bar-->`)).toMatchInlineSnapshot(
`"\`<!--bar-->\`"`