* What the flows want done to the graph, for one language. Handed a sync, null * means it can't say from what changed, and a pass over the project has to.
( language: ParameterFlowLanguage, queries: ParameterFlowQueries, context: ResolutionContext, files: string[], sync?: FlowSync, )
| 600 | * means it can't say from what changed, and a pass over the project has to. |
| 601 | */ |
| 602 | async function planFlows( |
| 603 | language: ParameterFlowLanguage, |
| 604 | queries: ParameterFlowQueries, |
| 605 | context: ResolutionContext, |
| 606 | files: string[], |
| 607 | sync?: FlowSync, |
| 608 | ): Promise<ParameterFlowPlan | null> { |
| 609 | const maybeYield = createYielder(); |
| 610 | const methods = new MethodsRead(queries, language.language); |
| 611 | const adapter = language.adapter(context, methods); |
| 612 | const pass = new FlowPass(adapter, methods, queries, language.language); |
| 613 | const reviewed = sync ? await namesToReview(sync, adapter, pass, queries) : null; |
| 614 | if (sync && !reviewed) return null; |
| 615 | const methodsReached = (names: string[]): FlowMethod[] => |
| 616 | queries.getMethodsNamed(names, language.language).filter((method) => queries.hasCallInto(method.id)); |
| 617 | const candidates = reviewed ? methodsReached([...reviewed]) : queries.getReachedMethods(language.language); |
| 618 | methods.know(candidates); |
| 619 | await maybeYield(); |
| 620 | |
| 621 | // A site whose answer the flow doesn't change needs no more proof. The others |
| 622 | // rest on the graph holding every call of the methods the flow went through, |
| 623 | // which only the project's text can show: it is read for those names, and |
| 624 | // only when there are any. |
| 625 | const onward = reviewed ? (names: string[]): FlowMethod[] => { |
| 626 | const fresh = names.filter((name) => !reviewed.has(name)); |
| 627 | for (const name of fresh) reviewed.add(name); |
| 628 | return methodsReached(fresh); |
| 629 | } : undefined; |
| 630 | const answers = (await pass.run(candidates, maybeYield, onward)).map((flow) => ({ |
| 631 | flow, |
| 632 | targets: flow.sites.map((site) => adapter.methodOn(flow.value, site.method)), |
| 633 | })); |
| 634 | const flows: RecordedFlow[] = answers.map(({ flow, targets }) => ({ |
| 635 | key: flowKey(flow), |
| 636 | // A class with no such method may answer the call itself, which it says |
| 637 | // somewhere no name of the flow's shows. |
| 638 | names: flow.sites.some((site, i) => targets[i] === null && (site.edgeId !== undefined || site.viaFlow)) ? null |
| 639 | : [...new Set([...flow.through, ...flow.sites.map((site) => site.method)])], |
| 640 | })); |
| 641 | if (sync) { |
| 642 | const recorded = sync.record.flows.filter((flow) => reviewed!.has(flowKeyName(flow.key))); |
| 643 | if (recorded.map((flow) => flow.key).sort().join('\n') !== flows.map((flow) => flow.key).sort().join('\n')) return null; |
| 644 | flows.push(...sync.record.flows.filter((flow) => !reviewed!.has(flowKeyName(flow.key)))); |
| 645 | } |
| 646 | const drops = (flow: TypedFlow, site: FlowSite, target: FlowMethod | null): boolean => |
| 647 | target === null && site.edgeId !== undefined && !site.viaFlow && adapter.answersItself(flow.value, site.method); |
| 648 | const changes = (a: (typeof answers)[number]): boolean => a.flow.sites.some((site, i) => { |
| 649 | const target = a.targets[i]!; |
| 650 | return site.viaFlow || drops(a.flow, site, target) || (target !== null && target.id !== site.targetId); |
| 651 | }); |
| 652 | const rests = new Set(answers.filter(changes).flatMap((a) => [...a.flow.through])); |
| 653 | const written = sync ? sync.record.written : rests.size > 0 ? await adapter.written(rests, files) : new Map() as FlowWritten; |
| 654 | if ([...rests].some((name) => sync && !written.has(name))) return null; |
| 655 | pass.writtenElsewhere = new Set([...rests].filter((name) => { |
| 656 | let count = 0; |
| 657 | for (const times of written.get(name)?.values() ?? []) count += times; |
| 658 | return count > adapter.declared(name) + pass.sitesOf(name); |
| 659 | })); |
no test coverage detected