RFC: Escaping doc comments inside doc comments
See original GitHub issueIt’s useful to users to include code examples in documentation comments. Sometimes you need to illustrate what the output of different code examples are, so you put it in a comment below, and if it’s multiple lines, you use a block comment. The problem is that TSDoc doesn’t correctly parse this:
/**
* ```
* getAverage()
* /* 'some output' */
* ```
*/
function getAverage(x, y) {
return (x + y) / 2.0;
}
I think it should handle block comments when they’re inside a code block in the documentation comment.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:34 (4 by maintainers)
Top Results From Across the Web
Pete Gonzalez on Twitter: "Suppose I need to write "/* */" inside a ...
Suppose I need to write "/* */" inside a #JSDoc or #TSDoc code comment. JavaScript provides no way to do this! Idea: Let's...
Read more >RFC 1866: Hypertext Markup Language - 2.0
HTML markup can represent hypertext news, mail, documentation, ... Comments To include comments in an HTML document, use a comment declaration.
Read more >IETF RFC-2396 on URI Syntax
Network Working Group T. Berners-Lee Request for Comments: 2396 MIT/LCS ... This document defines the generic syntax of URI, including both absolute and ......
Read more >Go Doc Comments - The Go Programming Language
“Doc comments” are comments that appear immediately before top-level package, const, func, type, and var declarations with no intervening newlines. Every ...
Read more >How do I escape characters in C# comments? - Stack Overflow
If you need to escape characters in XML comments, you need to use the character entities, so < would need to be escaped...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve been wrestling with this problem with JSDoc for years, both from the side as a user trying to document things, and as the creator of
jsdoc-md
which generates markdown API docs from JSDoc.Here are a few common situations where you need to use
*/
in a JSDoc comment:When documenting a glob string as a default parameter value, e.g:
Real world examples:
When documenting a CLI command containing a glob, e.g:
Real world examples:
When documenting template literal comment tags that have utility. A very common example is how linters, formatters, and syntax highlighters hook of the leading
/* GraphQL */
comment to process the following template string containing GraphQL SDL. In the code examples of my server and client GraphQL related packages, I want any example containing a GraphQL query string to have/* GraphQL */
in front of them to get users in a good habit, and so that the GraphQL SDL within code example is Prettier formatted (viaeslint-plugin-jsdoc
and thejsdoc/check-examples
rule).Example 1:
Another example of this situation is
babel-plugin-syntax-highlight
, which uses template string comment tags for build-time syntax highlighting. I had to give up on JSDoc for documenting that package, and manually write write code examples in the readme markdown:Real world examples:
For
jsdoc-md
, I came up with the convention that*/
within the content of JSDoc descriptions or types can be escaped with*\/
, and before processing such content is run through anunescapeJsdoc
function that simply replaces all occurrences of*\/
with*/
:https://github.com/jaydenseric/jsdoc-md/blob/v9.1.1/private/unescapeJsdoc.js#L3-L24
Edit: Updated function: https://github.com/jaydenseric/jsdoc-md/blob/c37795fa1976e17aeb4acce1832795efefa093c9/unescapeJsdoc.mjs#L1-L22
There are problems with this convention though, unless the wider ecosystem adopts it. Firstly, VS Code displays the code example with the escape still there in intellisense tooltips:
Secondly, I haven’t figured out yet how to get the
eslint-plugin-jsdoc
rulejsdoc/check-examples
to do the unescaping before attempting to parse examples as JS:This problem is fairly difficult. Users have a very strong expectation that a fenced code block is reproduced literally. For example, suppose we choose
\
as our escape character. We get something like this:If we allow
\/
to be escaped by the/*
*/
framing, it needs to be just the/
character. It would be wildly unacceptable to expand every single\
escape before TSDoc starts parsing the input, because\
is a standard escape character used by Markdown, JSDoc, and also source code.But this means
\/
gets expanded before\\
. So the second layer of the onion skin will look like this:It’s highly confusing why the
/
in the RegExp got expanded but not the"C:\\
, but I suppose someone could memorize that/
is special. It’s particularly counterintuitive with"*\\/abc"
where\\/
gets processed from right-to-left, whereas a casual reader would assume the opposite.The
*/
might be more acceptable merely because people don’t use HTML escapes as much.