From d45d78086934740645a1905ac9b8c56fd484383a Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Sat, 3 Dec 2016 10:16:41 -0800 Subject: [PATCH] Improve tests and comments --- src/nodes.coffee | 4 ++-- test/tagged_template_literals.coffee | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/nodes.coffee b/src/nodes.coffee index 71e32a02..962b5803 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -2274,8 +2274,8 @@ exports.StringWithInterpolations = class StringWithInterpolations extends Parens fragments.push @makeCode '`' for element in elements if element instanceof StringLiteral - value = element.value.slice(1, -1) - # Backticks or `${` inside template literals must be escaped + value = element.value[1...-1] + # Backticks and `${` inside template literals must be escaped. value = value.replace /(\\*)(`|\$\{)/g, (match, backslashes, toBeEscaped) -> if backslashes.length % 2 is 0 "#{backslashes}\\#{toBeEscaped}" diff --git a/test/tagged_template_literals.coffee b/test/tagged_template_literals.coffee index a5163f2a..92cccf5a 100644 --- a/test/tagged_template_literals.coffee +++ b/test/tagged_template_literals.coffee @@ -146,7 +146,7 @@ test "tagged template literal with escaped backticks", -> eq 'text: [ES template literals look like this: \\`foo bar\\`] expressions: []', func"ES template literals look like this: \\`foo bar\\`" -test "tagged template literal with invalidly escaped backticks", -> +test "tagged template literal with unnecessarily escaped backticks", -> eq 'text: [ES template literals look like this: `foo bar`] expressions: []', func"ES template literals look like this: \`foo bar\`" @@ -162,7 +162,10 @@ test "tagged template literal with escaped ES interpolation", -> eq 'text: [ES template literals also look like this: `3 + 5 = \\${3+5}`] expressions: []', func"ES template literals also look like this: `3 + 5 = \\${3+5}`" -test "tagged template literal with invalidly escaped ES interpolation", -> - eq 'text: [ES template literals also look like this: `3 + 5 = \${3+5}`] expressions: []', +test "tagged template literal with unnecessarily escaped ES interpolation", -> + eq 'text: [ES template literals also look like this: `3 + 5 = ${3+5}`] expressions: []', func"ES template literals also look like this: `3 + 5 = \${3+5}`" +test "tagged template literal special escaping", -> + eq 'text: [` ` \\` \\` \\\\` $ { ${ ${ \\${ \\${ \\\\${ | ` ${] expressions: [1]', + func"` \` \\` \\\` \\\\` $ { ${ \${ \\${ \\\${ \\\\${ #{1} ` ${"