-
-
Notifications
You must be signed in to change notification settings - Fork 413
prefer-string-raw
: Forbid unnecessary String.raw
#2695
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
e36341f
494361d
8b2bc0b
ae37793
be1ece7
15781f5
ee17e72
cc0a128
ffcc738
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ import {isStringLiteral, isDirective} from './ast/index.js'; | |
import {fixSpaceAroundKeyword} from './fix/index.js'; | ||
|
||
const MESSAGE_ID = 'prefer-string-raw'; | ||
const MESSAGE_ID_UNNECESSARY_STRING_RAW = 'unnecessary-string-raw'; | ||
const messages = { | ||
[MESSAGE_ID]: '`String.raw` should be used to avoid escaping `\\`.', | ||
[MESSAGE_ID_UNNECESSARY_STRING_RAW]: 'Using `String.raw` is unnecessary as the string does not contain any `\\`.', | ||
}; | ||
|
||
const BACKSLASH = '\\'; | ||
|
@@ -64,6 +66,40 @@ const create = context => { | |
}, | ||
}; | ||
}); | ||
|
||
context.on('TaggedTemplateExpression', node => { | ||
const {quasi, tag} = node; | ||
|
||
if (tag.type !== 'MemberExpression' | ||
|| tag.object.type !== 'Identifier' | ||
|| tag.property.type !== 'Identifier' | ||
|| tag.object.name !== 'String' | ||
|| tag.property.name !== 'raw' | ||
) { | ||
return; | ||
} | ||
|
||
const hasBackslash = quasi.quasis.some( | ||
quasi => quasi.value.raw.includes(BACKSLASH), | ||
); | ||
|
||
if (hasBackslash) { | ||
return; | ||
} | ||
|
||
const rawQuasi = context.sourceCode.getText(quasi); | ||
const suggestion = quasi.expressions.length > 0 || /\r?\n/.test(rawQuasi) | ||
? rawQuasi | ||
: `'${rawQuasi.slice(1, -1).replaceAll('\'', String.raw`\'`)}'`; | ||
fisker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
node, | ||
som-sm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
messageId: MESSAGE_ID_UNNECESSARY_STRING_RAW, | ||
* fix(fixer) { | ||
yield fixer.replaceText(node, suggestion); | ||
fisker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need check this.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I didn't get this. The following tests already work: valid: [
'a = String.raw`\\u0040`'
] invalid: [
'a = String.raw`\u0040`'
] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I thought we suggest Anyway, do you think we can check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, we can, updated in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above update, surfaced an interesting issue. So, after this update, I'm getting a parsing error in the Here's a simple reproduction of the same: Source code: let a = "\\38"; This correctly gets fixed to: let a = String.raw`\38`; However, the above
The But, with the value: { cooked: '\\38', raw: '\\38' } This works fine with the value: { raw: '\\38', cooked: null } So, I think it's better to keep the LMK if this makes sense, I'll add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is bug in typescript-eslint, can you report to them? I can do it if you don't want to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd appreciate if you could, I'm fairly new to parsers, so I might not be able to explain it clearly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
|
Uh oh!
There was an error while loading. Please reload this page.