-
Notifications
You must be signed in to change notification settings - Fork 67
fix(type-gen): improve ownership #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+176
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { ReflectionKind } from 'typedoc'; | ||
|
|
||
| const HOST_KINDS = ReflectionKind.Class | ReflectionKind.Namespace; | ||
|
|
||
| const collectReferences = (type, out, depth = 0) => { | ||
| if (!type || depth > 30) return; | ||
|
|
||
| const next = []; | ||
|
|
||
| switch (type.type) { | ||
| case 'reference': | ||
| if (type.reflection) out.add(type.reflection.id); | ||
| next.push(...(type.typeArguments ?? [])); | ||
| break; | ||
| case 'union': | ||
| case 'intersection': | ||
| next.push(...(type.types ?? [])); | ||
| break; | ||
| case 'array': | ||
| case 'optional': | ||
| case 'rest': | ||
| next.push(type.elementType); | ||
| break; | ||
| case 'tuple': | ||
| next.push(...(type.elements ?? [])); | ||
| break; | ||
| case 'namedTupleMember': | ||
| next.push(type.element); | ||
| break; | ||
| case 'indexedAccess': | ||
| next.push(type.objectType, type.indexType); | ||
| break; | ||
| case 'conditional': | ||
| next.push( | ||
| type.checkType, | ||
| type.extendsType, | ||
| type.trueType, | ||
| type.falseType | ||
| ); | ||
| break; | ||
| case 'predicate': | ||
| next.push(type.targetType); | ||
| break; | ||
| case 'query': | ||
| next.push(type.queryType); | ||
| break; | ||
| case 'mapped': | ||
| next.push(type.parameterType, type.templateType, type.nameType); | ||
| break; | ||
| case 'templateLiteral': | ||
| next.push(...(type.tail ?? []).map(([tailType]) => tailType)); | ||
| break; | ||
| case 'typeOperator': | ||
| next.push(type.target); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| for (const child of next) collectReferences(child, out, depth + 1); | ||
| }; | ||
|
|
||
| // The page that hosts a referring reflection: the nearest class or namespace, | ||
| // a root structural type (resolved transitively), or the project itself. | ||
| // Project-level referrers still count as owners so types used by root exports | ||
| // never look single-owned, but the project is never a co-location target. | ||
| const hostFor = (reflection, structuralIds) => { | ||
| let current = reflection; | ||
|
|
||
| while (current) { | ||
| if (current.kindOf?.(HOST_KINDS)) return current; | ||
| if (structuralIds.has(current.id)) return current; | ||
| if (current.isProject?.()) return current; | ||
| current = current.parent; | ||
| } | ||
|
|
||
| return undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * Map each root structural type to the single class or namespace page that | ||
| * references it, when exactly one exists. | ||
| */ | ||
| export const soleOwnerPages = (project, structuralTypes) => { | ||
| const structuralIds = new Set(structuralTypes.map(type => type.id)); | ||
| const owners = new Map(); | ||
| const structuralReferrers = new Map(); | ||
|
|
||
| for (const type of structuralTypes) { | ||
| owners.set(type.id, new Set()); | ||
| structuralReferrers.set(type.id, new Set()); | ||
| } | ||
|
|
||
| for (const reflection of Object.values(project.reflections)) { | ||
| const references = new Set(); | ||
|
|
||
| collectReferences(reflection.type, references); | ||
| collectReferences(reflection.default, references); | ||
| for (const extended of reflection.extendedTypes ?? []) { | ||
| collectReferences(extended, references); | ||
| } | ||
| for (const implemented of reflection.implementedTypes ?? []) { | ||
| collectReferences(implemented, references); | ||
| } | ||
|
|
||
| if (!references.size) continue; | ||
|
|
||
| const host = hostFor(reflection, structuralIds); | ||
| if (!host) continue; | ||
|
|
||
| for (const id of references) { | ||
| if (id === host.id || !owners.has(id)) continue; | ||
|
|
||
| if (structuralIds.has(host.id)) { | ||
| structuralReferrers.get(id).add(host.id); | ||
| } else { | ||
| owners.get(id).add(host); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let changed = true; | ||
| while (changed) { | ||
| changed = false; | ||
|
|
||
| for (const [id, referrerIds] of structuralReferrers) { | ||
| const ownHosts = owners.get(id); | ||
|
|
||
| for (const referrerId of referrerIds) { | ||
| for (const host of owners.get(referrerId)) { | ||
| if (!ownHosts.has(host)) { | ||
| ownHosts.add(host); | ||
| changed = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const soleOwners = new Map(); | ||
|
|
||
| for (const type of structuralTypes) { | ||
| const [owner, ...rest] = owners.get(type.id); | ||
| if (owner && !rest.length && owner.kindOf?.(HOST_KINDS)) { | ||
| soleOwners.set(type, owner); | ||
| } | ||
| } | ||
|
|
||
| return soleOwners; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.