I am aiming to retrieve the specific TypeScript AST node's location (start and end) in the emitted JavaScript file.
Consider this code snippet:
const program = ts.createProgram(tsconfig.fileNames, tsconfig.options);
const aNode = program.getSourceFiles()[0].getChildAt(1);
const emittedFiles = [];
program.emit(/*targetSourceFile */undefined, /*writeFile*/ (fileName, content) => emittedFiles.push({ fileName, content }));
How can I determine the location of the emitted code indicated by aNode
?
Currently, I am attempting to achieve this by extracting the source map file and deducing the location using https://npmjs.org/package/source-map. However, the source map file only offers an approximate location.
Is there a method to pinpoint the exact location either by reverse engineering the source map file or intercepting the precise location as the node is being written to the JavaScript output during the emit phase?