|
| 1 | +package prefer_rest_params |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/microsoft/typescript-go/shim/ast" |
| 5 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 6 | +) |
| 7 | + |
| 8 | +// https://eslint.org/docs/latest/rules/prefer-rest-params |
| 9 | +var PreferRestParamsRule = rule.Rule{ |
| 10 | + Name: "prefer-rest-params", |
| 11 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 12 | + return rule.RuleListeners{ |
| 13 | + ast.KindIdentifier: func(node *ast.Node) { |
| 14 | + if node.Text() != "arguments" { |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + // Skip if this is a property name (e.g., obj.arguments) |
| 19 | + if isPropertyName(node) { |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + // Skip if this is a declaration name (var arguments, function arguments(params)) |
| 24 | + if isDeclarationName(node) { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + // Find the enclosing function (not arrow) |
| 29 | + enclosingFunc := findEnclosingFunction(node) |
| 30 | + if enclosingFunc == nil { |
| 31 | + // arguments at module/program level - not in a function |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // If enclosing function is arrow function, skip (arrows don't have their own arguments) |
| 36 | + if enclosingFunc.Kind == ast.KindArrowFunction { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // Check if `arguments` is shadowed by a parameter or local var |
| 41 | + if isShadowedInFunction(enclosingFunc) { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + // Check if this is a non-computed member access (arguments.length is OK) |
| 46 | + if isNonComputedMemberAccess(node) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + ctx.ReportNode(node, rule.RuleMessage{ |
| 51 | + Id: "preferRestParams", |
| 52 | + Description: "Use the rest parameters instead of 'arguments'.", |
| 53 | + }) |
| 54 | + }, |
| 55 | + } |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +// isPropertyName checks if the node is the property name part of a property access (e.g., obj.arguments). |
| 60 | +func isPropertyName(node *ast.Node) bool { |
| 61 | + parent := node.Parent |
| 62 | + if parent == nil { |
| 63 | + return false |
| 64 | + } |
| 65 | + if parent.Kind == ast.KindPropertyAccessExpression { |
| 66 | + propAccess := parent.AsPropertyAccessExpression() |
| 67 | + if propAccess != nil && propAccess.Name() == node { |
| 68 | + return true |
| 69 | + } |
| 70 | + } |
| 71 | + return false |
| 72 | +} |
| 73 | + |
| 74 | +// isDeclarationName checks if the node is the name of a declaration (parameter, variable, function). |
| 75 | +func isDeclarationName(node *ast.Node) bool { |
| 76 | + parent := node.Parent |
| 77 | + if parent == nil { |
| 78 | + return false |
| 79 | + } |
| 80 | + switch parent.Kind { |
| 81 | + case ast.KindParameter: |
| 82 | + param := parent.AsParameterDeclaration() |
| 83 | + if param != nil && param.Name() == node { |
| 84 | + return true |
| 85 | + } |
| 86 | + case ast.KindVariableDeclaration: |
| 87 | + varDecl := parent.AsVariableDeclaration() |
| 88 | + if varDecl != nil && varDecl.Name() == node { |
| 89 | + return true |
| 90 | + } |
| 91 | + case ast.KindFunctionDeclaration: |
| 92 | + funcDecl := parent.AsFunctionDeclaration() |
| 93 | + if funcDecl != nil && funcDecl.Name() == node { |
| 94 | + return true |
| 95 | + } |
| 96 | + } |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +// findEnclosingFunction walks up the AST to find the nearest enclosing function-like node. |
| 101 | +// Returns nil if none is found. |
| 102 | +func findEnclosingFunction(node *ast.Node) *ast.Node { |
| 103 | + current := node.Parent |
| 104 | + for current != nil { |
| 105 | + switch current.Kind { |
| 106 | + case ast.KindFunctionDeclaration, ast.KindFunctionExpression, ast.KindArrowFunction, |
| 107 | + ast.KindMethodDeclaration, ast.KindConstructor, ast.KindGetAccessor, ast.KindSetAccessor: |
| 108 | + return current |
| 109 | + } |
| 110 | + current = current.Parent |
| 111 | + } |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +// isShadowedInFunction checks if `arguments` is shadowed by a parameter or a var declaration |
| 116 | +// anywhere inside the given function node. var declarations are hoisted to function scope, |
| 117 | +// so `var arguments` in any nested block still shadows the built-in arguments object. |
| 118 | +func isShadowedInFunction(funcNode *ast.Node) bool { |
| 119 | + // Check parameters |
| 120 | + paramList := funcNode.ParameterList() |
| 121 | + if paramList != nil { |
| 122 | + for _, param := range paramList.Nodes { |
| 123 | + name := param.Name() |
| 124 | + if name != nil && name.Kind == ast.KindIdentifier && name.Text() == "arguments" { |
| 125 | + return true |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Recursively check for var declarations named "arguments" in the function body. |
| 131 | + body := funcNode.Body() |
| 132 | + if body == nil { |
| 133 | + return false |
| 134 | + } |
| 135 | + |
| 136 | + found := false |
| 137 | + var walk func(*ast.Node) |
| 138 | + walk = func(n *ast.Node) { |
| 139 | + if n == nil || found { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + // Don't recurse into nested functions (they have their own arguments) |
| 144 | + if n != funcNode { |
| 145 | + switch n.Kind { |
| 146 | + case ast.KindFunctionDeclaration, ast.KindFunctionExpression, |
| 147 | + ast.KindArrowFunction, ast.KindMethodDeclaration, |
| 148 | + ast.KindConstructor, ast.KindGetAccessor, ast.KindSetAccessor: |
| 149 | + return |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if n.Kind == ast.KindVariableDeclaration { |
| 154 | + varDecl := n.AsVariableDeclaration() |
| 155 | + if varDecl != nil && varDecl.Name() != nil && |
| 156 | + varDecl.Name().Kind == ast.KindIdentifier && |
| 157 | + varDecl.Name().Text() == "arguments" { |
| 158 | + found = true |
| 159 | + return |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + n.ForEachChild(func(child *ast.Node) bool { |
| 164 | + walk(child) |
| 165 | + return found |
| 166 | + }) |
| 167 | + } |
| 168 | + walk(body) |
| 169 | + return found |
| 170 | +} |
| 171 | + |
| 172 | +// isNonComputedMemberAccess checks if the node is the object of a non-computed property access |
| 173 | +// (e.g., arguments.length). In that case, the usage is OK and should not be flagged. |
| 174 | +func isNonComputedMemberAccess(node *ast.Node) bool { |
| 175 | + parent := node.Parent |
| 176 | + if parent == nil { |
| 177 | + return false |
| 178 | + } |
| 179 | + if parent.Kind == ast.KindPropertyAccessExpression { |
| 180 | + propAccess := parent.AsPropertyAccessExpression() |
| 181 | + if propAccess != nil && propAccess.Expression == node { |
| 182 | + return true |
| 183 | + } |
| 184 | + } |
| 185 | + return false |
| 186 | +} |
0 commit comments