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.

Printer doesn't print jsDoc comments

See original GitHub issue

I’m using the compiler to convert some old JS files into TypeScript (specifically changing our old “classes” into TS/ES6 classes). I’m doing this by getting the original AST, applying some transformations, and then printing out the new AST to code.

JSDoc nodes are parsed just fine and are available in the original AST, but when printing out the AST back to source, they are missing.

TypeScript Version: 2.5.2

Code

Here’s an example of just converting a simple class to AST, and trying to print it out again:

const ts = require('typescript');

const fileContents = `
    export class MyClass {
        /**
         * My great documentation
         */
        myMethod() { return 1; }
    }
`;
const sourceFile = ts.createSourceFile('myFile.ts', fileContents, ts.ScriptTarget.ES2017, /* setParentNodes */ false);
const ast = sourceFile.statements[ 0 ];

const resultFile = ts.createSourceFile('newFile.ts', '', ts.ScriptTarget.Latest, /*setParentNodes*/ false, ts.ScriptKind.TS);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed, removeComments: false });
const result = printer.printNode(ts.EmitHint.Unspecified, ast, resultFile);

console.log(result);

Expected behavior:

The result from the Printer should be printed exactly as the input:

export class MyClass {
    /**
     * My great documentation
     */
    myMethod() { return 1; }
}

Actual behavior:

The jsDoc comment is missing:

export class MyClass {
    myMethod() { return 1; }
}

Great job btw with all of the functionality to be able to grab the AST and generate new AST nodes. Many thanks for that!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
Jack-Workscommented, Oct 15, 2017

Also, we need a way to generate JSDoc comment. Like Typescript.createJSDocNode or something

1reaction
hsiaosiyuan0commented, Jul 21, 2020

Hey guys, good news that the NodeFactory is under refactoring, perhaps we can use ts.factory to get a new factory and use that factory to create jsDoc or something more, however we can also print the jsDoc before the 4.0 is released just separating the original print process into two subroutines, one for printing the jsDoc, another for the specified type:

const fn = ts.createFunctionDeclaration()
fn["jsDoc"] = {
  kind: ts.SyntaxKind.JSDocComment,
  tags: [
    {
      kind: ts.SyntaxKind.JSDocTag,
      tagName: ts.createIdentifier("look"),
      comment: "at you",
    },
  ],
};
printNode(fn['jsDoc']);
printNode(fn);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Printer doesn't print jsDoc comments · Issue #18661 - GitHub
I'm using the compiler to convert some old JS files into TypeScript (specifically changing our old "classes" into TS/ES6 classes).
Read more >
How to ignore jsDoc while generating code using printNode ...
Based on looking at the printer code (see shouldWriteComment and follow the code out of there) it doesn't seem possible out of the...
Read more >
benjamn/recast - Gitter
I am working on a project that would update/adds new comments to the AST to be printed. What is the recommended way to...
Read more >
200935 – Spellchecker doesn't work in jsdoc comments
The spellchecker works fine in html and java but not in javascript. Comment 1 Petr Pisl 2011-10-04 12:48 ...
Read more >
JavaScript Documentation Standards
WordPress follows the JSDoc 3 standard for inline JavaScript documentation. ... For example: “Fires when printing the link tag in the header”.
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