Typescript: API for Plugin Support for Custom Transformers

Created on 28 Nov 2018  路  7Comments  路  Source: microsoft/TypeScript

related: https://github.com/Microsoft/TypeScript/issues/14419

Some third-party plugins that provide different APIs form transformer:

https://github.com/kimamula/ts-transformer-keys#how-to-use-the-custom-transformer

// rollup
{ transformers: [service => ({
  before: [ keysTransformer(service.getProgram()) ],
  after: []
}

// awesome-typescript-loader
getCustomTransformers: program => ({
    before: [
       keysTransformer(program)
    ]
})

So I raised this question: What parameters(ts.LanguageService or ts.Program ) do we need to pass to the third-party transformer?

I want to design the typescript-macros API closer to the Transformer Plugin API that TypeScript will provide in the future.


some usage example for typescript-macros:

https://github.com/xiaoxiangmoe/typescript-macros/blob/master/examples/console-scope.tsmacro/README.md

https://github.com/xiaoxiangmoe/typescript-macros/blob/master/examples/hooks.tsmacro/README.md

https://github.com/xiaoxiangmoe/typescript-macros/tree/master/examples/uppercase.tsmacro

Question feature-request

Most helpful comment

We just don't know right now. We haven't yet implemented compiler plugins because we have concerns over performance and code isolation that are difficult for us to work around without first altering our language service to allow asynchronous operations. That, and we'd need to merge services down into the compiler, but we have a few decent reasons to consider doing that anyway.

All 7 comments

@weswigham Do you have any suggestions about custom transformer for bundle tools?

A language service can provide a program, so in some respects, bundles more functionality; but that functionality is for editor actions - the program itself has most all of the functionality you'd actually need.

@weswigham LanguageService.findReferences is helpful for find references for processing macros.

Should we pass ts.LanguageService as a param for macros?

If you find it useful, sure.

@weswigham https://github.com/Microsoft/TypeScript/issues/16607#issuecomment-309535126

Will Compiler Plugins will provide LanguageService in the future? (When TypeScript implemented Compiler Plugins)


declare namespace ts {
    /**
     * Transform one or more nodes using the supplied transformers.
     * @param source A single `Node` or an array of `Node` objects.
     * @param transformers An array of `TransformerFactory` callbacks used to process the transformation.
     * @param compilerOptions Optional compiler options.
     */
    function transform<T extends Node>(source: T | T[], transformers: TransformerFactory<T>[], compilerOptions?: CompilerOptions): TransformationResult<T>;
}

ts only provide ts.TransformerFactory[] for transform function, without ts.LanguageService

We just don't know right now. We haven't yet implemented compiler plugins because we have concerns over performance and code isolation that are difficult for us to work around without first altering our language service to allow asynchronous operations. That, and we'd need to merge services down into the compiler, but we have a few decent reasons to consider doing that anyway.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Was this page helpful?
0 / 5 - 0 ratings