microsoft / microsoft/TypeScript
Include CompilerHost in TransformationContext
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TransformationContext and CompilerHost APIs, then trace how custom transformers receive context and how ts.resolveModuleName uses a host. Define the intended API shape and check its compatibility with existing transformers. Done means a transformer can access the relevant CompilerHost for module resolution without creating a redundant host.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100