Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var NoUnsafeReturnRule = rule.CreateRule(rule.Rule{ Name: "no-unsafe-return", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { compilerOptions := ctx.Program.Options() isNoImplicitThis := utils.IsStrictCompilerOptionEnabled( compilerOptions, compilerOptions.NoImplicitThis, ) checkReturn := func( returnNode *ast.Node, reportingNode *ast.Node, ) { t := ctx.TypeChecker.GetTypeAtLocation(returnNode) anyType := utils.DiscriminateAnyType( t, ctx.TypeChecker, ctx.Program, returnNode, ) functionNode := utils.GetParentFunctionNode(returnNode) if functionNode == nil { return } returnNodeType := utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, returnNode) // function expressions will not have their return type modified based on receiver typing // so we have to use the contextual typing in these cases, i.e. // const foo1: () => Set<string> = () => new Set<any>(); // the return type of the arrow function is Set<any> even though the variable is typed as Set<string> var functionType *checker.Type if ast.IsFunctionExpression(functionNode) || ast.IsArrowFunction(functionNode) { functionType = utils.GetContextualType(ctx.TypeChecker, functionNode) } if functionType == nil { functionType = ctx.TypeChecker.GetTypeAtLocation(functionNode) } callSignatures := utils.CollectAllCallSignatures(ctx.TypeChecker, functionType) if functionNode.Type() != nil { for _, signature := range callSignatures { signatureReturnType := checker.Checker_getReturnTypeOfSignature(ctx.TypeChecker, signature) if returnNodeType == signatureReturnType || utils.IsTypeFlagSet( signatureReturnType, checker.TypeFlagsAny|checker.TypeFlagsUnknown, ) { return } if ast.HasSyntacticModifier(functionNode, ast.ModifierFlagsAsync) { awaitedSignatureReturnType := checker.Checker_getAwaitedType(ctx.TypeChecker, signatureReturnType) awaitedReturnNodeType := checker.Checker_getAwaitedType(ctx.TypeChecker, returnNodeType) if awaitedSignatureReturnType == awaitedReturnNodeType || (awaitedSignatureReturnType != nil && utils.IsTypeFlagSet(awaitedSignatureReturnType, checker.TypeFlagsAny|checker.TypeFlagsUnknown)) { return } } } } if anyType != utils.DiscriminatedAnyTypeSafe { for _, signature := range callSignatures { functionReturnType := checker.Checker_getReturnTypeOfSignature(ctx.TypeChecker, signature) if anyType == utils.DiscriminatedAnyTypeAny && utils.IsTypeUnknownType(functionReturnType) { return } if anyType == utils.DiscriminatedAnyTypeAnyArray && utils.IsTypeUnknownArrayType(functionReturnType, ctx.TypeChecker) { return } awaitedType := checker.Checker_getAwaitedType(ctx.TypeChecker, functionReturnType) if awaitedType != nil && anyType == utils.DiscriminatedAnyTypePromiseAny && utils.IsTypeUnknownType(awaitedType) { return } } if anyType == utils.DiscriminatedAnyTypePromiseAny && !ast.HasSyntacticModifier(functionNode, ast.ModifierFlagsAsync) { return } var typeString string if utils.IsIntrinsicErrorType(returnNodeType) { typeString = "error" } else if anyType == utils.DiscriminatedAnyTypeAny { typeString = "`any`" } else if anyType == utils.DiscriminatedAnyTypePromiseAny { typeString = "`Promise<any>`" } else { typeString = "`any[]`" } if !isNoImplicitThis { thisExpression := utils.GetThisExpression(returnNode) if thisExpression != nil && utils.IsTypeAnyType(utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, thisExpression)) { ctx.ReportNode(reportingNode, buildUnsafeReturnThisMessage(typeString)) return } } ctx.ReportNode(reportingNode, buildUnsafeReturnMessage(typeString)) return } if len(callSignatures) < 1 { return } signature := callSignatures[0] functionReturnType := checker.Checker_getReturnTypeOfSignature(ctx.TypeChecker, signature) receiver, sender, unsafe := utils.IsUnsafeAssignment( returnNodeType, functionReturnType, ctx.TypeChecker, returnNode, ) if !unsafe { return } ctx.ReportNode(reportingNode, buildUnsafeReturnAssignmentMessage(ctx.TypeChecker.TypeToString(sender), ctx.TypeChecker.TypeToString(receiver))) } return rule.RuleListeners{ ast.KindArrowFunction: func(node *ast.Node) { body := node.Body() if !ast.IsBlock(body) { checkReturn(body, body) } }, ast.KindReturnStatement: func(node *ast.Node) { argument := node.Expression() if argument == nil { return } checkReturn(argument, node) }, } }, })
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.