* Check for variables that are defined but never used.
(dag: BlockDependencyDag, blockMap: Map<string, BlockInfo>)
| 390 | * Check for variables that are defined but never used. |
| 391 | */ |
| 392 | function checkUnusedVariables(dag: BlockDependencyDag, blockMap: Map<string, BlockInfo>): LintIssue[] { |
| 393 | const issues: LintIssue[] = [] |
| 394 | |
| 395 | // Collect all used variables |
| 396 | const usedVars = new Set<string>() |
| 397 | for (const node of dag.nodes) { |
| 398 | for (const v of node.inputVariables) { |
| 399 | usedVars.add(v) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Check each block's output variables |
| 404 | for (const node of dag.nodes) { |
| 405 | const info = blockMap.get(node.id) |
| 406 | if (!info) continue |
| 407 | |
| 408 | for (const outputVar of node.outputVariables) { |
| 409 | // Skip private variables (starting with _) |
| 410 | if (outputVar.startsWith('_')) continue |
| 411 | |
| 412 | if (!usedVars.has(outputVar)) { |
| 413 | issues.push({ |
| 414 | severity: 'warning', |
| 415 | code: 'unused-variable', |
| 416 | message: `Variable "${outputVar}" is defined but never used`, |
| 417 | blockId: node.id, |
| 418 | blockLabel: info.label, |
| 419 | notebookName: info.notebookName, |
| 420 | details: { variable: outputVar }, |
| 421 | }) |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return issues |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Check for variables that are redefined (shadowed). |