Skip to content

⬅️ Back to Table of Contents

📄 rule-schema-to-typescript-types/src

📊 Analysis Summary

Metric Count
🔧 Functions 3
📦 Imports 6

📚 Table of Contents

🛠️ File Location:

📂 packages/rule-schema-to-typescript-types/src/index.ts

📦 Imports

Name Source
JSONSchema4 @typescript-eslint/utils/json-schema
TSUtils @typescript-eslint/utils
SchemaAST ./types.js
generateType ./generateType.js
optimizeAST ./optimizeAST.js
printTypeAlias ./printAST.js

Functions

schemaToTypes(schema: JSONSchema4 | readonly JSONSchema4[]): string

Converts rule options schema(s) to the equivalent TypeScript type string.

Parameters:

  • schema any: Original rule schema(s) as declared in meta.schema.

Returns: undefined Stringified TypeScript type(s) equivalent to the options schema(s).

Raw JSDoc
/**
 * Converts rule options schema(s) to the equivalent TypeScript type string.
 *
 * @param schema Original rule schema(s) as declared in `meta.schema`.
 * @returns Stringified TypeScript type(s) equivalent to the options schema(s).
 */

Calls:

  • TSUtils.isArray
  • ['/** No options declared */', 'type Options = [];'].join
  • compileSchema
  • refTypes.push
  • types.push
  • printTypeAlias (from ./printAST.js)
  • [...refTypes, optionsType].join
Code
export function schemaToTypes(
  schema: JSONSchema4 | readonly JSONSchema4[],
): string {
  const [isArraySchema, schemaNormalized] = TSUtils.isArray(schema)
    ? [true, schema]
    : [false, [schema]];

  if (schemaNormalized.length === 0) {
    return ['/** No options declared */', 'type Options = [];'].join('\n');
  }

  const refTypes: string[] = [];
  const types: SchemaAST[] = [];
  for (let i = 0; i < schemaNormalized.length; i += 1) {
    const result = compileSchema(schemaNormalized[i], i);
    refTypes.push(...result.refTypes);
    types.push(result.type);
  }

  const optionsType = isArraySchema
    ? printTypeAlias('Options', {
        commentLines: [],
        elements: types,
        spreadType: null,
        type: 'tuple',
      })
    : printTypeAlias('Options', types[0]);

  return [...refTypes, optionsType].join('\n\n');
}

compileSchema(schema: JSONSchema4, index: number): { refTypes: string[]; type: SchemaAST }

Parameters:

  • schema JSONSchema4
  • index number

Returns: { refTypes: string[]; type: SchemaAST }

Calls:

  • Object.entries
  • toPascalCase
  • refMap.set
  • generateType (from ./generateType.js)
  • optimizeAST (from ./optimizeAST.js)
  • refTypes.push
  • printTypeAlias (from ./printAST.js)

Internal Comments:

// we only support defs at the top level for simplicity (x2)

Code
function compileSchema(
  schema: JSONSchema4,
  index: number,
): { refTypes: string[]; type: SchemaAST } {
  const refTypes: string[] = [];

  const refMap = new Map<string, string>();
  // we only support defs at the top level for simplicity
  const defs = schema.$defs ?? schema.definitions;
  if (defs) {
    for (const [defKey, defSchema] of Object.entries(defs)) {
      const typeName = toPascalCase(defKey);
      refMap.set(`#/$defs/${defKey}`, typeName);
      refMap.set(`#/items/${index}/$defs/${defKey}`, typeName);

      const type = generateType(defSchema, refMap);
      optimizeAST(type);
      refTypes.push(printTypeAlias(typeName, type));
    }
  }

  const type = generateType(schema, refMap);
  optimizeAST(type);

  return {
    refTypes,
    type,
  };
}

toPascalCase(key: string): string

Parameters:

  • key string

Returns: string

Calls:

  • key[0].toUpperCase
  • key.substring
Code
function toPascalCase(key: string): string {
  return key[0].toUpperCase() + key.substring(1);
}

Generated by Syntax Scribe