Ts-morph: Possible string / number literal printing bug

Created on 13 Oct 2018  路  6Comments  路  Source: dsherret/ts-morph

Literals not printed with printNode
Version: 14.4.3

function printNode does not print string literals like "hello" or number literals like 555
To Reproduce

import Project, {printNode} from "ts-simple-ast";
const project = new Project();
const sourceFile = project.createSourceFile("test.ts", `
function test()  {
  return 555
}
`);
sourceFile.getFunctions().forEach( f => {
  // does not emit literal 555
  console.log(printNode(f.compilerNode))
})

Expected behavior

Should print the function code

function test()  {
  return 555
}

currently prints

function test() {
    return ;
}
bug

All 6 comments

Tested with 17.0.0 with same results.

Weird. I guess the printer can't figure out the literal text. You'll need to pass in the source file:

console.log(printNode(f.compilerNode, f.getSourceFile().compilerNode));

Or call .print() on the node itself, which will handle that for you:

```ts
console.log(f.print());

Ok, thanks a million! I will try that 馃憤

What I could do here is check if the node has parents. If it does I can work up the tree to find the source file. I'll go ahead and do that.

Oh wait, there's definitely a problem here with printNode. Passing in the source file is a temporary workaround though. Thanks for reporting!

This will be fixed in 17.0.1 (just releasing now). Thanks again!

Was this page helpful?
0 / 5 - 0 ratings