* Check for variables that are redefined (shadowed).
(dag: BlockDependencyDag, blockMap: Map<string, BlockInfo>)
| 430 | * Check for variables that are redefined (shadowed). |
| 431 | */ |
| 432 | function checkShadowedVariables(dag: BlockDependencyDag, blockMap: Map<string, BlockInfo>): LintIssue[] { |
| 433 | const issues: LintIssue[] = [] |
| 434 | |
| 435 | // Track which variables have been defined and by which block |
| 436 | const varDefinitions = new Map<string, { blockId: string; order: number }>() |
| 437 | |
| 438 | // Sort nodes by order |
| 439 | const sortedNodes = [...dag.nodes].sort((a, b) => a.order - b.order) |
| 440 | |
| 441 | for (const node of sortedNodes) { |
| 442 | const info = blockMap.get(node.id) |
| 443 | if (!info) continue |
| 444 | |
| 445 | for (const outputVar of node.outputVariables) { |
| 446 | const existing = varDefinitions.get(outputVar) |
| 447 | if (existing && existing.blockId !== node.id) { |
| 448 | issues.push({ |
| 449 | severity: 'warning', |
| 450 | code: 'shadowed-variable', |
| 451 | message: `Variable "${outputVar}" shadows a previous definition`, |
| 452 | blockId: node.id, |
| 453 | blockLabel: info.label, |
| 454 | notebookName: info.notebookName, |
| 455 | details: { |
| 456 | variable: outputVar, |
| 457 | previousBlock: blockMap.get(existing.blockId)?.label ?? existing.blockId, |
| 458 | }, |
| 459 | }) |
| 460 | } |
| 461 | varDefinitions.set(outputVar, { blockId: node.id, order: node.order }) |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return issues |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Check for blocks that failed to parse. |