question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Escaping doc comments inside doc comments

See original GitHub issue

It’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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:34 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
jaydensericcommented, Apr 8, 2022

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.

This seems like a lot of effort to work around a problem that could more readily be solved with “don’t put */ in your code examples”.

Here are a few common situations where you need to use */ in a JSDoc comment:

  1. When documenting a glob string as a default parameter value, e.g:

    /** @param {string} [glob='**/*.js'] Glob pattern. */
    

    Real world examples:

  2. When documenting a CLI command containing a glob, e.g:

    /**
     * @example <caption>CLI usage.</caption>
     * ```sh
     * npx jsdoc-md --source-glob **/*.{mjs,js}
     * ```
     */
    

    Real world examples:

  3. 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 (via eslint-plugin-jsdoc and the jsdoc/check-examples rule).

    Example 1:

    /**
     * @example <caption>Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema).</caption>
     * ```js
     * const { makeExecutableSchema } = require('graphql-tools');
     * const { GraphQLUpload } = require('graphql-upload');
     *
     * const schema = makeExecutableSchema({
     *   typeDefs: /* GraphQL */ `
     *     scalar Upload
     *   `,
     *   resolvers: {
     *     Upload: GraphQLUpload,
     *   },
     * });
     * ```
     */
    

    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:

    Screen Shot 2021-03-30 at 2 16 20 pm

    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 an unescapeJsdoc 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:

Screen Shot 2021-03-30 at 2 39 18 pm

Secondly, I haven’t figured out yet how to get the eslint-plugin-jsdoc rule jsdoc/check-examples to do the unescaping before attempting to parse examples as JS:

Screen Shot 2021-03-30 at 2 38 21 pm
2reactions
octogonzcommented, Jul 13, 2019

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:

/**
 * ```ts
 * /**
 *  * Matches "abc\/def" or "*\\/abc" but not "C:\\foo\\bar"
 *  *\/
 * function foo(path) {
 *   return /[a-z*]+\/[a-z]+$/.test(path);
 * }
 * ```
 */

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:

```ts
/**
 * Matches "abc/def" or "*\/abc" but not "C:\\foo\\bar"
 */
function foo(path) {
  return /[a-z*]+/[a-z]+$/.test(path);
}
```

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 *&#47; might be more acceptable merely because people don’t use HTML escapes as much.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found