Skip to content

Commit cc0a128

Browse files
committed
fix: preserve comments after String.raw
1 parent ee17e72 commit cc0a128

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

rules/prefer-string-raw.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ const create = context => {
9898
yield fixer.insertTextBefore(node, ';');
9999
}
100100

101-
yield fixer.replaceText(node, suggestion);
101+
yield fixer.replaceText(node.quasi, suggestion);
102+
yield fixer.remove(node.tag);
102103
},
103104
};
104105
});

test/prefer-string-raw.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ test.snapshot({
6464
b\\c
6565
de\`
6666
`,
67+
outdent`
68+
a = String.raw
69+
// Comment
70+
\`ab\\c\`
71+
`,
6772
],
6873
invalid: [
6974
'a = String.raw`abc`',
@@ -91,6 +96,16 @@ test.snapshot({
9196
a
9297
String.raw\`a\${b}\`
9398
`,
99+
outdent`
100+
a = String.raw
101+
// Comment
102+
\`ab
103+
c\`
104+
`,
105+
outdent`
106+
a = String.raw /* comment */ \`ab
107+
c\`
108+
`,
94109
],
95110
});
96111

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,61 @@ Generated by [AVA](https://avajs.dev).
365365
> 2 | String.raw\`a${b}\`␊
366366
| ^^^^^^^^^^^^^^^^^ Using \`String.raw\` is unnecessary as the string does not contain any \`\\\`.␊
367367
`
368+
369+
## invalid(11): a = String.raw // Comment `ab c`
370+
371+
> Input
372+
373+
`␊
374+
1 | a = String.raw␊
375+
2 | // Comment␊
376+
3 | \`ab␊
377+
4 | c\`␊
378+
`
379+
380+
> Output
381+
382+
`␊
383+
1 | a = ␊
384+
2 | // Comment␊
385+
3 | \`ab␊
386+
4 | c\`␊
387+
`
388+
389+
> Error 1/1
390+
391+
`␊
392+
> 1 | a = String.raw␊
393+
| ^^^^^^^^^^␊
394+
> 2 | // Comment␊
395+
| ^^^^^^^^^^␊
396+
> 3 | \`ab␊
397+
| ^^^^^^^^^^␊
398+
> 4 | c\`␊
399+
| ^^^ Using \`String.raw\` is unnecessary as the string does not contain any \`\\\`.␊
400+
`
401+
402+
## invalid(12): a = String.raw /* comment */ `ab c`
403+
404+
> Input
405+
406+
`␊
407+
1 | a = String.raw /* comment */ \`ab␊
408+
2 | c\`␊
409+
`
410+
411+
> Output
412+
413+
`␊
414+
1 | a = /* comment */ \`ab␊
415+
2 | c\`␊
416+
`
417+
418+
> Error 1/1
419+
420+
`␊
421+
> 1 | a = String.raw /* comment */ \`ab␊
422+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^␊
423+
> 2 | c\`␊
424+
| ^^^ Using \`String.raw\` is unnecessary as the string does not contain any \`\\\`.␊
425+
`
126 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)