jsii
Version:
[](https://cdk.dev) [](https://github.com/aws/jsii
159 lines • 6.74 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAllowedCovariantSubtype = isAllowedCovariantSubtype;
const spec = __importStar(require("@jsii/spec"));
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
/**
* Check if subType is an allowed covariant subtype to superType
*
* This is not a generic check for subtypes or covariance, but a specific implementation
* that checks the currently allowed conditions for class covariance.
* In practice, this is driven by C# limitations.
*/
function isAllowedCovariantSubtype(subType, superType, dereference) {
// one void, while other isn't => not covariant
if ((subType === undefined) !== (superType === undefined)) {
return false;
}
// Same type is always covariant
if ((0, fast_deep_equal_1.default)(subType, superType)) {
return true;
}
// Handle array collections (covariant)
if (spec.isCollectionTypeReference(subType) && spec.isCollectionTypeReference(superType)) {
if (subType.collection.kind === 'array' && superType.collection.kind === 'array') {
return isAllowedCovariantSubtype(subType.collection.elementtype, superType.collection.elementtype, dereference);
}
// Maps are not allowed to be covariant in C#, so we exclude them here.
// This seems to be because we use C# Dictionary to implements Maps, which are using generics and generics are not allowed to be covariant
return false;
}
// Union types are currently not allowed, because we have not seen the need for it.
// Technically narrowing (removing `| Type` or subtyping) could be allowed and this works in C#.
if (spec.isUnionTypeReference(subType) || spec.isUnionTypeReference(superType)) {
return false;
}
// Intersection types are invalid, because intersections are only allowed as inputs
// and covariance is only allowed in outputs.
if (spec.isIntersectionTypeReference(subType) || spec.isIntersectionTypeReference(superType)) {
return false;
}
// Primitives can never be covariant to each other in C#
if (spec.isPrimitiveTypeReference(subType) || spec.isPrimitiveTypeReference(superType)) {
return false;
}
// We really only support covariance for named types (and lists of named types).
// To be safe, let's guard against any unknown cases.
if (!spec.isNamedTypeReference(subType) || !spec.isNamedTypeReference(superType)) {
return false;
}
const subTypeSpec = dereference(subType.fqn);
const superTypeSpec = dereference(superType.fqn);
if (!subTypeSpec || !superTypeSpec) {
return false;
}
// Handle class-to-class inheritance
if (spec.isClassType(subTypeSpec) && spec.isClassType(superTypeSpec)) {
return _classExtendsClass(subTypeSpec, superType.fqn);
}
// Handle interface-to-interface inheritance
if (spec.isInterfaceType(subTypeSpec) && spec.isInterfaceType(superTypeSpec)) {
return _interfaceExtendsInterface(subTypeSpec, superType.fqn);
}
// Handle class implementing interface
if (spec.isClassType(subTypeSpec) && spec.isInterfaceType(superTypeSpec)) {
return _classImplementsInterface(subTypeSpec, superType.fqn);
}
return false;
function _classExtendsClass(classType, targetFqn) {
let current = classType;
while (current.base) {
if (current.base === targetFqn) {
return true;
}
const baseType = dereference(current.base);
if (!spec.isClassType(baseType)) {
break;
}
current = baseType;
}
return false;
}
function _classImplementsInterface(classType, interfaceFqn) {
// Check direct interfaces
if (classType.interfaces?.includes(interfaceFqn)) {
return true;
}
// Check inherited interfaces
if (classType.interfaces) {
for (const iface of classType.interfaces) {
const ifaceType = dereference(iface);
if (spec.isInterfaceType(ifaceType) && _interfaceExtendsInterface(ifaceType, interfaceFqn)) {
return true;
}
}
}
// Check base class interfaces
if (classType.base) {
const baseType = dereference(classType.base);
if (spec.isClassType(baseType)) {
return _classImplementsInterface(baseType, interfaceFqn);
}
}
return false;
}
function _interfaceExtendsInterface(interfaceType, targetFqn) {
if (interfaceType.fqn === targetFqn) {
return true;
}
if (interfaceType.interfaces) {
for (const iface of interfaceType.interfaces) {
if (iface === targetFqn) {
return true;
}
const ifaceType = dereference(iface);
if (spec.isInterfaceType(ifaceType) && _interfaceExtendsInterface(ifaceType, targetFqn)) {
return true;
}
}
}
return false;
}
}
//# sourceMappingURL=type-analysis.js.map