我的目标是获取发出的 (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 对位置进行逆向工程。 .然而,源 map 文件只给我一个位置的估计。
有什么方法可以使用源映射文件对确切位置进行逆向工程,或者在发射阶段将节点写入 JavaScript 输出时拦截确切位置?
最佳答案
我认为我所寻找的是不可能的。我最终制作了源 map 并使用这些 map 使用 https://github.com/mozilla/source-map 计算原始位置
关于javascript - TypeScript 编译器 api : get position in generated code of a specific AST node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707191/