microsoft / microsoft/TypeScript

Ability to type check factory constructed source files

Ouverte
#43,600 0 commentaires 4 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Domain: API Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Suggestion

It would be nice to have the ability to type check factory constructed source files.

For example, say I create some ASTs like so, where params.module.body is some foreign AST that I'm converting to a tsc AST:

const sourceFile = ts.factory.createSourceFile(
    params.module.body.map(item => convertModuleItem(item)),
    ts.factory.createToken(ts.SyntaxKind.EndOfFileToken), // side note... i'm unsure why this param exists
    ts.NodeFlags.None,
);
sourceFile.fileName = params.fileName;
sourceFile.text = params.fileText;
setRange(sourceFile, params.module); // my own function that sets the range

...it would be better to be able to use these source files without having to implement all the necessary hacks. For example, not having to do...

(sourceFile as any).parseDiagnostics = [];
(sourceFile as any).bindDiagnostics = [];

...along with figuring out how to set many more internal properties (ex. identifiers, pragmas, imports, etc... etc...).

🔍 Search Terms

type check transformed AST source file

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

💻 Use Cases

  • Ability to do AST transformations then feed that back through the type checker without having to print out the AST to a string then reparsing that string to an AST using the TS compiler.
  • Ability to take a non-tsc AST representation of an AST and construct it to a tsc AST using the factory methods. This would be useful in Deno as it is already parsing an AST with swc and so it could pass that AST over to the TS compiler for type checking.

Viability?

I understand this is a huge ask and the main reason I'm opening this is to ask how viable it is?

I believe the type checker would need to be changed to sparingly consult the source file text or node ranges and some of the code in the parser that sets internal properties would need to be extracted out to support it being lazily computed in addition to when it's parsed. Additionally, factory methods would have to clear the appropriate internal properties on nodes when they're transformed so that they will be lazily computed in the future when the type checker goes to retrieve them. Anything else?

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire le point d’entrée factory createSourceFile ainsi que les composants internes du vérificateur de types et de l’analyseur décrits dans l’issue. Le travail sera terminé lorsque les fichiers source construits par la factory pourront être transmis au vérificateur de types sans définir manuellement parseDiagnostics, bindDiagnostics, identifiers, pragmas, imports et autres propriétés internes.

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
20/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.