Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape string and template literal #8

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/commands/to-string-literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ run(
const a = \`a\`; const b = \`b\`; const c = 'c';
`,
output: d`
const a = 'a'; const b = 'b'; const c = 'c';
const a = "a"; const b = "b"; const c = 'c';
`,
errors: ['command-removal', 'command-fix', 'command-fix'],
},
Expand All @@ -20,7 +20,7 @@ run(
const a = \`a\`, b = \`b\`, c = \`c\`, d = \`d\`;
`,
output: d`
const a = \`a\`, b = 'b', c = 'c', d = \`d\`;
const a = \`a\`, b = "b", c = "c", d = \`d\`;
`,
errors: ['command-removal', 'command-fix', 'command-fix'],
},
Expand All @@ -31,7 +31,7 @@ run(
const a = 'a', b = 'b', c = \`c\`, d = 'd', e = \`e\`, f = \`f\`;
`,
output: d`
const a = 'a', b = 'b', c = 'c', d = 'd', e = \`e\`, f = 'f';
const a = 'a', b = 'b', c = "c", d = 'd', e = \`e\`, f = "f";
`,
errors: ['command-removal', 'command-fix', 'command-fix'],
},
Expand All @@ -42,7 +42,18 @@ run(
const a = \`\${g}a\${a}a\${b}c\${d}e\${a}\`;
`,
output: d`
const a = g + 'a' + a + 'a' + b + 'c' + d + 'e' + a;
const a = g + "a" + a + "a" + b + "c" + d + "e" + a;
`,
errors: ['command-removal', 'command-fix'],
},
// escape
{
code: d`
// @2sl
const a = \`"\\"\\\\"\`
`,
output: d`
const a = "\\"\\\\\\"\\\\\\\\\\""
`,
errors: ['command-removal', 'command-fix'],
},
Expand Down
10 changes: 5 additions & 5 deletions src/commands/to-string-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export const toStringLiteral: Command = {
ctx.removeComment()
for (const node of getNodesByIndexes(nodes, indexes)) {
const ids = extractIdentifiers(node)
let raw = ctx.source.getText(node).slice(1, -1)
let raw = JSON.stringify(ctx.source.getText(node).slice(1, -1)).slice(1, -1)

if (ids.length)
raw = toStringWithIds(raw, node, ids)
else
raw = `'${raw}'`
raw = `"${raw}"`

ctx.report({
node,
Expand Down Expand Up @@ -56,8 +56,8 @@ function toStringWithIds(raw: string, node: Tree.TemplateLiteral, ids: Identifie
let hasStart = false
let hasEnd = false
ids.forEach(({ name, range }, index) => {
let startStr = `' + `
let endStr = ` + '`
let startStr = `" + `
let endStr = ` + "`

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

raw = raw.replace(`\${${name}}`, `${startStr}${name}${endStr}`)
})
return `${hasStart ? '' : `'`}${raw}${hasEnd ? '' : `'`}`
return `${hasStart ? '' : `"`}${raw}${hasEnd ? '' : `"`}`
}
31 changes: 31 additions & 0 deletions src/commands/to-template-literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,35 @@ run(
`,
errors: ['command-removal', 'command-fix', 'command-fix'],
},
// escape
{
code: d`
// @2tl
const a = "\`"
`,
output: d`
const a = \`\\\`\`
`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
// @2tl
const a = str + "\`"
`,
output: d`
const a = \`\${str}\\\`\`
`,
errors: ['command-removal', 'command-fix'],
},
{
code: d`
// @2tl
const a = "\${str}"
`,
output: d`
const a = \`\\\${str}\`
`,
errors: ['command-removal', 'command-fix'],
},
)
9 changes: 7 additions & 2 deletions src/commands/to-template-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function getExpressionValue(node: Tree.Expression | Tree.PrivateIdentifier) {
if (node.type === 'Identifier')
return `\${${node.name}}`
if (node.type === 'Literal' && typeof node.value === 'string')
return node.value
return escape(node.value)

return ''
}
Expand All @@ -76,7 +76,7 @@ function traverseBinaryExpression(node: Tree.BinaryExpression): string {
}

function convertStringLiteral(node: Tree.StringLiteral, ctx: CommandContext) {
const raw = `\`${node.value}\``
const raw = `\`${escape(node.value)}\``
report(ctx, node, raw)
}

Expand All @@ -89,3 +89,8 @@ function report(ctx: CommandContext, node: Tree.Node, raw: string) {
},
})
}

function escape(raw: string) {
// TODO handle multi escape characters '\\${str}'
return raw.replace(/`/g, '\\`').replace(/\$\{/g, '\\${')
}