27
Vote

Make the TypeScript Compiler extensible

description

It would be interesting to extend TypeScript in a more reliable fashion by allowing the compiler to be extensible. As a result, 3rd parties could leverage the published TypeScript version and, through the use of a composition or plugin model, extend TypeScript along a well-defined API surface.

Ideally it would be a great benefit to have a mechanism where a plugin or extension author could subclass the Compiler, Scanner, Parser, Pull-Type Checker, Emitter, and other interesting internals, to add additional capabilities.

As a result, plugin authors could use the well-defined surface to add additional behavior to TypeScript, such as minification at compile time, extensions to the language, embed other languages, localize at compile time, emit debug or coverage metadata, refactoring, etc., without needing to fork the entire codebase and find a means to keep the fork and the extensions in sync.

Please consider providing a published extensibility API for the TypeScript compiler, and a means to provide plugins either via command-line switches and/or by providing a global "plugins" folder in the TypeScript installation path.

There of course are open questions and likely concerns around forcing TypeScript into a well-defined API surface, which could make it more difficult to change the API to add future capabilities, as well as how plugins could effectively interface with tooling such as the Visual Studio language extensions.

comments

danquirk wrote Jul 19, 2013 at 6:14 PM

Thanks for the suggestion, assigned to Jonathan.

thorn0 wrote Oct 6, 2013 at 8:24 PM

As an example use of compiler extensibility, we can think of AngularJS and its DI annotations.
export class TodoCtrl {

    // $inject annotation.
    // It provides $injector with information about dependencies to be injected
    // into constructor. It is better to have it close to the constructor, because
    // the parameters must match in count and type.
    // See http://docs.angularjs.org/guide/di
    public static $inject = [ '$scope', '$location', 'todoStorage', 'filterFilter' ];

    // dependencies are injected via AngularJS $injector
    constructor(
        private $scope: ITodoScope,
        private $location: ng.ILocationService,
        private todoStorage: ITodoStorage,
        private filterFilter
    ) {
        //...
In order for DI not to break after minification, developers have to add $inject annotations manually. Would be cool if it was generated by the compiler.
export class TodoCtrl {

    [Injectable] // this attribute tells the compiler extension to generate $inject for us
    constructor(
        private $scope: ITodoScope,
        private $location: ng.ILocationService,
        private todoStorage: ITodoStorage,
        private filterFilter
    ) {
    //...

robfe wrote Oct 7, 2013 at 12:57 AM

I'd love to see this. Postsharp-style attributes (http://doc.postsharp.net/postsharp-2.1/Default.aspx##PostSharp-2.1.chm/html/T_PostSharp_Aspects_OnMethodBoundaryAspect.htm) would be amazing (except they'd probably transform a compile-time syntax tree rather than doing runtime AOP).

Another use case for this could be an attribute that makes all of a class' properties into knockout observables, or a way to control which method is used to implement inheritance (so I can use a JS framework's object.extend method instead of typescripts, for specific classes instead of the current global hack)

thorn0 wrote Oct 9, 2013 at 1:08 PM

On the other hand, this might be achievable with macros if they were added to the language.

Wolverine_ wrote Jan 31 at 4:38 PM

This is probably my #1 request for the language. I think a feature like this would really help strengthen the TypeScript platform / ecosystem. I can think of quite a few uses, most notably:

1 - Tree-shaking assertions, and other debug-related development code before shipping.
2 - Custom linters that enforcing company-specific guidelines on various code structures.

Please consider!

markbuer wrote Mar 17 at 12:39 AM

I wrote a small story about module resolution plugins here.

jamesnw wrote Apr 9 at 1:20 AM

This would be great for custom APIs. I think this would open many creative doors. :)

lohankin wrote Apr 9 at 2:29 AM

Indeed, this is #1 feature IMO. Mother of all features. Please give users the opportunity to explore their ideas.

Arnavion wrote May 23 at 11:51 PM

Another use: adding JSDoc comments to the AST based on type information. Currently I have to hack with TS internals to achieve that: https://typescript.codeplex.com/discussions/541331