Skip to content

[transform API]: Allow passing getEmitResolver into TransformationContext and expose getEmitResolver from ProgramΒ #46793

Open
@ahnpnl

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 transform API to process AST of TypeScript source codes with some custom AST transformers.

  • Use swc or esbuild to compile ts to js with 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...)

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptAwaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions