(result: ExtractionResult, fold: ScriptFold, sink: ScriptSink)
| 130 | const INSTANCE_REF_KINDS: ReadonlySet<string> = new Set(['calls', 'references', 'instantiates', 'function_ref']); |
| 131 | |
| 132 | export function foldScriptResult(result: ExtractionResult, fold: ScriptFold, sink: ScriptSink): void { |
| 133 | const blockFile = `file:${fold.filePath}`; |
| 134 | // What already has a parent inside the block — a method's class, a nested |
| 135 | // function's function. The block's file holding something is not a parent: |
| 136 | // the component is, now. |
| 137 | const parented = new Set<string>(); |
| 138 | for (const edge of result.edges) { |
| 139 | if (edge.kind === 'contains' && edge.source !== blockFile) parented.add(edge.target); |
| 140 | } |
| 141 | |
| 142 | // A per-instance block's top-level `const data = useFetch(…)` is component |
| 143 | // state: its initializer runs on every instance, as the component's doing. |
| 144 | const instanceValues = new Set<string>(); |
| 145 | if (fold.perInstance) { |
| 146 | for (const node of result.nodes) { |
| 147 | if ((node.kind === 'constant' || node.kind === 'variable') && !parented.has(node.id)) instanceValues.add(node.id); |
| 148 | } |
| 149 | } |
| 150 | const runsAsComponent = (id: string) => id === blockFile || instanceValues.has(id); |
| 151 | |
| 152 | for (const node of result.nodes) { |
| 153 | if (node.kind === 'file') continue; |
| 154 | node.startLine += fold.lineOffset; |
| 155 | node.endLine += fold.lineOffset; |
| 156 | node.language = fold.language; |
| 157 | sink.nodes.push(node); |
| 158 | if (!parented.has(node.id)) sink.edges.push({ source: fold.componentNodeId, target: node.id, kind: 'contains' }); |
| 159 | } |
| 160 | |
| 161 | for (const edge of result.edges) { |
| 162 | if (edge.kind === 'contains' && edge.source === blockFile) continue; |
| 163 | if (edge.line) edge.line += fold.lineOffset; |
| 164 | // What a top-level value DOES is the component's; what it HOLDS stays its |
| 165 | // own — `const api = { load() {…} }` keeps `api::load` (#2300). |
| 166 | if (fold.perInstance && runsAsComponent(edge.source) && edge.kind !== 'imports' && edge.kind !== 'contains') { |
| 167 | edge.source = fold.componentNodeId; |
| 168 | } |
| 169 | sink.edges.push(edge); |
| 170 | } |
| 171 | |
| 172 | for (const ref of result.unresolvedReferences) { |
| 173 | ref.line += fold.lineOffset; |
| 174 | ref.filePath = fold.filePath; |
| 175 | ref.language = fold.language; |
| 176 | if (fold.perInstance && runsAsComponent(ref.fromNodeId) && INSTANCE_REF_KINDS.has(ref.referenceKind)) { |
| 177 | ref.fromNodeId = fold.componentNodeId; |
| 178 | } |
| 179 | sink.unresolvedReferences.push(ref); |
| 180 | } |
| 181 | |
| 182 | for (const error of result.errors) { |
| 183 | if (error.line) error.line += fold.lineOffset; |
| 184 | sink.errors.push(error); |
| 185 | } |
| 186 | } |
no test coverage detected