* Remove locally-emitted Enum class definitions whose name already comes from * a `.session_events` import. * * Quicktype's enum-merging path collapses structurally-identical enums (even * with `combineClasses: false`, which only governs class merging). When the * RPC schema gains sibling enums
(
code: string,
importedFromSessionEvents: Set<string>,
sessionEventsSchema: JSONSchema7 | undefined
)
| 1195 | * strip the local class so references resolve to the import. |
| 1196 | */ |
| 1197 | function removeShadowedSessionEventEnumsForPython( |
| 1198 | code: string, |
| 1199 | importedFromSessionEvents: Set<string>, |
| 1200 | sessionEventsSchema: JSONSchema7 | undefined |
| 1201 | ): string { |
| 1202 | if (importedFromSessionEvents.size === 0 || !sessionEventsSchema) return code; |
| 1203 | const seDefs = collectDefinitionCollections(sessionEventsSchema as Record<string, unknown>); |
| 1204 | const enumBlockRe = |
| 1205 | /(?:^|\n)class\s+(\w+)\s*\(Enum\):\s*\r?\n([\s\S]*?)(?=\nclass\s+\w|\n@dataclass\b|\ndef\s+\w|$)/g; |
| 1206 | return code |
| 1207 | .replace(enumBlockRe, (match: string, className: string, body: string) => { |
| 1208 | if (!importedFromSessionEvents.has(className)) return match; |
| 1209 | const seDef = seDefs.definitions[className] ?? seDefs.$defs[className]; |
| 1210 | const seResolved = seDef ? resolveSchema(seDef, seDefs) ?? seDef : undefined; |
| 1211 | if ( |
| 1212 | !seResolved?.enum || |
| 1213 | !Array.isArray(seResolved.enum) || |
| 1214 | !seResolved.enum.every((value) => typeof value === "string") |
| 1215 | ) { |
| 1216 | return match; |
| 1217 | } |
| 1218 | const localValues = new Set<string>(); |
| 1219 | const valueRe = /^\s+\w+\s*=\s*"([^"]*)"/gm; |
| 1220 | let vm: RegExpExecArray | null; |
| 1221 | while ((vm = valueRe.exec(body)) !== null) { |
| 1222 | localValues.add(vm[1]); |
| 1223 | } |
| 1224 | const seValues = new Set(seResolved.enum as string[]); |
| 1225 | if (localValues.size !== seValues.size) return match; |
| 1226 | for (const value of localValues) { |
| 1227 | if (!seValues.has(value)) return match; |
| 1228 | } |
| 1229 | return ""; |
| 1230 | }) |
| 1231 | .replace(/\n{3,}/g, "\n\n"); |
| 1232 | } |
| 1233 | |
| 1234 | function reorderPythonDataclassFields(code: string): string { |
| 1235 | const fieldRe = |
no test coverage detected
searching dependent graphs…