(ast: AST.AST)
| 218 | * @internal |
| 219 | */ |
| 220 | export const extractUnionTypes = (ast: AST.AST): ReadonlyArray<AST.AST> => { |
| 221 | if (!AST.isUnion(ast)) { |
| 222 | return [ast] |
| 223 | } |
| 224 | const types = ast.types.flatMap(extractUnionTypes) |
| 225 | if (!types.some(needsOwnResponse)) { |
| 226 | return Reflect.ownKeys(ast.annotations).length > 0 ? [ast] : types |
| 227 | } |
| 228 | const annotations = extractAnnotations(ast.annotations) |
| 229 | const keys = Reflect.ownKeys(annotations) |
| 230 | if (keys.length === 0) { |
| 231 | return types |
| 232 | } |
| 233 | return types.map((type) => { |
| 234 | const merged = mergedAnnotations(type) |
| 235 | return keys.every((key) => key in merged) ? type : AST.annotations(type, { |
| 236 | ...annotations, |
| 237 | ...extractAnnotations(merged), |
| 238 | ...type.annotations |
| 239 | }) |
| 240 | }) |
| 241 | } |
| 242 | |
| 243 | const needsOwnResponse = (ast: AST.AST): boolean => |
| 244 | isVoid(ast) || Reflect.ownKeys(extractAnnotations(mergedAnnotations(ast))).length > 0 |
no test coverage detected