How to use the tsutils.getChildOfKind function in tsutils

To help you get started, we’ve selected a few tsutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fossasia / susper.com / node_modules / tslint / lib / rules / onlyArrowFunctionsRule.js View on Github external
return ts.forEachChild(sourceFile, function cb(node) {
        switch (node.kind) {
            case ts.SyntaxKind.FunctionDeclaration:
                if (allowDeclarations) {
                    break;
                }
            // falls through
            case ts.SyntaxKind.FunctionExpression: {
                var f = node;
                if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
                    ctx.addFailureAtNode(utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile), Rule.FAILURE_STRING);
                }
            }
        }
        return ts.forEachChild(node, cb);
    });
}
github fossasia / susper.com / node_modules / tslint / lib / rules / whitespaceRule.js View on Github external
function checkEqualsGreaterThanTokenInNode(node) {
        if (!options.operator) {
            return;
        }
        var equalsGreaterThanToken = utils.getChildOfKind(node, ts.SyntaxKind.EqualsGreaterThanToken, sourceFile);
        // condition so we don't crash if the arrow is somehow missing
        if (equalsGreaterThanToken === undefined) {
            return;
        }
        checkForTrailingWhitespace(equalsGreaterThanToken.getFullStart());
        checkForTrailingWhitespace(equalsGreaterThanToken.getEnd());
    }
    function checkForTrailingWhitespace(position, whiteSpacePos) {
github palantir / tslint / src / rules / arrowReturnShorthandRule.ts View on Github external
function createFix(
    arrowFunction: ts.FunctionLikeDeclaration,
    body: ts.Block,
    expr: ts.Expression,
    text: string,
): Lint.Fix | undefined {
    const statement = expr.parent;
    const returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword)!;
    const arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken)!;
    const openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken)!;
    const closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken)!;
    const semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);

    const anyComments =
        hasComments(arrow) ||
        hasComments(openBrace) ||
        hasComments(statement) ||
        hasComments(returnKeyword) ||
        hasComments(expr) ||
        (semicolon !== undefined && hasComments(semicolon)) ||
        hasComments(closeBrace);
    return anyComments
        ? undefined
        : [
              // Object literal must be wrapped in `()`
github palantir / tslint / src / rules / onlyArrowFunctionsRule.ts View on Github external
return ts.forEachChild(sourceFile, function cb(node): void {
        switch (node.kind) {
            case ts.SyntaxKind.FunctionDeclaration:
                if (allowDeclarations) {
                    break;
                }
            // falls through
            case ts.SyntaxKind.FunctionExpression: {
                const f = node as ts.FunctionLikeDeclaration;
                if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
                    ctx.addFailureAtNode(
                        utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile)!,
                        Rule.FAILURE_STRING,
                    );
                }
            }
        }
        return ts.forEachChild(node, cb);
    });
}
github palantir / tslint / src / rules / arrowReturnShorthandRule.ts View on Github external
function createFix(
    arrowFunction: ts.FunctionLikeDeclaration,
    body: ts.Block,
    expr: ts.Expression,
    text: string,
): Lint.Fix | undefined {
    const statement = expr.parent;
    const returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword)!;
    const arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken)!;
    const openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken)!;
    const closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken)!;
    const semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);

    const anyComments =
        hasComments(arrow) ||
        hasComments(openBrace) ||
        hasComments(statement) ||
        hasComments(returnKeyword) ||
        hasComments(expr) ||
        (semicolon !== undefined && hasComments(semicolon)) ||
        hasComments(closeBrace);
    return anyComments
        ? undefined
        : [
              // Object literal must be wrapped in `()`
              ...(expr.kind === ts.SyntaxKind.ObjectLiteralExpression
                  ? [
github fossasia / susper.com / node_modules / tslint / lib / rules / arrowReturnShorthandRule.js View on Github external
function createFix(arrowFunction, body, expr, text) {
    var statement = expr.parent;
    var returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword);
    var arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken);
    var openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken);
    var closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken);
    var semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
    var anyComments = hasComments(arrow) ||
        hasComments(openBrace) ||
        hasComments(statement) ||
        hasComments(returnKeyword) ||
        hasComments(expr) ||
        (semicolon !== undefined && hasComments(semicolon)) ||
        hasComments(closeBrace);
    return anyComments
        ? undefined
        : (expr.kind === ts.SyntaxKind.ObjectLiteralExpression
            ? [
                Lint.Replacement.appendText(expr.getStart(), "("),
github fossasia / susper.com / node_modules / tslint / lib / rules / arrowReturnShorthandRule.js View on Github external
function createFix(arrowFunction, body, expr, text) {
    var statement = expr.parent;
    var returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword);
    var arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken);
    var openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken);
    var closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken);
    var semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
    var anyComments = hasComments(arrow) ||
        hasComments(openBrace) ||
        hasComments(statement) ||
        hasComments(returnKeyword) ||
        hasComments(expr) ||
        (semicolon !== undefined && hasComments(semicolon)) ||
        hasComments(closeBrace);
    return anyComments
        ? undefined
        : (expr.kind === ts.SyntaxKind.ObjectLiteralExpression
            ? [
                Lint.Replacement.appendText(expr.getStart(), "("),
                Lint.Replacement.appendText(expr.getEnd(), ")"),
            ]
github palantir / tslint / src / rules / typedefRule.ts View on Github external
private shouldIgnoreVariableDeclaration(node: ts.Node): boolean {
        const ignoreFunctions: boolean =
            this.options[OPTION_VARIABLE_DECLARATION_IGNORE_FUNCTION] === true;

        return (
            ignoreFunctions &&
            (utils.getChildOfKind(node, ts.SyntaxKind.ArrowFunction) !== undefined ||
                utils.getChildOfKind(node, ts.SyntaxKind.FunctionExpression) !== undefined)
        );
    }
github ajafff / tslint-consistent-codestyle / rules / preferWhileRule.ts View on Github external
public visitForStatement(node: ts.ForStatement) {
        if (node.initializer === undefined && node.incrementor === undefined) {
            const sourceFile = this.getSourceFile();
            const start = node.getStart(sourceFile);
            const closeParenEnd = utils.getChildOfKind(node, ts.SyntaxKind.CloseParenToken, sourceFile)!.getEnd();
            const width = closeParenEnd - start;
            let fix: Lint.Fix;
            if (node.condition === undefined) {
                fix = this.createFix(new Lint.Replacement(start, width, 'while (true)'));
            } else {
                const conditionEnd = node.condition.getEnd();
                fix = this.createFix(
                    new Lint.Replacement(start,
                                         node.condition.getStart(sourceFile) - start,
                                         'while ('),
                    new Lint.Replacement(conditionEnd,
                                         closeParenEnd - conditionEnd - 1,
                                         ''),
                );
            }
github nativescript-rtl / ui / node_modules / tslint / lib / rules / noUnnecessaryClassRule.js View on Github external
if (node.members.length === 0) {
            if (!this.options.allowEmptyClass) {
                this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword), Rule.FAILURE_EMPTY_CLASS);
            }
            return;
        }
        var allMembersAreConstructors = node.members.every(tsutils_1.isConstructorDeclaration);
        if (allMembersAreConstructors &&
            !this.options.allowConstructorOnly &&
            !node.members.some(isConstructorWithShorthandProps)) {
            this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword, this.sourceFile), Rule.FAILURE_CONSTRUCTOR_ONLY);
        }
        if (!allMembersAreConstructors &&
            !this.options.allowStaticOnly &&
            !node.members.some(isNonStaticMember)) {
            this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword, this.sourceFile), Rule.FAILURE_STATIC_ONLY);
        }
    };
    return NoUnnecessaryClassWalker;