Skip to content

Commit de17322

Browse files
committed
fix: preserve comments and whitespaces inside expressions
1 parent 9c4d6c7 commit de17322

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

rules/prefer-string-raw.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {isStringLiteral, isDirective} from './ast/index.js';
2-
import {fixSpaceAroundKeyword} from './fix/index.js';
2+
import {fixSpaceAroundKeyword, replaceTemplateElement} from './fix/index.js';
33

44
const MESSAGE_ID = 'prefer-string-raw';
55
const messages = {
@@ -67,11 +67,9 @@ const create = context => {
6767
return;
6868
}
6969

70-
let unescaped = '';
7170
let hasBackslash = false;
7271

73-
for (let index = 0; index < node.quasis.length; index++) {
74-
const quasi = node.quasis[index];
72+
for (const quasi of node.quasis) {
7573
const {raw, cooked} = quasi.value;
7674

7775
if (cooked.at(-1) === BACKSLASH) {
@@ -86,12 +84,6 @@ const create = context => {
8684
if (cooked.includes(BACKSLASH)) {
8785
hasBackslash = true;
8886
}
89-
90-
if (index > 0) {
91-
unescaped += '${' + context.sourceCode.getText(node.expressions[index - 1]) + '}';
92-
}
93-
94-
unescaped += unescapedQuasi;
9587
}
9688

9789
if (!hasBackslash) {
@@ -102,8 +94,9 @@ const create = context => {
10294
node,
10395
messageId: MESSAGE_ID,
10496
* fix(fixer) {
105-
yield fixer.replaceText(node, `String.raw\`${unescaped}\``);
10697
yield * fixSpaceAroundKeyword(fixer, node, context.sourceCode);
98+
yield * node.quasis.map(quasi => replaceTemplateElement(fixer, quasi, quasi.value.cooked));
99+
yield fixer.insertTextBefore(node, 'String.raw');
107100
},
108101
};
109102
});

test/prefer-string-raw.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/* eslint-disable no-template-curly-in-string */
23
import outdent from 'outdent';
34
import {getTester} from './utils/test.js';
@@ -88,6 +89,9 @@ test.snapshot({
8889
b\${bar}c
8990
d\\\\\\\\e\`
9091
`,
92+
'a = `a\\\\b${ foo /* bar */}c\\\\d`',
93+
'a = `a\\\\b${ foo + bar }`',
94+
'a = `${ foo .bar }a\\\\b`',
9195
],
9296
});
9397

test/snapshots/prefer-string-raw.js.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,66 @@ Generated by [AVA](https://avajs.dev).
295295
> 3 | d\\\\\\\\e\`␊
296296
| ^^^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
297297
`
298+
299+
## invalid(8): a = `a\\b${ foo /* bar */}c\\d`
300+
301+
> Input
302+
303+
`␊
304+
1 | a = \`a\\\\b${ foo /* bar */}c\\\\d\`␊
305+
`
306+
307+
> Output
308+
309+
`␊
310+
1 | a = String.raw\`a\\b${ foo /* bar */}c\\d\`␊
311+
`
312+
313+
> Error 1/1
314+
315+
`␊
316+
> 1 | a = \`a\\\\b${ foo /* bar */}c\\\\d\`␊
317+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
318+
`
319+
320+
## invalid(9): a = `a\\b${ foo + bar }`
321+
322+
> Input
323+
324+
`␊
325+
1 | a = \`a\\\\b${ foo + bar }\`␊
326+
`
327+
328+
> Output
329+
330+
`␊
331+
1 | a = String.raw\`a\\b${ foo + bar }\`␊
332+
`
333+
334+
> Error 1/1
335+
336+
`␊
337+
> 1 | a = \`a\\\\b${ foo + bar }\`␊
338+
| ^^^^^^^^^^^^^^^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
339+
`
340+
341+
## invalid(10): a = `${ foo .bar }a\\b`
342+
343+
> Input
344+
345+
`␊
346+
1 | a = \`${ foo .bar }a\\\\b\`␊
347+
`
348+
349+
> Output
350+
351+
`␊
352+
1 | a = String.raw\`${ foo .bar }a\\b\`␊
353+
`
354+
355+
> Error 1/1
356+
357+
`␊
358+
> 1 | a = \`${ foo .bar }a\\\\b\`␊
359+
| ^^^^^^^^^^^^^^^^^^^ \`String.raw\` should be used to avoid escaping \`\\\`.␊
360+
`
136 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)