microsoft / microsoft/TypeScript
feature request: public the TransformationContext.addDiagnostic and read them in the compiler and transpileModule
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
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
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 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