Printer doesn't print jsDoc comments
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:5
- Comments:6 (3 by maintainers)
Also, we need a way to generate JSDoc comment. Like
Typescript.createJSDocNode
or somethingHey 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: