-
Notifications
You must be signed in to change notification settings - Fork 12.8k
JSDoc comment parsing supports multiline backticks #50677
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
Conversation
Previously, backticks in JSDoc ended at the end of a line. This PR changes that so that backticks continue parsing until another backtick. Note that triple backticks and single backticks are treated the same way. The change is already very clunky, and I haven't thought about how much clunkier it would be to track backtick history in the parser.
I haven't read the PR yet, but I enabled the "tsdoc" ESLint linter plugin and there are even quite a few comments in our repo that have unclosed backtick blocks, so I'm half worried what we might break out there. |
I agree. After some discussion with @andrewbranch , this can be summarised as improvement in correctness at the cost of breaking existing code. I'm inclined to close this PR, but other opinions are welcome. @DanielRosenwasser you might want to weigh in. Edit: Probably we need an estimate of how common unbalanced backticks are. |
Why are there unclosed back ticks? I've never seen that in my entire life, and it is an interesting coincidence that the TS repo (of all places) has them. |
Just a typo (which I've already fixed). I'm only worried because JSDoc has semantic meaning in some codebases where an unclosed backtick might eat an |
#52398 is a related issue - any recollection whether |
Let's revisit this and consider whether or not we can give suggestion diagnostics for plain JS users, and errors for checkJs users. |
@DanielRosenwasser Here's a summary of the discussion from the editor meeting. No conclusions, just more issues raised:
So one possible path forward is to add an "unclosed backtick" error to this PR, for JS files only. We'd need to check what linters issue similar errors, however. |
I am not sure the JSDoc parsing changes are sufficient to correctly handle embedded markdown. If we want backtick parsing to be reliable, we should probably ensure we support the following cases as well: Code spans in markdown aren't interpreted as code spans if there is a hard line break (e.g. 2+ newlines) anywhere after the starting backtick, and only include soft line breaks if it can find a trailing backtick.
is interpreted as a code span:
while
is interpreted as literal characters:
Code spans must also be balanced so that they are not terminated improperly:
is interpreted as:
while
is interpreted as:
Code spans can contain backticks as long as the starting and ending backtick runs are greater in number than the number of sequential backticks to include in the code span:
is interpreted as:
If the code text of code span needs to include a backtick at the start or end, a space is required:
is interpreted as:
while
is interpreted as:
Code blocks begin with three or more backticks, followed by any non-backtick characters (primarily used for language selection), and end when the parser encounters the same number of backticks on a separate line, or the end of the input:
is interpreted as:
while
is interpreted as (note the backticks are included in the code block):
Also, code blocks aren't only detected from backticks. Fenced code blocks can also be written using 3+ sequential tilde characters:
is interpreted as:
|
goTo.marker("x"); | ||
verify.quickInfoIs("(parameter) x: string", "hi there `@param`"); | ||
goTo.marker("y"); | ||
verify.quickInfoIs("(parameter) y: string", "hi there `@ * param\nthis is the margin"); | ||
verify.quickInfoIs("(parameter) y: string", "hi there `@ * param\nthis is the margin\n@param {string} z OOPS, unclosed backtick!"); |
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.
This isn't correct. Markdown won't detect this as a code span because there's no closing backtick, so it will treat the backtick as a literal character.
I was experimenting with better markdown parsing for TSDoc a few years ago. The parsing rules I pursued were that any
Would treat the code span starting at This gets a bit tricker when JSDoc parameter descriptions flow onto the next line, since JSDoc rules don't align with markdown's block-level indentation rules. Consider an ordered list:
is interpreted as:
while
is interpreted as:
Ordered list indentation expects the text to flow at the same indentation level. So for (1), the This makes the rules around
in the case of code blocks whose initial indentation is 0:
if the indentation of the code block is greater than the block tag that follows:
if the code block starts the parameter description, its initial indentation should be the offset of the start of the description:
|
I'd rather avoid parsing real markdown code blocks, since our JSDoc parsing is already quite slow. But as @rbuckton shows, any improvement less than real parsing is unlikely to improve things. I'm going to close this PR. |
@sandersn is there going to be any alternative to solve the issue? |
@dreamorosi I haven't come up with anything yet. If you have some ideas, please do propose them on the original issue. |
Previously, backticks in JSDoc ended at the end of a line. This PR changes that so that backticks continue parsing until another backtick. I'm not sure this is a good change, and I left the code a bit inelegant until we decide.
Note that triple backticks and single backticks are treated the same way: both have to be closed. The code change is already very clunky, and I haven't thought about how much clunkier it would be to track backtick history in the parser.
I would appreciate opinions on whether this is a sensible change in behaviour, as well as ideas on how hard it would be to distinguish triple from single backticks.
Fixes #47679