📄 generateUnionType¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 1 |
| 📦 Imports | 7 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/rule-schema-to-typescript-types/src/generateUnionType.ts
📦 Imports¶
| Name | Source |
|---|---|
JSONSchema4 |
@typescript-eslint/utils/json-schema |
JSONSchema4Type |
@typescript-eslint/utils/json-schema |
SchemaAST |
./types.js |
RefMap |
./types.js |
UnionAST |
./types.js |
NotSupportedError |
./errors.js |
generateType |
./generateType.js |
Functions¶
generateUnionType(members: (JSONSchema4 | JSONSchema4Type)[], refMap: RefMap): UnionAST¶
Parameters:
members(JSONSchema4 | JSONSchema4Type)[]refMapRefMap
Returns: UnionAST
Calls:
elements.pushcomplex_call_475memberSchema.replaceAllArray.isArraygenerateType (from ./generateType.js)
Code
export function generateUnionType(
members: (JSONSchema4 | JSONSchema4Type)[],
refMap: RefMap,
): UnionAST {
const elements: SchemaAST[] = [];
for (const memberSchema of members) {
elements.push(
((): SchemaAST => {
switch (typeof memberSchema) {
case 'string':
return {
code: `'${memberSchema.replaceAll("'", "\\'")}'`,
commentLines: [],
type: 'literal',
};
case 'number':
case 'boolean':
return {
code: `${memberSchema}`,
commentLines: [],
type: 'literal',
};
case 'object':
if (memberSchema == null) {
throw new NotSupportedError('null in an enum', memberSchema);
}
if (Array.isArray(memberSchema)) {
throw new NotSupportedError('array in an enum', memberSchema);
}
return generateType(memberSchema, refMap);
}
})(),
);
}
return {
commentLines: [],
elements,
type: 'union',
};
}
Generated by Syntax Scribe