Search code examples
typescriptabstract-syntax-treetypescript-compiler-api

What is the AST for decorator in typescript?


I need to know what is the AST name of the typescript decorators?

I need it for the ESLint selectors.

I saw a tool somewhere but I can't find it


Solution

  • Decorators are available on the modifiers of the decoratable node. There is a ts.getDecorators helper method you can use to access them though:

    const classDecl: ts.ClassDeclaration = ...;
    const decorators = ts.getDecorators(classDecl);
    // use decorators here
    

    This is the type of ts.Decorator:

    export interface Decorator extends Node {
        readonly kind: SyntaxKind.Decorator;
        readonly parent: NamedDeclaration;
        readonly expression: LeftHandSideExpression;
    }
    

    https://ts-ast-viewer.com/#code/AIZQ9gtgpgIlDGYBOBDALsgUPANigzvgAQCiAHihAA45REDemAvkA