microsoft / microsoft/TypeScript

feature request: public the TransformationContext.addDiagnostic and read them in the compiler and transpileModule

Aperta
#36,733 4 commenti 4 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

custom transformer diagnostic

Suggestion

mark the method addDiagnostic in TransformationContext as public and read the diagnostic result in the compiler

Use Cases

I'm writing a custom transformer. In some cases, the transform will fail but I don't want to throw an Error. I want to add my error to the diagnostics.

Examples

I have tried to call the addDiagnostic by as any

function createDiagnosticForNodeInSourceFile(
    ts: ts,
    sourceFile: SourceFile,
    node: Node,
    message: DiagnosticMessage,
    arg0?: string | number,
    arg1?: string | number,
    arg2?: string | number,
    arg3?: string | number,
): DiagnosticWithLocation | undefined {
    return (ts as any).createDiagnosticForNodeInSourceFile?.(sourceFile, node, message, arg0, arg1, arg2, arg3)
}
function appendDiagnosticMessageForNode(context: TransformationContext, diag: DiagnosticWithLocation | undefined) {
    if (!diag) return false
    const _context = internalTransformationContext(context)
    if (!_context.addDiagnostic) return false
    _context.addDiagnostic(diag)
    return true

    function internalTransformationContext(
        context: TransformationContext,
    ): TransformationContext & { addDiagnostic?(diag: DiagnosticWithLocation): void } {
        return context as any
    }
}

But that doesn't work. After digging into the source code, I found that it seems like the diagnostics only read in the dts generating.

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 seguendo TransformationContext.addDiagnostic attraverso il compilatore, transpileModule e i percorsi di generazione delle dichiarazioni menzionati nell’issue. Conferma dove vengono attualmente letti i diagnostici dei transformer, quindi individua i test necessari per dimostrare che un diagnostico aggiunto da un transformer personalizzato raggiunge i risultati del compilatore e di transpileModule.

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
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.