Skip to content

Commit 4fef1d3

Browse files
authored
fix: escape string and template literal (#8)
1 parent f9fa194 commit 4fef1d3

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

src/commands/to-string-literal.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ run(
99
const a = \`a\`; const b = \`b\`; const c = 'c';
1010
`,
1111
output: d`
12-
const a = 'a'; const b = 'b'; const c = 'c';
12+
const a = "a"; const b = "b"; const c = 'c';
1313
`,
1414
errors: ['command-removal', 'command-fix', 'command-fix'],
1515
},
@@ -20,7 +20,7 @@ run(
2020
const a = \`a\`, b = \`b\`, c = \`c\`, d = \`d\`;
2121
`,
2222
output: d`
23-
const a = \`a\`, b = 'b', c = 'c', d = \`d\`;
23+
const a = \`a\`, b = "b", c = "c", d = \`d\`;
2424
`,
2525
errors: ['command-removal', 'command-fix', 'command-fix'],
2626
},
@@ -31,7 +31,7 @@ run(
3131
const a = 'a', b = 'b', c = \`c\`, d = 'd', e = \`e\`, f = \`f\`;
3232
`,
3333
output: d`
34-
const a = 'a', b = 'b', c = 'c', d = 'd', e = \`e\`, f = 'f';
34+
const a = 'a', b = 'b', c = "c", d = 'd', e = \`e\`, f = "f";
3535
`,
3636
errors: ['command-removal', 'command-fix', 'command-fix'],
3737
},
@@ -42,7 +42,18 @@ run(
4242
const a = \`\${g}a\${a}a\${b}c\${d}e\${a}\`;
4343
`,
4444
output: d`
45-
const a = g + 'a' + a + 'a' + b + 'c' + d + 'e' + a;
45+
const a = g + "a" + a + "a" + b + "c" + d + "e" + a;
46+
`,
47+
errors: ['command-removal', 'command-fix'],
48+
},
49+
// escape
50+
{
51+
code: d`
52+
// @2sl
53+
const a = \`"\\"\\\\"\`
54+
`,
55+
output: d`
56+
const a = "\\"\\\\\\"\\\\\\\\\\""
4657
`,
4758
errors: ['command-removal', 'command-fix'],
4859
},

src/commands/to-string-literal.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export const toStringLiteral: Command = {
1919
ctx.removeComment()
2020
for (const node of getNodesByIndexes(nodes, indexes)) {
2121
const ids = extractIdentifiers(node)
22-
let raw = ctx.source.getText(node).slice(1, -1)
22+
let raw = JSON.stringify(ctx.source.getText(node).slice(1, -1)).slice(1, -1)
2323

2424
if (ids.length)
2525
raw = toStringWithIds(raw, node, ids)
2626
else
27-
raw = `'${raw}'`
27+
raw = `"${raw}"`
2828

2929
ctx.report({
3030
node,
@@ -56,8 +56,8 @@ function toStringWithIds(raw: string, node: Tree.TemplateLiteral, ids: Identifie
5656
let hasStart = false
5757
let hasEnd = false
5858
ids.forEach(({ name, range }, index) => {
59-
let startStr = `' + `
60-
let endStr = ` + '`
59+
let startStr = `" + `
60+
let endStr = ` + "`
6161

6262
if (index === 0) {
6363
hasStart = range[0] - /* `${ */3 === node.range[0]
@@ -72,5 +72,5 @@ function toStringWithIds(raw: string, node: Tree.TemplateLiteral, ids: Identifie
7272

7373
raw = raw.replace(`\${${name}}`, `${startStr}${name}${endStr}`)
7474
})
75-
return `${hasStart ? '' : `'`}${raw}${hasEnd ? '' : `'`}`
75+
return `${hasStart ? '' : `"`}${raw}${hasEnd ? '' : `"`}`
7676
}

src/commands/to-template-literal.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,35 @@ run(
8686
`,
8787
errors: ['command-removal', 'command-fix', 'command-fix'],
8888
},
89+
// escape
90+
{
91+
code: d`
92+
// @2tl
93+
const a = "\`"
94+
`,
95+
output: d`
96+
const a = \`\\\`\`
97+
`,
98+
errors: ['command-removal', 'command-fix'],
99+
},
100+
{
101+
code: d`
102+
// @2tl
103+
const a = str + "\`"
104+
`,
105+
output: d`
106+
const a = \`\${str}\\\`\`
107+
`,
108+
errors: ['command-removal', 'command-fix'],
109+
},
110+
{
111+
code: d`
112+
// @2tl
113+
const a = "\${str}"
114+
`,
115+
output: d`
116+
const a = \`\\\${str}\`
117+
`,
118+
errors: ['command-removal', 'command-fix'],
119+
},
89120
)

src/commands/to-template-literal.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function getExpressionValue(node: Tree.Expression | Tree.PrivateIdentifier) {
5151
if (node.type === 'Identifier')
5252
return `\${${node.name}}`
5353
if (node.type === 'Literal' && typeof node.value === 'string')
54-
return node.value
54+
return escape(node.value)
5555

5656
return ''
5757
}
@@ -76,7 +76,7 @@ function traverseBinaryExpression(node: Tree.BinaryExpression): string {
7676
}
7777

7878
function convertStringLiteral(node: Tree.StringLiteral, ctx: CommandContext) {
79-
const raw = `\`${node.value}\``
79+
const raw = `\`${escape(node.value)}\``
8080
report(ctx, node, raw)
8181
}
8282

@@ -89,3 +89,8 @@ function report(ctx: CommandContext, node: Tree.Node, raw: string) {
8989
},
9090
})
9191
}
92+
93+
function escape(raw: string) {
94+
// TODO handle multi escape characters '\\${str}'
95+
return raw.replace(/`/g, '\\`').replace(/\$\{/g, '\\${')
96+
}

0 commit comments

Comments
 (0)