(result: ExtractionResult, fold: ScriptFold, sink: ScriptSink)
| 471 | const INSTANCE_REF_KINDS: ReadonlySet<string> = new Set(['calls', 'references', 'instantiates', 'function_ref']); |
| 472 | |
| 473 | export function foldScriptResult(result: ExtractionResult, fold: ScriptFold, sink: ScriptSink): void { |
| 474 | const blockFile = `file:${fold.filePath}`; |
| 475 | // What already has a parent inside the block — a method's class, a nested |
| 476 | // function's function. The block's file holding something is not a parent: |
| 477 | // the component is, now. |
| 478 | const parented = new Set<string>(); |
| 479 | for (const edge of result.edges) { |
| 480 | if (edge.kind === 'contains' && edge.source !== blockFile) parented.add(edge.target); |
| 481 | } |
| 482 | |
| 483 | // A per-instance block's top-level `const data = useFetch(…)` is component |
| 484 | // state: its initializer runs on every instance, as the component's doing. |
| 485 | const instanceValues = new Set<string>(); |
| 486 | if (fold.perInstance) { |
| 487 | for (const node of result.nodes) { |
| 488 | if ((node.kind === 'constant' || node.kind === 'variable') && !parented.has(node.id)) instanceValues.add(node.id); |
| 489 | } |
| 490 | } |
| 491 | const runsAsComponent = (id: string) => id === blockFile || instanceValues.has(id); |
| 492 | |
| 493 | for (const node of result.nodes) { |
| 494 | if (node.kind === 'file') continue; |
| 495 | node.startLine += fold.lineOffset; |
| 496 | node.endLine += fold.lineOffset; |
| 497 | node.language = fold.language; |
| 498 | sink.nodes.push(node); |
| 499 | if (!parented.has(node.id)) sink.edges.push({ source: fold.componentNodeId, target: node.id, kind: 'contains' }); |
| 500 | } |
| 501 | |
| 502 | for (const edge of result.edges) { |
| 503 | if (edge.kind === 'contains' && edge.source === blockFile) continue; |
| 504 | if (edge.line) edge.line += fold.lineOffset; |
| 505 | // What a top-level value DOES is the component's; what it HOLDS stays its |
| 506 | // own — `const api = { load() {…} }` keeps `api::load` (#2300). |
| 507 | if (fold.perInstance && runsAsComponent(edge.source) && edge.kind !== 'imports' && edge.kind !== 'contains') { |
| 508 | edge.source = fold.componentNodeId; |
| 509 | } |
| 510 | sink.edges.push(edge); |
| 511 | } |
| 512 | |
| 513 | for (const ref of result.unresolvedReferences) { |
| 514 | ref.line += fold.lineOffset; |
| 515 | ref.filePath = fold.filePath; |
| 516 | ref.language = fold.language; |
| 517 | if (fold.perInstance && runsAsComponent(ref.fromNodeId) && INSTANCE_REF_KINDS.has(ref.referenceKind)) { |
| 518 | ref.fromNodeId = fold.componentNodeId; |
| 519 | } |
| 520 | sink.unresolvedReferences.push(ref); |
| 521 | } |
| 522 | |
| 523 | for (const error of result.errors) { |
| 524 | if (error.line) error.line += fold.lineOffset; |
| 525 | sink.errors.push(error); |
| 526 | } |
| 527 | } |
no test coverage detected