我的目标是获取发出的(JavaScript)文件中特定TypeScript AST节点的位置(开始和结束)。
以此代码为例:
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 }));
如何获取由aNode
表示的发出代码的位置?
我现在尝试这种方式的方法是捕获源地图文件并使用https://npmjs.org/package/source-map对位置进行反向工程。但是,源映射文件只给出了对位置的估计。
有没有办法使用源映射文件对确切位置进行反向工程,或者在发射阶段将节点写入JavaScript输出时拦截确切位置?