microsoft / microsoft/TypeScript

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

Offen
#36,733 4 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, TransformationContext.addDiagnostic durch den Compiler sowie die in der Issue erwähnten Pfade für transpileModule und die Deklarationsgenerierung zu verfolgen. Bestätige, wo Diagnosen von Transformern derzeit gelesen werden, und ermittle anschließend die erforderlichen Tests, um zu zeigen, dass eine von einem benutzerdefinierten Transformer hinzugefügte Diagnose die Ergebnisse von Compiler und transpileModule erreicht.

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
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.