-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(eslint-plugin): [no-useless-template-literal] do not render escaped strings in autofixes #8688
base: main
Are you sure you want to change the base?
Changes from 5 commits
04e4736
37b7259
d30dd72
125b997
655d5b0
e07e146
ae589b4
98bc977
4d28bc4
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ import { | |
createRule, | ||
getConstrainedTypeAtLocation, | ||
getParserServices, | ||
getStaticStringValue, | ||
isTypeFlagSet, | ||
isUndefinedIdentifier, | ||
} from '../util'; | ||
|
@@ -53,7 +52,9 @@ export default createRule<[], MessageId>({ | |
return isString(type); | ||
} | ||
|
||
function isLiteral(expression: TSESTree.Expression): boolean { | ||
function isLiteral( | ||
expression: TSESTree.Expression, | ||
): expression is TSESTree.Literal { | ||
return expression.type === AST_NODE_TYPES.Literal; | ||
} | ||
|
||
|
@@ -144,10 +145,11 @@ export default createRule<[], MessageId>({ | |
]), | ||
]; | ||
|
||
const stringValue = getStaticStringValue(expression); | ||
|
||
if (stringValue != null) { | ||
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1'); | ||
if (isLiteral(expression)) { | ||
const escapedValue = | ||
auvred marked this conversation as resolved.
Show resolved
Hide resolved
|
||
typeof expression.value === 'string' | ||
? expression.raw.slice(1, -1).replace(/([`$])/g, '\\$1') | ||
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. (nit, optional) - why are the escaping patterns different for each branch? Maybe worth a comment? 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, it does seem a bit confusing Does 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. oof, i think i get it, but i still struggle tbh 😆 It's just really tricky to me to figure out which things are in escaped-land and which things are non-escaped in the code comment there (it's even hard for me to type out the special characters in this comment 😆 ). Is the idea
cooked string ⇒
I really think a few English words would be helpful in the code comments (to go along with the examples), since my brain is not big enough to make meaning of all the special characters at a glance 😄 |
||
: String(expression.value).replace(/([`$\\])/g, '\\$1'); | ||
|
||
fixes.push(fixer.replaceText(expression, escapedValue)); | ||
} else if (isTemplateLiteral(expression)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirkwaiblinger, Ohh, I missed that in the original issue!
I'm 50/50 on this!
Maybe..
Fixing to string literal vs template literal sounds a bit stylistic to me, I think it would be great to hear more opinions on this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, agreed. I will say, at the time, I didn't realize it was just removing the wrapping template syntax, as opposed to overriding whatever the user had typed with one particular quote style. So my motivation for that change has softened a lot as well. It still feels a little surprising to me, but it does have a very justifiable internal logic as-is.
So unless someone comes in and has a strong opinion, I'd say, just leave it as-is :)