TypeScript编译器api:获取特定AST节点的生成代码中的位置

问题描述 投票:3回答:1

我的目标是获取发出的(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输出时拦截确切位置?

javascript typescript compilation
1个回答
0
投票

我认为我所寻找的是不可能的。我最终生成源地图并使用https://github.com/mozilla/source-map计算原始位置

© www.soinside.com 2019 - 2024. All rights reserved.