📄 generateObjectType¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 1 |
| 📦 Imports | 8 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/rule-schema-to-typescript-types/src/generateObjectType.ts
📦 Imports¶
| Name | Source |
|---|---|
JSONSchema4ObjectSchema |
@typescript-eslint/utils/json-schema |
requiresQuoting |
@typescript-eslint/type-utils |
TSUtils |
@typescript-eslint/utils |
SchemaAST |
./types.js |
ObjectAST |
./types.js |
RefMap |
./types.js |
generateType |
./generateType.js |
getCommentLines |
./getCommentLines.js |
Functions¶
generateObjectType(schema: JSONSchema4ObjectSchema, refMap: RefMap): ObjectAST¶
Parameters:
schemaJSONSchema4ObjectSchemarefMapRefMap
Returns: ObjectAST
Calls:
getCommentLines (from ./getCommentLines.js)generateType (from ./generateType.js)TSUtils.isArrayObject.entriesrequiresQuoting (from @typescript-eslint/type-utils)properties.pushrequired.has
Internal Comments:
Code
export function generateObjectType(
schema: JSONSchema4ObjectSchema,
refMap: RefMap,
): ObjectAST {
const commentLines = getCommentLines(schema);
let indexSignature: SchemaAST | null = null;
if (
schema.additionalProperties === true ||
// eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
schema.additionalProperties === undefined
) {
indexSignature = {
commentLines: [],
type: 'type-reference',
typeName: 'unknown',
};
} else if (typeof schema.additionalProperties === 'object') {
const indexSigType = generateType(schema.additionalProperties, refMap);
indexSignature = indexSigType;
}
const properties: ObjectAST['properties'] = [];
const required = new Set(
TSUtils.isArray(schema.required) ? schema.required : [],
);
if (schema.properties) {
const propertyDefs = Object.entries(schema.properties);
for (const [propName, propSchema] of propertyDefs) {
const propType = generateType(propSchema, refMap);
const sanitizedPropName = requiresQuoting(propName)
? `'${propName}'`
: propName;
properties.push({
name: sanitizedPropName,
optional: !required.has(propName),
type: propType,
});
}
}
return {
commentLines,
indexSignature,
properties,
type: 'object',
};
}
Generated by Syntax Scribe