Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'tsutils' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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;
}
}
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);
}
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)];
}
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) {
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) {
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),
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) {
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) {
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) {
// 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;
}