microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.12k stars 12.5k forks source link

Ability to type check factory constructed source files #43600

Open dsherret opened 3 years ago

dsherret commented 3 years ago

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:

💻 Use Cases

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?

ClixStudios commented 1 year ago

Would love some feedback/advice on this particular suggestion, just some insight on it's viability would be appreciated - or if anyone's aware of any duplicate issues?