microsoft / microsoft/TypeScript
Include CompilerHost in TransformationContext
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den APIs von TransformationContext und CompilerHost und verfolge anschließend, wie benutzerdefinierte Transformer Kontext erhalten und wie ts.resolveModuleName einen Host verwendet. Definiere die vorgesehene API-Struktur und prüfe ihre Kompatibilität mit bestehenden Transformern. Erledigt ist es, wenn ein Transformer auf den relevanten CompilerHost für die Modulauflösung zugreifen kann, ohne einen redundanten Host zu erstellen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100