microsoft / microsoft/TypeScript

Include CompilerHost in TransformationContext

Aperta
#37,754 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalle API di TransformationContext e CompilerHost, quindi traccia il modo in cui i transformer personalizzati ricevono il contesto e il modo in cui ts.resolveModuleName utilizza un host. Definisci la forma prevista dell'API e verifica la sua compatibilità con i transformer esistenti. Il lavoro è completo quando un transformer può accedere al CompilerHost pertinente per la risoluzione dei moduli senza creare un host ridondante.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.