microsoft / microsoft/TypeScript
feature request: public the TransformationContext.addDiagnostic and read them in the compiler and transpileModule
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par suivre TransformationContext.addDiagnostic à travers le compilateur, transpileModule et les chemins de génération de déclarations mentionnés dans l’issue. Confirmez où les diagnostics des transformateurs sont actuellement lus, puis identifiez les tests nécessaires pour montrer qu’un diagnostic ajouté par un transformateur personnalisé atteint les résultats du compilateur et de transpileModule.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 35/100