prefer-string-raw: Forbid unnecessary String.raw#2695
prefer-string-raw: Forbid unnecessary String.raw#2695som-sm wants to merge 16 commits intosindresorhus:mainfrom
prefer-string-raw: Forbid unnecessary String.raw#2695Conversation
| yield fixer.replaceText(node, suggestion); | ||
| }, | ||
| }; | ||
| }); |
There was a problem hiding this comment.
We need check this.
`\u0040`
'@'
String.raw`\u0040`
'\\u0040'
There was a problem hiding this comment.
Maybe check .cooked === .raw?
There was a problem hiding this comment.
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.
Okay, I thought we suggest `\u0040` over String.raw`\u0040`, I must made a mistake.
Anyway, do you think we can check .cooked === .raw instead of checking contains \?
There was a problem hiding this comment.
Anyway, do you think we can check
.cooked === .rawinstead of checking contains\?
Yeah, we can, updated in 2eb20bf.
There was a problem hiding this comment.
The above update, surfaced an interesting issue.
So, after this update, I'm getting a parsing error in the puppeteer codebase at the following line:
https://github.com/puppeteer/puppeteer/blob/832831d21c42ff542300c194c2b4f07274d8f82f/test/src/queryhandler.spec.ts#L373
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 String.raw`\38` expression fails with the typescript parser, throwing the following error:
Parsing error: Octal escape sequences are not allowed. Use the syntax '\x03'.
The includes(BACKSLASH) check wasn’t failing because it allowed the function to exit early when a backslash was detected in the raw value.
But, with the .cooked === .raw check, it fails because both cooked and raw values here are the same ('\\38'), causing the check to pass.
cooked value can't be '\38' because that '\38' is syntactically incorrect, maybe because of this the parser keeps the cooked value same as raw.
value: { cooked: '\\38', raw: '\\38' }This works fine with the babel parser because looks like it sets the cooked value to null in cases like these.
value: { raw: '\\38', cooked: null }So, I think it's better to keep the includes(BACKSLASH) check because it operates on the raw value, which seems to be a better choice in this case.
LMK if this makes sense, I'll add the let a = String.raw`\38`; test with the typescript parser.
There was a problem hiding this comment.
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.
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.
prefer-string-raw: Remove unnecessary usagesprefer-string-raw: Forbid unnecessary String.raw
| outdent` | ||
| a | ||
| String.raw\`a\${b}\` | ||
| `, |
There was a problem hiding this comment.
Please add a test for
function foo() {
return String.raw
// Comment
``
}
There was a problem hiding this comment.
Comments like these weren't being preserved, had to update the implementation a bit, refer cc0a128.
There was a problem hiding this comment.
The comment is not the point, in this case, we can't simply remove String.raw need add parenthesis after return.
There was a problem hiding this comment.
Ideally, it should fix to
function foo() {
return (
// Comment
``)
}There are similar cases exists, please search other rules.
There was a problem hiding this comment.
Search addParenthesizesToReturnOrThrowExpression
There was a problem hiding this comment.
Gotcha, apologies for the confusion.
There was a problem hiding this comment.
Added addParenthesizesToReturnOrThrowExpression.
0c30a3c to
ffcc738
Compare
| } | ||
|
|
||
| yield fixer.replaceText(node.quasi, suggestion); | ||
| yield fixer.remove(node.tag); |
There was a problem hiding this comment.
This is incorrect, since tag can be parenthesis.
((String.raw))``
There was a problem hiding this comment.
You can use removeParentheses
There was a problem hiding this comment.
Added removeParentheses.
| ? rawQuasi | ||
| : `'${rawQuasi.slice(1, -1).replaceAll('\'', String.raw`\'`)}'`; | ||
|
|
||
| return { |
There was a problem hiding this comment.
There is also an edge case that can't autofix (possible, but hard).
String.raw`use strict`;This will become a "Directive", I don't think we need handle it, but let's leave a comment.
Or simply remove the fix if it's an ExpressionStatement if you want.
There was a problem hiding this comment.
Added a test with note for this.
|
@som-sm Can you solve the conflicts? |
|
Bump :) |
|
Hi, apologies! This slipped off my head. I'll try to update this sometime this week. |
0cc9823 to
1aebc29
Compare
528fef7 to
27be5c6
Compare
27be5c6 to
bf5619e
Compare
|
I had lost all context on this, but I've tried to update it and address all pending comments. |
|
The fixer handles multiline Input: function* foo() {
yield String.raw
// comment
`abc`;
}Current fix: function* foo() {
yield
// comment
'abc';
}This changes runtime behvaior because ASI inserts after And an extra tests worth adding, same multiline function* foo() {
yield String.raw
`a${value}`;
} |
814b622 to
0e023e3
Compare
Closes #2652
This PR simply loops over all the
quasisand checks if any of them contain a backslash. If none of them do, then the rule starts complaining.Also, the suggestion is a template string(
`) only if there areexpressionsor the string contains line breaks, otherwise, the suggestion is always a single quotes string (').