Skip to content

Commit be1ece7

Browse files
committed
fix: ASI issue
1 parent ae37793 commit be1ece7

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

rules/prefer-string-raw.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {isStringLiteral, isDirective, isMemberExpression} from './ast/index.js';
22
import {fixSpaceAroundKeyword} from './fix/index.js';
3+
import needsSemicolon from './utils/needs-semicolon.js';
34

45
const MESSAGE_ID = 'prefer-string-raw';
56
const MESSAGE_ID_UNNECESSARY_STRING_RAW = 'unnecessary-string-raw';
@@ -69,6 +70,7 @@ const create = context => {
6970

7071
context.on('TaggedTemplateExpression', node => {
7172
const {quasi, tag} = node;
73+
const {sourceCode} = context;
7274

7375
if (!isMemberExpression(tag, {object: 'String', property: 'raw'})) {
7476
return;
@@ -82,7 +84,7 @@ const create = context => {
8284
return;
8385
}
8486

85-
const rawQuasi = context.sourceCode.getText(quasi);
87+
const rawQuasi = sourceCode.getText(quasi);
8688
const suggestion = quasi.expressions.length > 0 || /\r?\n/.test(rawQuasi)
8789
? rawQuasi
8890
: `'${rawQuasi.slice(1, -1).replaceAll('\'', String.raw`\'`)}'`;
@@ -91,6 +93,11 @@ const create = context => {
9193
node,
9294
messageId: MESSAGE_ID_UNNECESSARY_STRING_RAW,
9395
* fix(fixer) {
96+
const tokenBefore = sourceCode.getTokenBefore(node);
97+
if (needsSemicolon(tokenBefore, sourceCode, suggestion)) {
98+
yield fixer.insertTextAfter(tokenBefore, ';');
99+
}
100+
94101
yield fixer.replaceText(node, suggestion);
95102
},
96103
};

test/prefer-string-raw.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ test.snapshot({
8282
a\${foo}b
8383
\${bar}cd\`
8484
`,
85+
outdent`
86+
a
87+
String.raw\`abc\`
88+
`,
89+
// ASI
90+
outdent`
91+
a
92+
String.raw\`a\${b}\`
93+
`,
8594
],
8695
});
8796

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,51 @@ Generated by [AVA](https://avajs.dev).
317317
> 3 | ${bar}cd\`␊
318318
| ^^^^^^^^^^ Using \`String.raw\` is unnecessary as the string does not contain any \`\\\`.␊
319319
`
320+
321+
## invalid(9): a String.raw`abc`
322+
323+
> Input
324+
325+
`␊
326+
1 | a␊
327+
2 | String.raw\`abc\`␊
328+
`
329+
330+
> Output
331+
332+
`␊
333+
1 | a␊
334+
2 | 'abc'␊
335+
`
336+
337+
> Error 1/1
338+
339+
`␊
340+
1 | a␊
341+
> 2 | String.raw\`abc\`␊
342+
| ^^^^^^^^^^^^^^^ Using \`String.raw\` is unnecessary as the string does not contain any \`\\\`.␊
343+
`
344+
345+
## invalid(10): a String.raw`a${b}`
346+
347+
> Input
348+
349+
`␊
350+
1 | a␊
351+
2 | String.raw\`a${b}\`␊
352+
`
353+
354+
> Output
355+
356+
`␊
357+
1 | a;␊
358+
2 | \`a${b}\`␊
359+
`
360+
361+
> Error 1/1
362+
363+
`␊
364+
1 | a␊
365+
> 2 | String.raw\`a${b}\`␊
366+
| ^^^^^^^^^^^^^^^^^ Using \`String.raw\` is unnecessary as the string does not contain any \`\\\`.␊
367+
`
76 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)