How to use tsutils - 10 common examples

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 microsoft / reactxp / src / tslint / groupedImportRule.ts View on Github external
private _checkImportType(node: ts.ImportEqualsDeclaration | ts.ImportDeclaration): ImportType {
        let modulePath: string | undefined;
        if (tsutils.isImportEqualsDeclaration(node)) {
            if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
                const matches = node.moduleReference.getFullText().match(/require\s*\(\s*'([^']+)'\s*\)/);
                if (matches && matches.length === 2) {
                    modulePath = matches[1];
                } else {
                    console.log('Unknown Missed Regex: ' + node.moduleReference.kind + '/' + node.moduleReference.getFullText());
                }
            }
        }

        if (tsutils.isImportDeclaration(node)) {
            modulePath = node.moduleSpecifier.getText().replace(/'/g, '');
        }

        if (modulePath) {
            // Assume that "@" is a shortcut for a relative path.
            if (modulePath[0] === '.' || modulePath[0] === '@') {
                return ImportType.Relative;
            } else {
                return ImportType.Ambient;
            }
        }

        return ImportType.None;
    }
}
github ajafff / tslint-consistent-codestyle / rules / earlyExitRule.ts View on Github external
function getExit(node: ts.IfStatement): string | undefined {
    const parent = node.parent!;
    if (isBlock(parent)) {
        const container = parent.parent!;
        return isCaseOrDefaultClause(container) && container.statements.length === 1
            ? getCaseClauseExit(container, parent, node)
            // Must be the last statement in the block
            : isLastStatement(node, parent.statements) ? getEarlyExitKind(container) : undefined;
    }
    return isCaseOrDefaultClause(parent)
        ? getCaseClauseExit(parent, parent, node)
        // This is the only statement in its container, so of course it's the final statement.
        : getEarlyExitKind(parent);
}
github palantir / tslint / src / rules / strictComparisonsRule.ts View on Github external
function getTypes(types: ts.Type): TypeKind[] {
    // Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
    const baseType = ((types as any) as { baseType: ts.LiteralType }).baseType;
    return isUnionType(types)
        ? Array.from(new Set(types.types.map(getKind)))
        : isTypeFlagSet(types, ts.TypeFlags.EnumLiteral) && typeof baseType !== "undefined"
        ? [getKind(baseType)]
        : [getKind(types)];
}
github nativescript-rtl / ui / node_modules / tslint / lib / rules / strictBooleanExpressionsRule.js View on Github external
function isBooleanUndefined(type) {
    var isTruthy = false;
    for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
        var ty = _a[_i];
        if (tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.Boolean)) {
            isTruthy = true;
        }
        else if (tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.BooleanLiteral)) {
            isTruthy = isTruthy || ty.intrinsicName === "true";
            // tslint:disable-next-line:no-bitwise
        }
        else if (!tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.Void | ts.TypeFlags.Undefined)) {
            return undefined;
        }
    }
    return isTruthy;
}
function handleUnion(type, options) {
github fossasia / susper.com / node_modules / tslint / lib / rules / strictBooleanExpressionsRule.js View on Github external
function getTypeFailure(type, options) {
    if (isUnionType(type)) {
        return handleUnion(type, options);
    }
    var kind = getKind(type);
    var failure = failureForKind(kind, /*isInUnion*/ false, options);
    if (failure !== undefined) {
        return failure;
    }
    switch (triState(kind)) {
        case true:
            // Allow 'any'. Allow 'true' itself, but not any other always-truthy type.
            // tslint:disable-next-line no-bitwise
            return tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.BooleanLiteral)
                ? undefined
                : 0 /* AlwaysTruthy */;
        case false:
            // Allow 'false' itself, but not any other always-falsy type
            return tsutils_1.isTypeFlagSet(type, ts.TypeFlags.BooleanLiteral)
                ? undefined
                : 1 /* AlwaysFalsy */;
        case undefined:
            return undefined;
    }
}
function isBooleanUndefined(type) {
github palantir / tslint / src / rules / noUnnecessaryTypeAssertionRule.ts View on Github external
private verifyCast(node: ts.AssertionExpression) {
        if (this.options.indexOf(node.type.getText(this.sourceFile)) !== -1) {
            return;
        }
        const castType = this.checker.getTypeAtLocation(node);

        if (
            isTypeFlagSet(castType, ts.TypeFlags.Literal) ||
            (isObjectType(castType) &&
                (isObjectFlagSet(castType, ts.ObjectFlags.Tuple) || couldBeTupleType(castType)))
        ) {
            // It's not always safe to remove a cast to a literal type or tuple
            // type, as those types are sometimes widened without the cast.
            return;
        }

        const uncastType = this.checker.getTypeAtLocation(node.expression);
        if (uncastType === castType) {
            this.addFailureAtNode(
                node,
                Rule.FAILURE_STRING,
                node.kind === ts.SyntaxKind.TypeAssertionExpression
                    ? Lint.Replacement.deleteFromTo(node.getStart(), node.expression.getStart())
                    : Lint.Replacement.deleteFromTo(node.expression.end, node.end),
github nativescript-rtl / ui / node_modules / tslint / lib / rules / strictBooleanExpressionsRule.js View on Github external
function getTypeFailure(type, options) {
    if (isUnionType(type)) {
        return handleUnion(type, options);
    }
    var kind = getKind(type);
    var failure = failureForKind(kind, /*isInUnion*/ false, options);
    if (failure !== undefined) {
        return failure;
    }
    switch (triState(kind)) {
        case true:
            // Allow 'any'. Allow 'true' itself, but not any other always-truthy type.
            return tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.BooleanLiteral)
                ? undefined
                : 0 /* AlwaysTruthy */;
        case false:
            // Allow 'false' itself, but not any other always-falsy type
            return tsutils_1.isTypeFlagSet(type, ts.TypeFlags.BooleanLiteral)
                ? undefined
                : 1 /* AlwaysFalsy */;
        case undefined:
            return undefined;
    }
}
function isBooleanUndefined(type) {
github nativescript-rtl / ui / node_modules / tslint / lib / rules / strictBooleanExpressionsRule.js View on Github external
function isBooleanUndefined(type) {
    var isTruthy = false;
    for (var _i = 0, _a = type.types; _i < _a.length; _i++) {
        var ty = _a[_i];
        if (tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.Boolean)) {
            isTruthy = true;
        }
        else if (tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.BooleanLiteral)) {
            isTruthy = isTruthy || ty.intrinsicName === "true";
            // tslint:disable-next-line:no-bitwise
        }
        else if (!tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.Void | ts.TypeFlags.Undefined)) {
            return undefined;
        }
    }
    return isTruthy;
}
function handleUnion(type, options) {
github nativescript-rtl / ui / node_modules / tslint / lib / rules / restrictPlusOperandsRule.js View on Github external
function getBaseTypeOfLiteralType(type) {
    if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.StringLiteral) ||
        tsutils_1.isTypeFlagSet(type, ts.TypeFlags.String)) {
        return "string";
    }
    else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.NumberLiteral) ||
        tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Number)) {
        return "number";
    }
    else if (tsutils_1.isUnionType(type) && !tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Enum)) {
        var types = type.types.map(getBaseTypeOfLiteralType);
        return allSame(types) ? types[0] : "invalid";
    }
    else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.EnumLiteral)) {
        // Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
        getBaseTypeOfLiteralType(type.baseType);
    }
    return "invalid";
}
function allSame(array) {
github palantir / tslint / src / rules / returnUndefinedRule.ts View on Github external
// Handle generator functions/methods:
        if (node.asteriskToken !== undefined) {
            return ReturnKind.Void;
        }

        const contextual =
            isFunctionExpressionLike(node) && node.type === undefined
                ? tryGetReturnType(checker.getContextualType(node), checker)
                : undefined;
        const returnType =
            contextual !== undefined
                ? contextual
                : tryGetReturnType(checker.getTypeAtLocation(node), checker);

        if (returnType === undefined || isTypeFlagSet(returnType, ts.TypeFlags.Any)) {
            return undefined;
        }

        const effectivelyVoidChecker = hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword)
            ? isEffectivelyVoidPromise
            : isEffectivelyVoid;

        if (effectivelyVoidChecker(returnType)) {
            return ReturnKind.Void;
        }

        return ReturnKind.Value;
    }