Skip to content

Commit

Permalink
fix: escape
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 3, 2024
1 parent 37cb05a commit 8a9037d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 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,18 +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 = \`'\\'\`
const a = \`"\\"\\\\"\`
`,
output: d`
const a = '\\'\\''
const a = "\\"\\\\\\"\\\\\\\\\\""
`,
errors: ['command-removal', 'command-fix'],
},
Expand Down
8 changes: 4 additions & 4 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).replace(/(?<!\\)'/g, `\\'`)
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 Down

0 comments on commit 8a9037d

Please sign in to comment.