-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Improve line-break detection to handle "/*" inside "//" comments, #2129
Conversation
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.
Nice work on the quick fix here.
src/utils/renderHelpers.ts
Outdated
do { | ||
commentStart = code.indexOf('/', start); | ||
if (commentStart === -1 || commentStart > lineBreakPos) return lineBreakPos; | ||
nextChar = code[commentStart + 1]; |
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.
charCodeAt perhaps?
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.
Of course! Has been changed. I also slightly improved it to not misinterpret /*/
as an immediately closed block comment
src/utils/renderHelpers.ts
Outdated
@@ -45,17 +45,20 @@ export function findFirstOccurrenceOutsideComment( | |||
} | |||
|
|||
function findFirstLineBreakOutsideComment(code: string, start: number = 0) { | |||
let commentStart, lineBreakPos; | |||
let commentStart, lineBreakPos, nextChar; |
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.
I take it the assumption in this function is that start
is always at the end of all the code for a line?
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.
What happens in cases like:
(code) /*
*/ (moar code)
Will this be able to deal with the start of a code block inline with the block comment?
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.
There is actually no assumption about start
. Basically we
- find the next line-break following start
- find the next comment following start if it exists and see if it closes before we reach the line-break
- if there is a block comment, we find the end of the comment and repeat with the next line-break outside the comment
Thus in the situation above, the first line-break will be ignored and since there is not line-break between the end of the comment and the next statement, the algorithm will return -1.
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.
Ahh so the code
passed to this function in my example above would only ever be the " /*\n\n*/ "
part, that is it is guaranteed to be a substring between AST Nodes already?
6a12566
to
1872d80
Compare
Also adjusted |
1872d80
to
0391890
Compare
Released as 0.58.1. |
Resolves #2127
Previously, line-break detection outside comments was faulty in that since line comments cannot contain line-breaks, it would only scan the code for block comments. This approach failed if a block comment start
/*
was nested inside a line comment.The new logic now scans for all types of comments and ignores any markup inside both types of comments.