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

Include CompilerHost in TransformationContext #37754

Open
5 tasks done
HoldYourWaffle opened this issue Apr 2, 2020 · 2 comments
Open
5 tasks done

Include CompilerHost in TransformationContext #37754

HoldYourWaffle opened this issue Apr 2, 2020 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@HoldYourWaffle
Copy link
Contributor

Search Terms

compiler host, CompilerHost,
transformation context, TransformationContext,
transformer, custom transformer

Suggestion

I'd like to access the CompilerHost instance from my custom transformers (via TransformationContext).

As far as I can see this wouldn't expose any "risky" patterns like #25147.

Use Cases

My specific use-case is resolving import declarations. This can be done using ts.resolveModuleName (#28276 (comment)), but this requires a CompilerHost instance.

Example

An example for my specific use-case would be:

const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
    return file => {
        return ts.visitEachChild(
            file,
            node => {
                if (ts.isImportDeclaration(node)) {
                    const resolved = ts.resolveModuleName(
                        (node.moduleSpecifier as ts.StringLiteral).text,
                        file.fileName,
                        context.getCompilerOptions(),
                        context.getCompilerHost() // this is currently impossible
                    )
                    
                    // do something interesting with resolved
                }
                return node;
            },
            context
        )
    }
}

Workaround

Luckily there is a workaround for this:

const host = ts.createCompilerHost(context.getCompilerOptions());

But this feels redundant and somewhat "wrong".

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Apr 21, 2020
@RyanCavanaugh
Copy link
Member

@rbuckton thoughts?

@rbuckton
Copy link
Member

I don't see why not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants