dependency-injection-cat
Version:
DI Cat is a truly clean DI-container, which allows you not to pollute your business logic with decorators from DI/IOC libraries!
126 lines (125 loc) • 6.17 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerPropertyBean = void 0;
var ts = __importStar(require("typescript"));
var typescript_1 = require("typescript");
var lodash_1 = require("lodash");
var getPropertyBeanInfo_1 = require("../ts-helpers/bean-info/getPropertyBeanInfo");
var BeanRepository_1 = require("./BeanRepository");
var CompilationContext_1 = require("../../compilation-context/CompilationContext");
var node_source_descriptor_1 = require("../ts-helpers/node-source-descriptor");
var constants_1 = require("./constants");
var QualifiedType_1 = require("../ts-helpers/type-qualifier/QualifiedType");
var TypeQualifier_1 = require("../ts-helpers/type-qualifier/TypeQualifier");
var ExtendedSet_1 = require("../utils/ExtendedSet");
var registerPropertyBean = function (contextDescriptor, classElement) {
var classElementName = classElement.name.getText();
if (constants_1.restrictedClassMemberNames.has(classElementName)) {
CompilationContext_1.CompilationContext.reportError({
node: classElement,
message: "\"" + classElementName + "\" property is reserved for the di-container, please use another name instead",
filePath: contextDescriptor.absolutePath,
relatedContextPath: contextDescriptor.absolutePath,
});
return;
}
var qualifiedType = getBeanTypeInfoFromClassProperty(contextDescriptor, classElement);
var beanInfo = (0, getPropertyBeanInfo_1.getPropertyBeanInfo)(classElement);
if (qualifiedType === null) {
return;
}
BeanRepository_1.BeanRepository.registerBean({
classMemberName: classElement.name.getText(),
nestedProperty: null,
contextDescriptor: contextDescriptor,
scope: beanInfo.scope,
qualifiedType: qualifiedType,
node: classElement,
beanKind: 'property',
//Will be assigned when resolving dependencies
beanSourceLocation: null,
isPublic: false,
});
};
exports.registerPropertyBean = registerPropertyBean;
function getBeanTypeInfoFromClassProperty(contextDescriptor, classElement) {
var _a, _b, _c;
var propertyType = (_a = classElement.type) !== null && _a !== void 0 ? _a : null;
var beanGenericType = (_c = ((_b = classElement.initializer.typeArguments) !== null && _b !== void 0 ? _b : [])[0]) !== null && _c !== void 0 ? _c : null;
if (propertyType !== null && beanGenericType !== null) {
//TODO add caching of type nodes in typeQualifier
var resolvedPropertyType = TypeQualifier_1.TypeQualifier.qualify(propertyType);
var resolvedBeanGenericType = TypeQualifier_1.TypeQualifier.qualify(beanGenericType);
if (resolvedPropertyType === null && resolvedBeanGenericType === null) {
return null;
}
if (resolvedPropertyType !== null && resolvedBeanGenericType !== null && (0, lodash_1.isEqual)(resolvedPropertyType.typeIds, resolvedBeanGenericType.typeIds)) {
return resolvedBeanGenericType;
}
else {
CompilationContext_1.CompilationContext.reportError({
node: beanGenericType,
message: 'Bean generic type and property type should be equal',
filePath: contextDescriptor.absolutePath,
relatedContextPath: contextDescriptor.absolutePath,
});
}
if (resolvedBeanGenericType !== null) {
return resolvedBeanGenericType;
}
if (resolvedPropertyType !== null) {
return resolvedPropertyType;
}
}
if (propertyType === null && beanGenericType !== null) {
return TypeQualifier_1.TypeQualifier.qualify(beanGenericType);
}
if (beanGenericType === null && propertyType !== null) {
return TypeQualifier_1.TypeQualifier.qualify(propertyType);
}
var firstArgument = classElement.initializer.arguments[0];
if (!ts.isIdentifier(firstArgument)) {
CompilationContext_1.CompilationContext.reportError({
node: firstArgument,
message: 'First argument in property bean should be a class reference',
filePath: contextDescriptor.absolutePath,
relatedContextPath: contextDescriptor.absolutePath,
});
return null;
}
var nodeSourceDescriptor = (0, node_source_descriptor_1.getNodeSourceDescriptorDeep)(firstArgument.getSourceFile(), firstArgument.getText());
if (nodeSourceDescriptor === null) {
CompilationContext_1.CompilationContext.reportError({
node: firstArgument,
message: 'Can\'t qualify type of Bean, please specify type explicitly',
filePath: contextDescriptor.absolutePath,
relatedContextPath: contextDescriptor.absolutePath,
});
return null;
}
var typeReferenceFullName = "" + nodeSourceDescriptor.name + nodeSourceDescriptor.path;
var qualifiedType = new QualifiedType_1.QualifiedType();
qualifiedType.typeIds = new ExtendedSet_1.ExtendedSet([typeReferenceFullName]);
qualifiedType.fullTypeId = typeReferenceFullName;
qualifiedType.typeNode = typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier(firstArgument.getText()));
return qualifiedType;
}