Skip to content

⬅️ Back to Table of Contents

📄 generateUnionType.ts

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 7
📊 Variables & Constants 1

📚 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
AST ./types
RefMap ./types
UnionAST ./types
NotSupportedError ./errors
generateType ./generateType

Variables & Constants

Name Type Kind Value Exported
elements AST[] const []

Functions

generateUnionType(members: (JSONSchema4 | JSONSchema4Type)[], refMap: RefMap): UnionAST

Code
export function generateUnionType(
  members: (JSONSchema4 | JSONSchema4Type)[],
  refMap: RefMap,
): UnionAST {
  const elements: AST[] = [];

  for (const memberSchema of members) {
    elements.push(
      ((): AST => {
        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',
  };
}
  • Parameters:
  • members: (JSONSchema4 | JSONSchema4Type)[]
  • refMap: RefMap
  • Return Type: UnionAST
  • Calls:
  • elements.push
  • complex_call_454
  • memberSchema.replaceAll
  • Array.isArray
  • generateType (from ./generateType)