dipend
Version:
This library implements a dependency injection (DI) system in JavaScript/TypeScript, making it easier to manage dependencies in modular applications.
73 lines (71 loc) • 3.3 kB
JavaScript
/*
* Copyright 2025 Saulo V. Alvarenga. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetInterfacesTransform = void 0;
const tslib_1 = require("tslib");
const typescript_1 = tslib_1.__importDefault(require("typescript"));
const crypto_1 = require("crypto");
const base_transform_1 = require("./base-transform");
class GetInterfacesTransform extends base_transform_1.BaseTransform {
isValidDependencyContainerCall(nodeExpression) {
const validMethods = new Set([
"addSingletonBuilder",
"addMappedSingletonBuilder",
"addSingletonInstance",
"addMappedSingletonInstance",
"addSingleton",
"addMappedSingleton",
"addTransientBuilder",
"addMappedTransientBuilder",
"addTransient",
"addMappedTransient",
]);
if (!validMethods.has(nodeExpression.name.getText()))
return false;
const symbol = this.typeChecker.getSymbolAtLocation(nodeExpression.expression);
if (!symbol?.valueDeclaration)
return false;
const type = this.typeChecker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration);
return this.typeChecker.typeToString(type) === "DependencyContainer";
}
registerInterface(declaration) {
const symbolForInterface = typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("Symbol"), "for"), undefined, [typescript_1.default.factory.createStringLiteral(`DI_${declaration.name.getText()}_${(0, crypto_1.randomUUID)()}`)]);
this.interfaces.set(declaration, symbolForInterface);
}
registerDependencyToken(dependencyToken) {
const dependencyTokenType = this.typeChecker.getTypeFromTypeNode(dependencyToken);
const dependencyTokenSymbol = dependencyTokenType.getSymbol();
if (!dependencyTokenSymbol?.declarations)
return;
dependencyTokenSymbol.declarations.forEach((declaration) => {
if (this.tsInstance.isInterfaceDeclaration(declaration)) {
this.registerInterface(declaration);
}
});
}
execute(node) {
if (!this.tsInstance.isCallExpression(node) || !this.tsInstance.isPropertyAccessExpression(node.expression))
return;
if (!this.isValidDependencyContainerCall(node.expression))
return;
const dependencyToken = node.typeArguments?.[0];
if (!dependencyToken)
return;
this.registerDependencyToken(dependencyToken);
}
}
exports.GetInterfacesTransform = GetInterfacesTransform;