microsoft / microsoft/TypeScript
Include CompilerHost in TransformationContext
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
Search Terms
compiler host, CompilerHost,
transformation context, TransformationContext,
transformer, custom transformer
Suggestion
I'd like to access the CompilerHost instance from my custom transformers (via TransformationContext).
As far as I can see this wouldn't expose any "risky" patterns like #25147.
Use Cases
My specific use-case is resolving import declarations. This can be done using ts.resolveModuleName (https://github.com/microsoft/TypeScript/issues/28276#issuecomment-435016356), but this requires a CompilerHost instance.
Example
An example for my specific use-case would be:
const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
return file => {
return ts.visitEachChild(
file,
node => {
if (ts.isImportDeclaration(node)) {
const resolved = ts.resolveModuleName(
(node.moduleSpecifier as ts.StringLiteral).text,
file.fileName,
context.getCompilerOptions(),
context.getCompilerHost() // this is currently impossible
)
// do something interesting with resolved
}
return node;
},
context
)
}
}
Workaround
Luckily there is a workaround for this:
const host = ts.createCompilerHost(context.getCompilerOptions());
But this feels redundant and somewhat "wrong".
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 TransformationContext 和 CompilerHost API 开始,然后追踪自定义 transformer 如何接收 context,以及 ts.resolveModuleName 如何使用 host。定义预期的 API 形式,并检查其与现有 transformer 的兼容性。当 transformer 可以访问用于模块解析的相关 CompilerHost,而无需创建冗余 host 时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100