UNPKG

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!

235 lines (234 loc) 13 kB
"use strict"; 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; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNodeSourceDescriptorDeep = void 0; var ts = __importStar(require("typescript")); var upath_1 = __importDefault(require("upath")); var lodash_1 = require("lodash"); var LocalExportDeclaration_1 = require("./LocalExportDeclaration"); var NamedStatement_1 = require("./NamedStatement"); var NamedExportStatement_1 = require("./NamedExportStatement"); var ImportDeclarationWithClauseAndNamedBindings_1 = require("./ImportDeclarationWithClauseAndNamedBindings"); var removeQuotesFromString_1 = require("../../utils/removeQuotesFromString"); var PathResolverCache_1 = require("../path-resolver/PathResolverCache"); var SourceFilesCache_1 = require("../source-files-cache/SourceFilesCache"); var ExportDeclarationWithoutClauseAndModuleSpecifier_1 = require("./ExportDeclarationWithoutClauseAndModuleSpecifier"); var ExternalExportDeclaration_1 = require("./ExternalExportDeclaration"); var CompilationContext_1 = require("../../../compilation-context/CompilationContext"); var isPathRelative_1 = require("../../utils/isPathRelative"); //TODO Add support for default imports/exports function getNodeSourceDescriptorDeep(sourceFile, nameToFind, exportNodesStack) { if (exportNodesStack === void 0) { exportNodesStack = []; } var splittedNameToFind = nameToFind.split('.'); //Trying to Find in imports----------------------------------------------------------------------------------------- var namedImports = sourceFile.statements.filter(ImportDeclarationWithClauseAndNamedBindings_1.isNamedImports); var namespaceImports = sourceFile.statements.filter(ImportDeclarationWithClauseAndNamedBindings_1.isNamespaceImportDeclaration); //Trying to find in NamespaceImports (import * as DDD from '')------------------------------------------------------ var foundNamespaceImport = namespaceImports.find(function (it) { return it.importClause.namedBindings.name.getText() === splittedNameToFind[0]; }); if (foundNamespaceImport !== undefined) { var modulePath = (0, removeQuotesFromString_1.removeQuotesFromString)(foundNamespaceImport.moduleSpecifier.getText()); var resolvedPath = PathResolverCache_1.PathResolverCache.getAbsolutePathWithExtension(sourceFile.fileName, modulePath); var newNameToFind = splittedNameToFind.slice(1).join('.'); if (!upath_1.default.isAbsolute(resolvedPath) && !(0, isPathRelative_1.isPathRelative)(resolvedPath)) { return { node: null, path: resolvedPath, name: newNameToFind, }; } if (!upath_1.default.isAbsolute(resolvedPath)) { return getNodeSourceDescriptorDeep(SourceFilesCache_1.SourceFilesCache.getSourceFileByPath(resolvedPath), nameToFind, exportNodesStack); } var newSourceFile = SourceFilesCache_1.SourceFilesCache.getSourceFileByPath(resolvedPath); return getNodeSourceDescriptorDeep(newSourceFile, newNameToFind, exportNodesStack); } //Trying to find in NamedImports import { dd as cc } from ''-------------------------------------------------------- var originalNameFromNamedImport = ''; var foundNamedImport = namedImports.find(function (it) { return it.importClause.namedBindings.elements.some(function (it) { if (it.name.getText() === splittedNameToFind[0]) { originalNameFromNamedImport = it.propertyName ? it.propertyName.getText() : it.name.getText(); return true; } return false; }); }); if (foundNamedImport !== undefined) { var modulePath = (0, removeQuotesFromString_1.removeQuotesFromString)(foundNamedImport.moduleSpecifier.getText()); var resolvedPath = PathResolverCache_1.PathResolverCache.getAbsolutePathWithExtension(sourceFile.fileName, modulePath); var newNameToFind = __spreadArray([ originalNameFromNamedImport ], __read(splittedNameToFind.slice(1)), false).join('.'); if (!upath_1.default.isAbsolute(resolvedPath)) { return { path: resolvedPath, name: newNameToFind, node: null, }; } var newSourceFile = SourceFilesCache_1.SourceFilesCache.getSourceFileByPath(resolvedPath); return getNodeSourceDescriptorDeep(newSourceFile, newNameToFind, exportNodesStack); } //Trying to find named export statement in current file------------------------------------------------------------- var nodeSourceDescriptorFromTopStatements = getNodeSourceDescriptorFromTopStatements(sourceFile, nameToFind); if (nodeSourceDescriptorFromTopStatements !== null) { return nodeSourceDescriptorFromTopStatements; } var exportDeclarations = sourceFile.statements.filter(ts.isExportDeclaration); //Trying to find node in local exports------------------------------------------------------------------------------ var localExportClauses = exportDeclarations .filter(LocalExportDeclaration_1.isLocalExportDeclaration) .map(function (it) { return it.exportClause; }); var localExportSpecifier = (0, lodash_1.flatten)(localExportClauses .filter(ts.isNamedExports) .map(function (it) { return it.elements; })) .find(function (it) { return it.name.getText() === splittedNameToFind[0]; }); if (localExportSpecifier !== undefined) { var originalLocalName_1 = localExportSpecifier.propertyName ? localExportSpecifier.propertyName.getText() : localExportSpecifier.name.getText(); var statement = sourceFile.statements .filter(NamedStatement_1.isNamedStatement) .find(function (it) { return it.name.getText() === originalLocalName_1; }); if (statement !== undefined) { var joinedLocalName = __spreadArray([ originalLocalName_1 ], __read(splittedNameToFind.slice(1)), false).join('.'); return { path: sourceFile.fileName, name: joinedLocalName, node: statement, }; } } //Trying to find node in external exports var externalExports = exportDeclarations.filter(ExternalExportDeclaration_1.isExternalExportDeclaration); var externalNamespaceExport = externalExports.find(function (it) { if (ts.isNamespaceExport(it.exportClause)) { return it.exportClause.name.getText() === splittedNameToFind[0]; } return false; }); if (externalNamespaceExport !== undefined && !exportNodesStack.includes(externalNamespaceExport)) { exportNodesStack.push(externalNamespaceExport); var modulePath = (0, removeQuotesFromString_1.removeQuotesFromString)(externalNamespaceExport.moduleSpecifier.getText()); var resolvedPath = PathResolverCache_1.PathResolverCache.getAbsolutePathWithExtension(sourceFile.fileName, modulePath); if (!upath_1.default.isAbsolute(resolvedPath)) { CompilationContext_1.CompilationContext.reportError({ node: externalNamespaceExport, message: 'DI container does not support export * as Something from "node-module"', filePath: sourceFile.fileName, }); return null; } var newSourceFile = SourceFilesCache_1.SourceFilesCache.getSourceFileByPath(resolvedPath); return getNodeSourceDescriptorDeep(newSourceFile, splittedNameToFind[1], exportNodesStack); } var externalNamedExports = externalExports.filter(ExternalExportDeclaration_1.isNamedExternalExportsDeclaration); var externalExportClauseElements = (0, lodash_1.flatten)(externalNamedExports.map(function (it) { return it.exportClause.elements; })); var externalExportSpecifier = externalExportClauseElements.find(function (it) { return it.name.getText() === splittedNameToFind[0]; }); if (externalExportSpecifier !== undefined && !exportNodesStack.includes(externalExportSpecifier.parent.parent)) { var exportExpression = externalExportSpecifier.parent.parent; exportNodesStack.push(exportExpression); var externalExportNameToFind = externalExportSpecifier.propertyName ? __spreadArray([ externalExportSpecifier.propertyName.getText() ], __read(splittedNameToFind.slice(1)), false).join('.') : splittedNameToFind.join('.'); var modulePath = (0, removeQuotesFromString_1.removeQuotesFromString)(exportExpression.moduleSpecifier.getText()); var resolvedPath = PathResolverCache_1.PathResolverCache.getAbsolutePathWithExtension(sourceFile.fileName, modulePath); if (!upath_1.default.isAbsolute(resolvedPath)) { CompilationContext_1.CompilationContext.reportError({ node: externalExportSpecifier, message: 'DI container does not support export * as Something from "node-module"', filePath: sourceFile.fileName, }); return null; } var newSourceFile = SourceFilesCache_1.SourceFilesCache.getSourceFileByPath(resolvedPath); return getNodeSourceDescriptorDeep(newSourceFile, externalExportNameToFind, exportNodesStack); } //Trying to find in export * from '' declarations var exportAllStatements = exportDeclarations.filter(ExportDeclarationWithoutClauseAndModuleSpecifier_1.isExportDeclarationWithoutClauseAndWithModuleSpecifier); var result = null; exportAllStatements.some(function (it) { var modulePath = (0, removeQuotesFromString_1.removeQuotesFromString)(it.moduleSpecifier.getText()); var resolvedPath = PathResolverCache_1.PathResolverCache.getAbsolutePathWithExtension(sourceFile.fileName, modulePath); if (!upath_1.default.isAbsolute(resolvedPath)) { CompilationContext_1.CompilationContext.reportError({ message: 'DI container does not support export * from "node-module"', node: it, filePath: sourceFile.fileName, }); return null; } if (!exportNodesStack.includes(it)) { exportNodesStack.push(it); var newSourceFile = SourceFilesCache_1.SourceFilesCache.getSourceFileByPath(resolvedPath); result = getNodeSourceDescriptorDeep(newSourceFile, nameToFind, exportNodesStack); return Boolean(result); } return false; }); return result; } exports.getNodeSourceDescriptorDeep = getNodeSourceDescriptorDeep; function getNodeSourceDescriptorFromTopStatements(sourceFile, nameToFind) { var leftHandExpression = nameToFind.split('.')[0]; var topExportStatements = sourceFile.statements.filter(NamedExportStatement_1.isNamedExportStatement); var statement = topExportStatements.find(function (it) { return it.name.getText() === leftHandExpression; }); if (statement === undefined) { return null; } return { name: nameToFind, path: sourceFile.fileName, node: statement, }; }