-
-
Notifications
You must be signed in to change notification settings - Fork 422
prefer-string-raw: Add support for template literals
#2691
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
prefer-string-raw: Add support for template literals
#2691
Conversation
28f9147 to
b6932a5
Compare
b6932a5 to
b36a7a9
Compare
57d18d8 to
ea61482
Compare
1686254 to
8bbb723
Compare
8bbb723 to
3de2032
Compare
de17322 to
30ed709
Compare
0b09f48 to
6d81306
Compare
|
I'm a little worried about how |
Is this comment for this PR or the other one? |
|
This one. |
Ok, I'm not sure if I follow, will If the string contains |
As far as I know, it's not really clear in ESTree spec. |
|
@fisker Looked into this a bit, here are my observations: With The only thing is that in some situations we won't be preserving carriage returns, like this: test.babel({
valid: [],
invalid: [
// eslint-disable-next-line internal/no-test-only
test.only({
code: 'a = `a\\\\b \r\n`',
output: 'a = String.raw`a\\b \r\n`',
errors: 1,
}),
],
});Is this a problem? With the [
{
cooked: 'a\\b \n',
raw: 'a\\\\b \r\n',
unescapeBackslash: 'a\\b \r\n'
}
]Maybe we can fix this by replacing all function normalizeCarriageReturn(text) {
return text.replaceAll(/\r\n?/g, '\n');
}if (
(node.parent.type === 'TaggedTemplateExpression' && node.parent.quasi === node)
- || node.quasis.every(({value: {cooked, raw}}) => cooked === raw)
+ || node.quasis.every(({value: {cooked, raw}}) => cooked === normalizeCarriageReturn(raw))
- || node.quasis.some(({value: {cooked, raw}}) => cooked.at(-1) === BACKSLASH || unescapeBackslash(raw) !== cooked)
+ || node.quasis.some(({value: {cooked, raw}}) => cooked.at(-1) === BACKSLASH || unescapeBackslash(normalizeCarriageReturn(raw)) !== cooked)
) {
return;
}This would make cases like these be reported, but the carriage return would still not be preserved. So, you would get WDYT? |
|
I prefer just ignore it, if user complain, we can just blame typescript-eslint 😄 |
Agreed |
|
Approve? |

Closes #2653