How to use the tsutils.isTypeFlagSet 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 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;
    }
github palantir / tslint / src / rules / noUnsafeAnyRule.ts View on Github external
function isAny(type: ts.Type | undefined, orUnknown: boolean = false): boolean {
    return (
        type !== undefined &&
        (isTypeFlagSet(type, ts.TypeFlags.Any) ||
            (orUnknown && isTypeFlagSet(type, ts.TypeFlags.Unknown)))
    );
}