Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks done
ahnpnl opened this issue Nov 12, 2021 · 5 comments
Labels
API Relates to the public API for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@ahnpnl
Copy link

ahnpnl commented Nov 12, 2021

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...)

@andrewbranch andrewbranch added API Relates to the public API for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Nov 12, 2021
@andrewbranch
Copy link
Member

@rbuckton for thoughts?

@frigus02
Copy link
Contributor

At Google we would also benefit from having the EmitResolver available in transformers. Use cases:

To improve edit/refresh performance during development we would like to transpile TS to JS without type checking. Ideally we would do that using ts.transpileModule(). We still have to do some transforms, though, and for 2 cases we don't see a way to do them without type checker or emit resolver:

  • To transform user-written .default accesses on imports, we need to find the import declaration for a given identifier. (code). EmitResolver#getReferencedImportDeclaration would help here.
  • To transform imports and still elide type-only imports, we need to know if a given identifer was used in a value position. EmitResolver#isReferencedAliasDeclaration would help here.

@rbuckton, would you consider making EmitResolver and some of its methods public?

@timocov
Copy link
Contributor

timocov commented Dec 27, 2023

Having exposed program's EmitResolver would benefit tools like dts-bundle-generator or @microsoft/api-extractor as well - in their case we need to perform a name collision resolution while generating new nodes or updating existing ones and try to preserve user's input as much as possible. Without exposing either program's EmitResolver or globalThis symbol it would be quite problematic or even impossible.

FYI @microsoft/api-extractor is using an internal method already to get EmitResolver in order to use hasGlobalName to perform described check.

@andrewbranch @rbuckton do you have any concerns re exposing it to public?

@rbuckton
Copy link
Member

rbuckton commented Jan 2, 2024

EmitResolver is primarily an emit-focused wrapper for TypeChecker. You can almost always get the same information from the checker directly, and if you're using our API to run custom transformers, you would already have access to the checker. I'd be more interested as to whether there is missing functionality that we should be exposing on TypeChecker instead. For example, rather than making EmitResolver public just to access hasGlobalName, we could instead expose TypeChecker's existing resolveName method for the same functionality, i.e.: checker.resolveName(name, /*location*/ undefined, SymbolFlags.All, /*excludeGlobals*/ false).

@timocov
Copy link
Contributor

timocov commented Jan 2, 2024

we could instead expose TypeChecker's existing resolveName method for the same functionality, i.e.: checker.resolveName(name, /location/ undefined, SymbolFlags.All, /excludeGlobals/ false).

@rbuckton this makes sense, thanks! For this particular use-case, can I create a PR with opening this API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants