|
| 1 | +package no_const_assign |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/microsoft/typescript-go/shim/ast" |
| 5 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 6 | +) |
| 7 | + |
| 8 | +// Message builder |
| 9 | +func buildConstMessage(name string) rule.RuleMessage { |
| 10 | + return rule.RuleMessage{ |
| 11 | + Id: "const", |
| 12 | + Description: "'" + name + "' is constant.", |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +// isConstBinding checks if a variable declaration is a const binding |
| 17 | +func isConstBinding(node *ast.Node) bool { |
| 18 | + if node == nil || node.Kind != ast.KindVariableDeclarationList { |
| 19 | + return false |
| 20 | + } |
| 21 | + |
| 22 | + varDeclList := node.AsVariableDeclarationList() |
| 23 | + if varDeclList == nil { |
| 24 | + return false |
| 25 | + } |
| 26 | + |
| 27 | + // Check if the declaration is const (or using/await using in the future) |
| 28 | + // In TypeScript AST, const declarations have flags |
| 29 | + return (varDeclList.Flags & ast.NodeFlagsConst) != 0 |
| 30 | +} |
| 31 | + |
| 32 | +// getIdentifierName gets the name of an identifier node |
| 33 | +func getIdentifierName(node *ast.Node) string { |
| 34 | + if node == nil || node.Kind != ast.KindIdentifier { |
| 35 | + return "" |
| 36 | + } |
| 37 | + |
| 38 | + identifier := node.AsIdentifier() |
| 39 | + if identifier == nil { |
| 40 | + return "" |
| 41 | + } |
| 42 | + |
| 43 | + return identifier.EscapedText |
| 44 | +} |
| 45 | + |
| 46 | +// isWriteReference checks if a reference is a write operation (assignment, increment, decrement, etc.) |
| 47 | +func isWriteReference(node *ast.Node) bool { |
| 48 | + if node == nil { |
| 49 | + return false |
| 50 | + } |
| 51 | + |
| 52 | + parent := node.Parent |
| 53 | + if parent == nil { |
| 54 | + return false |
| 55 | + } |
| 56 | + |
| 57 | + switch parent.Kind { |
| 58 | + case ast.KindBinaryExpression: |
| 59 | + // Check if this is an assignment operation |
| 60 | + binary := parent.AsBinaryExpression() |
| 61 | + if binary == nil { |
| 62 | + return false |
| 63 | + } |
| 64 | + |
| 65 | + // Check if the node is on the left side of an assignment |
| 66 | + if binary.Left != node { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + // Check for all assignment operators |
| 71 | + switch binary.OperatorToken.Kind { |
| 72 | + case ast.KindEqualsToken, // = |
| 73 | + ast.KindPlusEqualsToken, // += |
| 74 | + ast.KindMinusEqualsToken, // -= |
| 75 | + ast.KindAsteriskEqualsToken, // *= |
| 76 | + ast.KindSlashEqualsToken, // /= |
| 77 | + ast.KindPercentEqualsToken, // %= |
| 78 | + ast.KindAsteriskAsteriskEqualsToken, // **= |
| 79 | + ast.KindLessThanLessThanEqualsToken, // <<= |
| 80 | + ast.KindGreaterThanGreaterThanEqualsToken, // >>= |
| 81 | + ast.KindGreaterThanGreaterThanGreaterThanEqualsToken, // >>>= |
| 82 | + ast.KindAmpersandEqualsToken, // &= |
| 83 | + ast.KindBarEqualsToken, // |= |
| 84 | + ast.KindCaretEqualsToken, // ^= |
| 85 | + ast.KindQuestionQuestionEqualsToken, // ??= |
| 86 | + ast.KindAmpersandAmpersandEqualsToken, // &&= |
| 87 | + ast.KindBarBarEqualsToken: // ||= |
| 88 | + return true |
| 89 | + } |
| 90 | + |
| 91 | + case ast.KindPrefixUnaryExpression: |
| 92 | + // Check for ++ and -- prefix operators |
| 93 | + prefix := parent.AsPrefixUnaryExpression() |
| 94 | + if prefix == nil { |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + switch prefix.Operator { |
| 99 | + case ast.KindPlusPlusToken, // ++ |
| 100 | + ast.KindMinusMinusToken: // -- |
| 101 | + return true |
| 102 | + } |
| 103 | + |
| 104 | + case ast.KindPostfixUnaryExpression: |
| 105 | + // Check for ++ and -- postfix operators |
| 106 | + postfix := parent.AsPostfixUnaryExpression() |
| 107 | + if postfix == nil { |
| 108 | + return false |
| 109 | + } |
| 110 | + |
| 111 | + switch postfix.Operator { |
| 112 | + case ast.KindPlusPlusToken, // ++ |
| 113 | + ast.KindMinusMinusToken: // -- |
| 114 | + return true |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return false |
| 119 | +} |
| 120 | + |
| 121 | +// isInInitializer checks if a node is in the initializer of its declaration |
| 122 | +func isInInitializer(identifierNode *ast.Node, declNode *ast.Node) bool { |
| 123 | + if identifierNode == nil || declNode == nil { |
| 124 | + return false |
| 125 | + } |
| 126 | + |
| 127 | + // Walk up from the identifier to see if we're in the initializer |
| 128 | + current := identifierNode.Parent |
| 129 | + for current != nil && current != declNode { |
| 130 | + // If we hit a VariableDeclaration, check if we're in its initializer |
| 131 | + if current.Kind == ast.KindVariableDeclaration { |
| 132 | + varDecl := current.AsVariableDeclaration() |
| 133 | + if varDecl != nil && varDecl.Initializer != nil { |
| 134 | + // Check if the identifier is within the initializer |
| 135 | + if containsNode(varDecl.Initializer, identifierNode) { |
| 136 | + return true |
| 137 | + } |
| 138 | + } |
| 139 | + break |
| 140 | + } |
| 141 | + current = current.Parent |
| 142 | + } |
| 143 | + |
| 144 | + return false |
| 145 | +} |
| 146 | + |
| 147 | +// containsNode checks if a root node contains a target node in its subtree |
| 148 | +func containsNode(root, target *ast.Node) bool { |
| 149 | + if root == nil || target == nil { |
| 150 | + return false |
| 151 | + } |
| 152 | + if root == target { |
| 153 | + return true |
| 154 | + } |
| 155 | + |
| 156 | + // Walk up from target to see if we reach root |
| 157 | + current := target.Parent |
| 158 | + for current != nil { |
| 159 | + if current == root { |
| 160 | + return true |
| 161 | + } |
| 162 | + current = current.Parent |
| 163 | + } |
| 164 | + |
| 165 | + return false |
| 166 | +} |
| 167 | + |
| 168 | +// findVariableDeclaration finds the declaration for a given identifier |
| 169 | +func findVariableDeclaration(identifier *ast.Node, variableDeclarationList *ast.Node) *ast.Node { |
| 170 | + if identifier == nil || variableDeclarationList == nil { |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + identName := getIdentifierName(identifier) |
| 175 | + if identName == "" { |
| 176 | + return nil |
| 177 | + } |
| 178 | + |
| 179 | + varDeclList := variableDeclarationList.AsVariableDeclarationList() |
| 180 | + if varDeclList == nil || varDeclList.Declarations == nil { |
| 181 | + return nil |
| 182 | + } |
| 183 | + |
| 184 | + // Search through all declarations |
| 185 | + for _, decl := range varDeclList.Declarations.Slice() { |
| 186 | + if decl.Kind != ast.KindVariableDeclaration { |
| 187 | + continue |
| 188 | + } |
| 189 | + |
| 190 | + varDecl := decl.AsVariableDeclaration() |
| 191 | + if varDecl == nil || varDecl.Name == nil { |
| 192 | + continue |
| 193 | + } |
| 194 | + |
| 195 | + // Check if this declaration matches our identifier |
| 196 | + if matchesIdentifier(varDecl.Name, identName) { |
| 197 | + return decl |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return nil |
| 202 | +} |
| 203 | + |
| 204 | +// matchesIdentifier checks if a binding name matches an identifier |
| 205 | +func matchesIdentifier(bindingName *ast.Node, identName string) bool { |
| 206 | + if bindingName == nil { |
| 207 | + return false |
| 208 | + } |
| 209 | + |
| 210 | + switch bindingName.Kind { |
| 211 | + case ast.KindIdentifier: |
| 212 | + return getIdentifierName(bindingName) == identName |
| 213 | + |
| 214 | + case ast.KindObjectBindingPattern: |
| 215 | + // Check if any element in the object binding matches |
| 216 | + objBinding := bindingName.AsObjectBindingPattern() |
| 217 | + if objBinding == nil || objBinding.Elements == nil { |
| 218 | + return false |
| 219 | + } |
| 220 | + |
| 221 | + for _, elem := range objBinding.Elements.Slice() { |
| 222 | + if elem.Kind == ast.KindBindingElement { |
| 223 | + bindingElem := elem.AsBindingElement() |
| 224 | + if bindingElem != nil && bindingElem.Name != nil { |
| 225 | + if matchesIdentifier(bindingElem.Name, identName) { |
| 226 | + return true |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + case ast.KindArrayBindingPattern: |
| 233 | + // Check if any element in the array binding matches |
| 234 | + arrBinding := bindingName.AsArrayBindingPattern() |
| 235 | + if arrBinding == nil || arrBinding.Elements == nil { |
| 236 | + return false |
| 237 | + } |
| 238 | + |
| 239 | + for _, elem := range arrBinding.Elements.Slice() { |
| 240 | + if elem.Kind == ast.KindBindingElement { |
| 241 | + bindingElem := elem.AsBindingElement() |
| 242 | + if bindingElem != nil && bindingElem.Name != nil { |
| 243 | + if matchesIdentifier(bindingElem.Name, identName) { |
| 244 | + return true |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return false |
| 252 | +} |
| 253 | + |
| 254 | +// NoConstAssignRule disallows reassigning const variables |
| 255 | +var NoConstAssignRule = rule.CreateRule(rule.Rule{ |
| 256 | + Name: "no-const-assign", |
| 257 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 258 | + // Track const declarations and their identifiers |
| 259 | + constDeclarations := make(map[string]*ast.Node) // maps identifier name to declaration list node |
| 260 | + |
| 261 | + return rule.RuleListeners{ |
| 262 | + // Track const variable declarations |
| 263 | + ast.KindVariableDeclarationList: func(node *ast.Node) { |
| 264 | + if !isConstBinding(node) { |
| 265 | + return |
| 266 | + } |
| 267 | + |
| 268 | + varDeclList := node.AsVariableDeclarationList() |
| 269 | + if varDeclList == nil || varDeclList.Declarations == nil { |
| 270 | + return |
| 271 | + } |
| 272 | + |
| 273 | + // Track all identifiers declared as const |
| 274 | + for _, decl := range varDeclList.Declarations.Slice() { |
| 275 | + if decl.Kind != ast.KindVariableDeclaration { |
| 276 | + continue |
| 277 | + } |
| 278 | + |
| 279 | + varDecl := decl.AsVariableDeclaration() |
| 280 | + if varDecl == nil || varDecl.Name == nil { |
| 281 | + continue |
| 282 | + } |
| 283 | + |
| 284 | + // Collect all identifiers from the binding name |
| 285 | + collectIdentifiers(varDecl.Name, node, constDeclarations) |
| 286 | + } |
| 287 | + }, |
| 288 | + |
| 289 | + // Check for reassignments to const variables |
| 290 | + ast.KindIdentifier: func(node *ast.Node) { |
| 291 | + identName := getIdentifierName(node) |
| 292 | + if identName == "" { |
| 293 | + return |
| 294 | + } |
| 295 | + |
| 296 | + // Check if this identifier refers to a const variable |
| 297 | + declListNode, isConst := constDeclarations[identName] |
| 298 | + if !isConst { |
| 299 | + return |
| 300 | + } |
| 301 | + |
| 302 | + // Check if this is a write reference (assignment, increment, etc.) |
| 303 | + if !isWriteReference(node) { |
| 304 | + return |
| 305 | + } |
| 306 | + |
| 307 | + // Find the specific variable declaration for this identifier |
| 308 | + varDecl := findVariableDeclaration(node, declListNode) |
| 309 | + if varDecl == nil { |
| 310 | + return |
| 311 | + } |
| 312 | + |
| 313 | + // Don't report if this is part of the initializer |
| 314 | + if isInInitializer(node, varDecl) { |
| 315 | + return |
| 316 | + } |
| 317 | + |
| 318 | + // Report the violation |
| 319 | + ctx.ReportNode(node, buildConstMessage(identName)) |
| 320 | + }, |
| 321 | + } |
| 322 | + }, |
| 323 | +}) |
| 324 | + |
| 325 | +// collectIdentifiers recursively collects all identifiers from a binding pattern |
| 326 | +func collectIdentifiers(bindingName *ast.Node, declListNode *ast.Node, constDeclarations map[string]*ast.Node) { |
| 327 | + if bindingName == nil { |
| 328 | + return |
| 329 | + } |
| 330 | + |
| 331 | + switch bindingName.Kind { |
| 332 | + case ast.KindIdentifier: |
| 333 | + name := getIdentifierName(bindingName) |
| 334 | + if name != "" { |
| 335 | + constDeclarations[name] = declListNode |
| 336 | + } |
| 337 | + |
| 338 | + case ast.KindObjectBindingPattern: |
| 339 | + objBinding := bindingName.AsObjectBindingPattern() |
| 340 | + if objBinding == nil || objBinding.Elements == nil { |
| 341 | + return |
| 342 | + } |
| 343 | + |
| 344 | + for _, elem := range objBinding.Elements.Slice() { |
| 345 | + if elem.Kind == ast.KindBindingElement { |
| 346 | + bindingElem := elem.AsBindingElement() |
| 347 | + if bindingElem != nil && bindingElem.Name != nil { |
| 348 | + collectIdentifiers(bindingElem.Name, declListNode, constDeclarations) |
| 349 | + } |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + case ast.KindArrayBindingPattern: |
| 354 | + arrBinding := bindingName.AsArrayBindingPattern() |
| 355 | + if arrBinding == nil || arrBinding.Elements == nil { |
| 356 | + return |
| 357 | + } |
| 358 | + |
| 359 | + for _, elem := range arrBinding.Elements.Slice() { |
| 360 | + if elem.Kind == ast.KindBindingElement { |
| 361 | + bindingElem := elem.AsBindingElement() |
| 362 | + if bindingElem != nil && bindingElem.Name != nil { |
| 363 | + collectIdentifiers(bindingElem.Name, declListNode, constDeclarations) |
| 364 | + } |
| 365 | + } |
| 366 | + } |
| 367 | + } |
| 368 | +} |
0 commit comments