Skip to content

JSDoc stripping leading whitespace in code blocks #15749

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

Closed
mjbvz opened this issue May 10, 2017 · 14 comments
Closed

JSDoc stripping leading whitespace in code blocks #15749

mjbvz opened this issue May 10, 2017 · 14 comments
Assignees
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Help Wanted You can do this VS Code Priority Critical issues that VS Code needs fixed in the current TypeScript milestone

Comments

@mjbvz
Copy link
Contributor

mjbvz commented May 10, 2017

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

@mjbvz mjbvz added the VS Code Tracked There is a VS Code equivalent to this issue label May 10, 2017
@mjbvz
Copy link
Contributor Author

mjbvz commented May 10, 2017

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

@mhegazy mhegazy added Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation labels May 10, 2017
@mhegazy mhegazy added the Help Wanted You can do this label Apr 12, 2018
@mhegazy mhegazy added this to the Community milestone Apr 12, 2018
@jiayihu
Copy link

jiayihu commented Jul 10, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented Jul 10, 2018

@sandersn, can you point @jiayihu to where this change needs to be made

@sandersn
Copy link
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.

@sandersn sandersn changed the title JSDoc Stripping Leading WhiteSpace in CodeBlocks JSDoc stripping leading whitespace in code blocks Jul 10, 2018
@psyrendust
Copy link

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

@mjbvz mjbvz added VS Code Priority Critical issues that VS Code needs fixed in the current TypeScript milestone and removed VS Code Tracked There is a VS Code equivalent to this issue labels Jan 24, 2019
@sandersn
Copy link
Member

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

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

@sandersn
Copy link
Member

sandersn commented Apr 8, 2019

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 @psyrendust's last example, not for the oddly-indented example I just gave.

@justinmchase
Copy link

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

@mjbvz
Copy link
Contributor Author

mjbvz commented Feb 26, 2020

The original example works for me in VS Code 1.43:

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

@justinmchase
Copy link

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

@tuliocoppola
Copy link

tuliocoppola commented May 11, 2020

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

@erichiller
Copy link

erichiller commented Jun 8, 2020

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

@davwheat
Copy link

davwheat commented May 1, 2021

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

@sandersn
Copy link
Member

sandersn commented May 4, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Help Wanted You can do this VS Code Priority Critical issues that VS Code needs fixed in the current TypeScript milestone
Projects
None yet
Development

No branches or pull requests

10 participants