-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Suggestion
π Search Terms
- getEmitResolver
- transform API
β 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.
β Suggestion
transform API is quite nice and convenient because using this API doesn't need to invoke Program to compile ts to js to use custom AST transformers. However, it doesn't allow the case which requires to access to EmitResolver. It would be nice if transform API can allow users to pass getEmitResolver into the TransformationContext.
Currently, getEmitResolver isn't exposed for both TransformationContext as well as Program interfaces.
π Motivating Example
An ideal example would be like this
export function constructorParametersDownlevelTransform(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
const typeChecker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(typeChecker);
return (ctx: ts.TransformationContext) => {
return getDownlevelDecoratorsTransform(
typeChecker,
reflectionHost,
[],
false,
false,
true,
)({
...ctx,
getEmitResolver: ctx.getEmitResolver()
? ctx.getEmitResolver
: program.getDiagnosticsProducingTypeChecker().getEmitResolver,
});
};
}
π» Use Cases
I am trying to experiment the idea:
-
Use
transformAPI to process AST of TypeScript source codes with some custom AST transformers. -
Use
swcoresbuildto compiletstojswith the newly transformed source.
As far as I can tell, process AST with transform API is pretty fast. If I can process AST without using Program to do the full compilation step but use swc or esbuild to compile, that would help me to achieve the goal that: fast compilation + source is processed in such a way I want (Jest hoisting, Angular downlevel constructor etc...)