UNPKG

jsii

Version:

[![Join the chat at https://cdk.Dev](https://img.shields.io/static/v1?label=Slack&message=cdk.dev&color=brightgreen&logo=slack)](https://cdk.dev) [![All Contributors](https://img.shields.io/github/all-contributors/aws/jsii/main?label=%E2%9C%A8%20All%20Con

75 lines 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.typeReferenceToString = typeReferenceToString; exports.typeReferenceEqual = typeReferenceEqual; exports.createTypeResolver = createTypeResolver; exports.resolveType = resolveType; const spec = require("@jsii/spec"); const type_visitor_1 = require("./type-visitor"); /** * Convert a type reference to a string */ function typeReferenceToString(x) { return (0, type_visitor_1.visitTypeReference)(x, { named: function (ref) { return ref.fqn; }, primitive: function (ref) { return ref.primitive; }, collection: function (ref) { return `${ref.collection.kind}<${typeReferenceToString(ref.collection.elementtype)}>`; }, union: function (ref) { return ref.union.types.map(typeReferenceToString).join(' | '); }, intersection: function (ref) { return ref.intersection.types.map(typeReferenceToString).join(' & '); }, }); } /** * Return whether the given type references are equal */ function typeReferenceEqual(a, b) { if (spec.isNamedTypeReference(a) && spec.isNamedTypeReference(b)) { return a.fqn === b.fqn; } if (spec.isPrimitiveTypeReference(a) && spec.isPrimitiveTypeReference(b)) { return a.primitive === b.primitive; } if (spec.isCollectionTypeReference(a) && spec.isCollectionTypeReference(b)) { return (a.collection.kind === b.collection.kind && typeReferenceEqual(a.collection.elementtype, b.collection.elementtype)); } if (spec.isUnionTypeReference(a) && spec.isUnionTypeReference(b)) { return (a.union.types.length === b.union.types.length && a.union.types.every((aType, i) => typeReferenceEqual(aType, b.union.types[i]))); } if (spec.isIntersectionTypeReference(a) && spec.isIntersectionTypeReference(b)) { return (a.intersection.types.length === b.intersection.types.length && a.intersection.types.every((aType, i) => typeReferenceEqual(aType, b.intersection.types[i]))); } return false; } /** * Creates a type resolver function for a given context (assembly + dependency closure). */ function createTypeResolver(assembly, dependencyClosure) { return (typeRef) => resolveType(typeRef, assembly, dependencyClosure); } /** * Resolve a type from a name to the actual type. * Uses a given assembly and dependency closure for lookup. */ function resolveType(typeRef, assembly, dependencyClosure) { if (typeof typeRef !== 'string') { typeRef = typeRef.fqn; } const [assm] = typeRef.split('.'); if (assembly.name === assm) { return assembly.types?.[typeRef]; } const foreignAssm = dependencyClosure.find((dep) => dep.name === assm); return foreignAssm?.types?.[typeRef]; } //# sourceMappingURL=type-reference.js.map