All files / utils decoratorUtils.ts

75% Statements 24/32
59.09% Branches 13/22
83.33% Functions 5/6
82.14% Lines 23/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x   1x 16x 16x   16x   23x       23x 23x 9x 9x   9x     9x   23x 23x         1x 1x 1x   1x     1x 1x 1x   1x     1x                          
import * as ts from 'typescript';
 
export function getDecorators(node: ts.Node, isMatching: (identifier: DecoratorData) => boolean): DecoratorData[] {
    const decorators = node.decorators;
    Iif (!decorators || !decorators.length) { return []; }
 
    return decorators
        .map(d => {
            const result: any = {
                arguments: [],
                typeArguments: []
            };
            let x: any = d.expression;
            if (x.kind === ts.SyntaxKind.CallExpression) {
                Eif (x.arguments) {
                    result.arguments = x.arguments.map((argument: ts.StringLiteral) => argument.text);
                }
                Iif (x.typeArguments) {
                    result.typeArguments = x.typeArguments;
                }
                x = x.expression;
            }
            result.text = x.text;
            return result as DecoratorData;
        })
        .filter(isMatching);
}
 
export function getDecoratorName(node: ts.Node, isMatching: (identifier: DecoratorData) => boolean) {
    const decorators = getDecorators(node, isMatching);
    Iif (!decorators || !decorators.length) { return; }
 
    return decorators[0].text;
}
 
export function getDecoratorTextValue(node: ts.Node, isMatching: (identifier: DecoratorData) => boolean) {
    const decorators = getDecorators(node, isMatching);
    Iif (!decorators || !decorators.length) { return; }
 
    return decorators[0].arguments[0];
}
 
export function isDecorator(node: ts.Node, isMatching: (identifier: DecoratorData) => boolean) {
    const decorators = getDecorators(node, isMatching);
    if (!decorators || !decorators.length) {
        return false;
    }
    return true;
}
 
export interface DecoratorData {
    text: string;
    arguments: Array<string>;
    typeArguments: Array<any>;
}