Skip to content

JSDoc stripping leading whitespace in code blocks #15749

Closed

Description

@mjbvz

From: microsoft/vscode#26415

TypeScript Version: 2.3.2

Code

/**
 * @example
 * ```
 * if (true) {
 *     foo()
 * }
 * ```
 */
function foo() {
    return '2';
}

Expected behavior:
For the @example tag, we should return content including any leading whitespace

Actual behavior:
In this case, whitespace is stripped:

[Trace - 3:32:54 PM] Response received: quickinfo (120). Request took 2 ms. Success: true 
Result: {
    "kind": "function",
    "kindModifiers": "",
    "start": {
        "line": 9,
        "offset": 10
    },
    "end": {
        "line": 9,
        "offset": 13
    },
    "displayString": "function foo(): string",
    "documentation": "",
    "tags": [
        {
            "name": "example",
            "text": "```\nif (true) {\nfoo()\n}\n```"
        }
    ]
}

This results in incorrect rendering of the code example:

screen shot 2017-05-10 at 3 33 17 pm

Activity

  1. mjbvz commented on May 10, 2017

    @mjbvz
    ContributorAuthor

    This does not happen if you have an extra newlines before the code block:

    /**
     * @example
     * 
     * ```
     * if (true) {
     *     foo()
     * }
     * ```
     */
    function foo() {
        return '2';
    }

    screen shot 2017-05-10 at 3 34 17 pm

  2. added this to the milestone on Apr 12, 2018
  3. jiayihu commented on Jul 10, 2018

    @jiayihu

    The issue is still there, where should I have a look in order to fix it and open a PR?

  4. mhegazy commented on Jul 10, 2018

    @mhegazy
    Contributor

    Nathan Shively-Sanders (@sandersn), can you point Jiayi Hu (@jiayihu) to where this change needs to be made

  5. sandersn commented on Jul 10, 2018

    @sandersn
    Member

    Take a look at parseTagComments, although the bug might be in the initial state when calling the function, since there's an initial indent passed in.

  6. changed the title [-]JSDoc Stripping Leading WhiteSpace in CodeBlocks[/-] [+]JSDoc stripping leading whitespace in code blocks[/+] on Jul 10, 2018
  7. psyrendust commented on Oct 31, 2018

    @psyrendust

    I'm getting a slightly different results in vscode Version 1.28.2 (1.28.2)

    with newline after @example

    /**
     * @example
     *
     * ```
     * if (true) {
     *     foo()
     * }
     * ```
     */
    function foo() {
        return '2';
    }

    image

    no newline after @example

    /**
     * @example
     * ```
     * if (true) {
     *     foo()
     * }
     * ```
     */
    function foo() {
        return '2';
    }

    image

    no fenced code blocks

    /**
     * @example
     * if (true) {
     *     foo()
     * }
     */
    function foo() {
        return '2';
    }

    image

  8. added
    VS Code PriorityCritical issues that VS Code needs fixed in the current TypeScript milestone
    and removed
    VS Code TrackedThere is a VS Code equivalent to this issue
    on Jan 24, 2019
  9. sandersn commented on Mar 13, 2019

    @sandersn
    Member

    In summary, the original issue is fixed, but unfenced code blocks still squash leading white space as in Larry Gordon (@psyrendust)'s last example.

    The parser doesn't know about markdown, so I suspect this is caused later in the pipeline. Needs investigation.

  10. sandersn commented on Apr 8, 2019

    @sandersn
    Member

    The problem is that the code currently only handle indentations of the form

    /**
     * @example if (true) {
     *              foo()
     *          }
     */

    The code starts off parsing @example's comment with the wrong indentation because it doesn't reset the indent level after parsing @example followed by newline.

    Edit: the code is wrong for Larry Gordon (@psyrendust)'s last example, not for the oddly-indented example I just gave.

  11. justinmchase commented on Feb 26, 2020

    @justinmchase

    I see that this is closed but it appears to still be a bug in the latest release.

  12. mjbvz commented on Feb 26, 2020

    @mjbvz
    ContributorAuthor

    The original example works for me in VS Code 1.43:

    Screen Shot 2020-02-26 at 10 22 09 AM

  13. justinmchase commented on Feb 26, 2020

    @justinmchase

    False alarm. It wasn't working for me I swear, but just trying it again its now fine... Please ignore.

  14. tuliocoppola commented on May 11, 2020

    @tuliocoppola

    A related issue still occurring on version 1.44.0

    The original example works:
    Screen Shot 2020-05-11 at 16 42 54

    But if you use the <caption> tag on the @example it breaks again and trims the whitespaces:
    Screen Shot 2020-05-11 at 16 43 13

  15. erichiller commented on Jun 8, 2020

    @erichiller

    Doubling what @tulio-coppola said, <caption> seems to break things.
    <caption> requires an initial indentation, whereas, without <caption> (second example below) no initial indentation is required

    Example:

    /**
     * Create a new type which has the properties of `Base` that match `Condition`
     * 
     * @example <caption>This example extracts properties of `foo` that have a value type of `string`</caption>
     *          // `fooT` is now { a: string, d: string }
     *          // `fooKT` is now ` 'a' | 'd' `
     *          interface foo {
     *              a: string;
     *              b: number;
     *              c: string[];
     *              d: string;
     *          }
     *          type fooT = SubType<foo, string>;
     *          type fooKT = keyof fooT ;
     * @example
     * interface foo {
     *      prop: string;
     * }
     */
    export type SubType<Base, Condition> =
        Pick<Base, AllowedNames<Base, Condition>>;

    results in:
    image

    This is 1.45.1 on Windows

  16. davwheat commented on May 1, 2021

    @davwheat

    This issue is still present in the latest version of TS and VSCode.

  17. sandersn commented on May 4, 2021

    @sandersn
    Member

    Please open a new issue; this one has been closed a long time.

  18. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generationHelp WantedYou can do thisVS Code PriorityCritical issues that VS Code needs fixed in the current TypeScript milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions