Skip to content
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

Possible string / number literal printing bug #464

Closed
terotests opened this issue Oct 13, 2018 · 6 comments
Closed

Possible string / number literal printing bug #464

terotests opened this issue Oct 13, 2018 · 6 comments
Labels

Comments

@terotests
Copy link

terotests commented Oct 13, 2018

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 ;
}
@terotests
Copy link
Author

Tested with 17.0.0 with same results.

@dsherret
Copy link
Owner

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:

console.log(f.print());

@terotests
Copy link
Author

Ok, thanks a million! I will try that 👍

@dsherret
Copy link
Owner

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.

@dsherret
Copy link
Owner

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

@dsherret
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants