📄 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:
schemaany: Original rule schema(s) as declared inmeta.schema.
Returns: undefined
Stringified TypeScript type(s) equivalent to the options schema(s).
Raw JSDoc
Calls:
TSUtils.isArray['/** No options declared */', 'type Options = [];'].joincompileSchemarefTypes.pushtypes.pushprintTypeAlias (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:
schemaJSONSchema4indexnumber
Returns: { refTypes: string[]; type: SchemaAST }
Calls:
Object.entriestoPascalCaserefMap.setgenerateType (from ./generateType.js)optimizeAST (from ./optimizeAST.js)refTypes.pushprintTypeAlias (from ./printAST.js)
Internal Comments:
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:
keystring
Returns: string
Calls:
key[0].toUpperCasekey.substring
Generated by Syntax Scribe