|
| 1 | +# Maincode Filtering |
| 2 | + |
| 3 | +The code generator automatically filters the output to include only maincode nodes and their connected dependencies. This feature reduces the generated output by excluding imported types that are not actually used by the main code. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The code generator includes only the following nodes in the output: |
| 8 | + |
| 9 | +1. **Maincode nodes**: Types defined in the main source file (not imported) |
| 10 | +2. **Connected dependencies**: Types that are directly or indirectly referenced by maincode nodes |
| 11 | + |
| 12 | +Imported types that are not referenced anywhere in the dependency chain are automatically filtered out. |
| 13 | + |
| 14 | +## Example |
| 15 | + |
| 16 | +Consider the following file structure: |
| 17 | + |
| 18 | +**external.ts** |
| 19 | + |
| 20 | +```typescript |
| 21 | +export type UsedType = { |
| 22 | + value: string |
| 23 | +} |
| 24 | + |
| 25 | +export type UnusedType = { |
| 26 | + unused: boolean |
| 27 | +} |
| 28 | +``` |
| 29 | +
|
| 30 | +**main.ts** |
| 31 | +
|
| 32 | +```typescript |
| 33 | +import { UsedType, UnusedType } from './external' |
| 34 | + |
| 35 | +export interface MainInterface { |
| 36 | + data: UsedType // UsedType is referenced and will be included |
| 37 | +} |
| 38 | + |
| 39 | +export type SimpleType = { |
| 40 | + id: string |
| 41 | +} |
| 42 | + |
| 43 | +// UnusedType is imported but not referenced, so it will be filtered out |
| 44 | +``` |
| 45 | + |
| 46 | +### Generated Output |
| 47 | + |
| 48 | +```typescript |
| 49 | +const result = generateCode({ |
| 50 | + filePath: './main.ts', |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +**Output automatically excludes unconnected imports:** |
| 55 | + |
| 56 | +```typescript |
| 57 | +export const UsedType = Type.Object({ |
| 58 | + value: Type.String(), |
| 59 | +}) |
| 60 | + |
| 61 | +export const MainInterface = Type.Object({ |
| 62 | + data: UsedType, |
| 63 | +}) |
| 64 | + |
| 65 | +export const SimpleType = Type.Object({ |
| 66 | + id: Type.String(), |
| 67 | +}) |
| 68 | + |
| 69 | +// UnusedType is not included because it's not connected to any maincode node |
| 70 | +``` |
| 71 | + |
| 72 | +## Dependency Chain Handling |
| 73 | + |
| 74 | +The filtering algorithm correctly handles deep dependency chains. If a maincode node references a type that has its own dependencies, all dependencies in the chain are included: |
| 75 | + |
| 76 | +**level1.ts** |
| 77 | + |
| 78 | +```typescript |
| 79 | +export type Level1 = { |
| 80 | + value: string |
| 81 | +} |
| 82 | +``` |
| 83 | +
|
| 84 | +**level2.ts** |
| 85 | +
|
| 86 | +```typescript |
| 87 | +import { Level1 } from './level1' |
| 88 | +export type Level2 = { |
| 89 | + level1: Level1 |
| 90 | +} |
| 91 | +``` |
| 92 | +
|
| 93 | +**main.ts** |
| 94 | +
|
| 95 | +```typescript |
| 96 | +import { Level2 } from './level2' |
| 97 | + |
| 98 | +export interface MainType { |
| 99 | + data: Level2 |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +The output automatically includes `Level1`, `Level2`, and `MainType` because they form a complete dependency chain starting from the maincode node `MainType`. |
| 104 | + |
| 105 | +## Implementation Details |
| 106 | + |
| 107 | +The filtering algorithm: |
| 108 | + |
| 109 | +1. Identifies maincode nodes (nodes with `isImported: false`) and marks them as required |
| 110 | +2. Analyzes type references to identify which imported types are actually used |
| 111 | +3. Marks referenced types and their transitive dependencies as required |
| 112 | +4. Filters the final output to include only required nodes |
| 113 | +5. Returns the filtered nodes in topological order to maintain proper dependency ordering |
| 114 | + |
| 115 | +This approach ensures that only types that are part of a connected dependency graph starting from maincode nodes are included in the output. |
0 commit comments