-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Upgrade dependencies and refactor chunk creation #25
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,121 @@ | ||
| import { NodeGraph } from '@daxserver/validation-schema-codegen/traverse/node-graph' | ||
| import type { TraversedNode } from '@daxserver/validation-schema-codegen/traverse/types' | ||
| import { resolverStore } from '@daxserver/validation-schema-codegen/utils/resolver-store' | ||
| import { Node, SourceFile } from 'ts-morph' | ||
|
|
||
| const CHUNK_SIZE = 20 | ||
|
|
||
| export const shouldChunkUnion = (node: Node): boolean => { | ||
| if (Node.isUnionTypeNode(node)) { | ||
| return node.getTypeNodes().length >= CHUNK_SIZE | ||
| } | ||
|
|
||
| // Handle IndexedAccessType that resolves to large unions (e.g., typeof sites[number]) | ||
| if (Node.isIndexedAccessTypeNode(node)) { | ||
| const typeChecker = node.getProject().getTypeChecker() | ||
| const type = typeChecker.getTypeAtLocation(node) | ||
| if (type.isUnion()) { | ||
| return type.getUnionTypes().length >= CHUNK_SIZE | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| export const createChunkNodes = ( | ||
| node: Node, | ||
| parentTypeName: string, | ||
| nodeGraph: NodeGraph, | ||
| maincodeNodeIds: Set<string>, | ||
| requiredNodeIds: Set<string>, | ||
| sourceFile: SourceFile, | ||
| addToRequired: boolean = true, | ||
| ): string[] => { | ||
| let typeTexts: string[] = [] | ||
|
|
||
| if (Node.isUnionTypeNode(node)) { | ||
| typeTexts = node.getTypeNodes().map((n) => n.getText()) | ||
| } else if (Node.isIndexedAccessTypeNode(node)) { | ||
| // Handle IndexedAccessType by resolving to union types | ||
| const typeChecker = node.getProject().getTypeChecker() | ||
| const type = typeChecker.getTypeAtLocation(node) | ||
| if (type.isUnion()) { | ||
| typeTexts = type.getUnionTypes().map((t) => t.getText()) | ||
| } | ||
| } | ||
|
|
||
| if (typeTexts.length === 0) { | ||
| return [] | ||
| } | ||
| const chunks: string[][] = [] | ||
|
|
||
| // Create chunks of 20 items each | ||
| for (let i = 0; i < typeTexts.length; i += CHUNK_SIZE) { | ||
| chunks.push(typeTexts.slice(i, i + CHUNK_SIZE)) | ||
| } | ||
|
|
||
| const chunkReferences: string[] = [] | ||
|
|
||
| // scratch file for chunk nodes (once per parent) | ||
| const scratchSourceFile = node.getProject().createSourceFile( | ||
| `__chunks_${parentTypeName}_${Date.now()}.ts`, | ||
| '', | ||
| { overwrite: true }, | ||
| ) | ||
|
|
||
| // Create chunk nodes | ||
| for (let i = 0; i < chunks.length; i++) { | ||
| const chunkName = `${parentTypeName}_Chunk${i + 1}` | ||
| const chunkQualifiedName = resolverStore.generateQualifiedName(chunkName, sourceFile) | ||
|
|
||
| chunkReferences.push(chunkQualifiedName) | ||
| maincodeNodeIds.add(chunkQualifiedName) | ||
| if (addToRequired) { | ||
| requiredNodeIds.add(chunkQualifiedName) | ||
| } | ||
|
|
||
| // Create a new union node with only the chunk's type texts | ||
| const chunkTypeTexts = chunks[i]! | ||
|
|
||
| // Create a synthetic union node for this chunk | ||
| const chunkAlias = scratchSourceFile.addTypeAlias({ | ||
| name: chunkName, | ||
| type: chunkTypeTexts.join(' | '), | ||
| }) | ||
| const chunkTypeNode = chunkAlias.getTypeNode()! | ||
|
|
||
| const chunkTraversedNode: TraversedNode = { | ||
| node: chunkTypeNode, // Use the chunk-specific union node | ||
| type: 'chunk', | ||
| originalName: chunkName, | ||
| qualifiedName: chunkQualifiedName, | ||
| isImported: false, | ||
| isMainCode: true, | ||
| isChunk: true, | ||
| chunkReferences: [], // Chunk nodes don't need references to other chunks | ||
| } | ||
|
|
||
| nodeGraph.addTypeNode(chunkQualifiedName, chunkTraversedNode) | ||
|
|
||
| // Add to ResolverStore | ||
| resolverStore.addTypeMapping({ | ||
| originalName: chunkName, | ||
| sourceFile, | ||
| }) | ||
| } | ||
|
|
||
| return chunkReferences | ||
| } | ||
|
|
||
| export const markChunksAsRequired = ( | ||
| parentQualifiedName: string, | ||
| nodeGraph: NodeGraph, | ||
| requiredNodeIds: Set<string>, | ||
| ): void => { | ||
| const parentNode = nodeGraph.getNode(parentQualifiedName) | ||
| if (parentNode?.chunkReferences) { | ||
| for (const chunkRef of parentNode.chunkReferences) { | ||
| requiredNodeIds.add(chunkRef) | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getNode() will throw when parent hasn’t been added — add a guard.
nodeGraph.getNode throws for unknown nodes; current optional chaining won’t help.
Apply:
export const markChunksAsRequired = ( parentQualifiedName: string, nodeGraph: NodeGraph, requiredNodeIds: Set<string>, ): void => { - const parentNode = nodeGraph.getNode(parentQualifiedName) - if (parentNode?.chunkReferences) { - for (const chunkRef of parentNode.chunkReferences) { - requiredNodeIds.add(chunkRef) - } - } + if (!nodeGraph.hasNode(parentQualifiedName)) return + const parentNode = nodeGraph.getNode(parentQualifiedName) + const refs = parentNode.chunkReferences + if (!refs?.length) return + for (const chunkRef of refs) { + requiredNodeIds.add(chunkRef) + } }📝 Committable suggestion
🤖 Prompt for AI Agents