|
| 1 | +package void_use_memo |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/microsoft/typescript-go/shim/ast" |
| 7 | + "github.com/web-infra-dev/rslint/internal/plugins/react_hooks/react_hooksutil" |
| 8 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + missingReturnReason = "useMemo() callbacks must return a value" |
| 13 | + missingReturnDescription = "This useMemo() callback doesn't return a value. useMemo() is for computing and caching values, not for arbitrary side effects" |
| 14 | + unusedResultReason = "useMemo() result is unused" |
| 15 | + unusedResultDescription = "This useMemo() value is unused. useMemo() is for computing and caching values, not for arbitrary side effects" |
| 16 | +) |
| 17 | + |
| 18 | +type voidUseMemoState struct { |
| 19 | + ctx rule.RuleContext |
| 20 | +} |
| 21 | + |
| 22 | +// VoidUseMemoRule is the rslint port of upstream `react-hooks/void-use-memo`. |
| 23 | +// |
| 24 | +// Upstream emits this rule from React Compiler's VoidUseMemo diagnostic |
| 25 | +// category. This port validates the published local contract: useMemo |
| 26 | +// callbacks must contain an explicit or implicit return, and a returned |
| 27 | +// useMemo value must not be discarded as a bare expression statement. |
| 28 | +var VoidUseMemoRule = rule.Rule{ |
| 29 | + Name: "react-hooks/void-use-memo", |
| 30 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 31 | + state := &voidUseMemoState{ctx: ctx} |
| 32 | + return rule.RuleListeners{ |
| 33 | + ast.KindCallExpression: state.processCallExpression, |
| 34 | + } |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +func (state *voidUseMemoState) processCallExpression(node *ast.Node) { |
| 39 | + call := node.AsCallExpression() |
| 40 | + if call == nil || !react_hooksutil.IsManualUseMemoCallee(call.Expression, state.ctx.TypeChecker) { |
| 41 | + return |
| 42 | + } |
| 43 | + args := call.Arguments |
| 44 | + if args == nil || len(args.Nodes) == 0 { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + callback := ast.SkipParentheses(args.Nodes[0]) |
| 49 | + if callback == nil || callback.Kind == ast.KindSpreadElement || !ast.IsFunctionExpressionOrArrowFunction(callback) { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if !hasUseMemoReturn(callback) { |
| 54 | + state.ctx.ReportNode(callback, buildVoidUseMemoMessage("missingReturn", missingReturnReason, missingReturnDescription)) |
| 55 | + return |
| 56 | + } |
| 57 | + if isUnusedUseMemoResult(node) { |
| 58 | + state.ctx.ReportNode(reportableCallee(call.Expression), buildVoidUseMemoMessage("unusedResult", unusedResultReason, unusedResultDescription)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func hasUseMemoReturn(callback *ast.Node) bool { |
| 63 | + if callback == nil { |
| 64 | + return false |
| 65 | + } |
| 66 | + body := react_hooksutil.GetFunctionBody(callback) |
| 67 | + if body == nil { |
| 68 | + return false |
| 69 | + } |
| 70 | + // React Compiler treats arrow expression bodies as implicit returns. |
| 71 | + if callback.Kind == ast.KindArrowFunction { |
| 72 | + if body.Kind != ast.KindBlock { |
| 73 | + return true |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // React Compiler's HIR-based terminal scan does not count returns from the |
| 78 | + // try block of a try/finally statement or from its finally block. |
| 79 | + return ast.ForEachReturnStatement(body, func(stmt *ast.Node) bool { |
| 80 | + return !isIgnoredTryFinallyReturn(stmt, callback) |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +func isIgnoredTryFinallyReturn(ret *ast.Node, callback *ast.Node) bool { |
| 85 | + child := ret |
| 86 | + for parent := ret.Parent; parent != nil && parent != callback; parent = parent.Parent { |
| 87 | + if react_hooksutil.IsFunctionLikeContainer(parent) { |
| 88 | + return false |
| 89 | + } |
| 90 | + if parent.Kind == ast.KindTryStatement { |
| 91 | + tryStmt := parent.AsTryStatement() |
| 92 | + if tryStmt != nil && tryStmt.FinallyBlock != nil { |
| 93 | + if tryStmt.TryBlock != nil && tryStmt.TryBlock.AsNode() == child { |
| 94 | + return true |
| 95 | + } |
| 96 | + if tryStmt.FinallyBlock.AsNode() == child { |
| 97 | + return true |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + child = parent |
| 102 | + } |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +func isUnusedUseMemoResult(call *ast.Node) bool { |
| 107 | + current := call |
| 108 | + for { |
| 109 | + child, parent := walkUpResultWrappers(current, current == call) |
| 110 | + if parent == nil { |
| 111 | + return false |
| 112 | + } |
| 113 | + switch parent.Kind { |
| 114 | + case ast.KindExpressionStatement: |
| 115 | + return current == call |
| 116 | + case ast.KindBinaryExpression: |
| 117 | + // In a comma expression, every operand except the final one is |
| 118 | + // discarded even when the comma expression's final result is used. |
| 119 | + binary := parent.AsBinaryExpression() |
| 120 | + if binary == nil || binary.OperatorToken == nil || binary.OperatorToken.Kind != ast.KindCommaToken { |
| 121 | + return false |
| 122 | + } |
| 123 | + if binary.Left == child { |
| 124 | + return true |
| 125 | + } |
| 126 | + if binary.Right == child { |
| 127 | + current = parent |
| 128 | + continue |
| 129 | + } |
| 130 | + return false |
| 131 | + default: |
| 132 | + return false |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func walkUpResultWrappers(node *ast.Node, allowNonNull bool) (*ast.Node, *ast.Node) { |
| 138 | + child := node |
| 139 | + parent := child.Parent |
| 140 | + kinds := ast.OEKParentheses |
| 141 | + if allowNonNull { |
| 142 | + kinds |= ast.OEKNonNullAssertions |
| 143 | + } |
| 144 | + for parent != nil && ast.IsOuterExpression(parent, kinds) { |
| 145 | + child = parent |
| 146 | + parent = child.Parent |
| 147 | + } |
| 148 | + return child, parent |
| 149 | +} |
| 150 | + |
| 151 | +func reportableCallee(callee *ast.Node) *ast.Node { |
| 152 | + if unwrapped := ast.SkipParentheses(callee); unwrapped != nil { |
| 153 | + return unwrapped |
| 154 | + } |
| 155 | + return callee |
| 156 | +} |
| 157 | + |
| 158 | +func buildVoidUseMemoMessage(id, reason, description string) rule.RuleMessage { |
| 159 | + return rule.RuleMessage{ |
| 160 | + Id: id, |
| 161 | + Description: fmt.Sprintf("Error: %s\n\n%s.", reason, description), |
| 162 | + Data: map[string]string{ |
| 163 | + "description": description, |
| 164 | + "reason": reason, |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
0 commit comments