UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

82 lines 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImportAnalyzer = void 0; const MapUtil_1 = require("../utils/MapUtil"); var ImportAnalyzer; (function (ImportAnalyzer) { /** * @deprecated Removed in the Go-migration cycle. Import metadata is now * attached by the native transformer in * `packages/core/native/cmd/ttsc-nestia` and consumed via * `IOperationMetadata.imports`. Call sites that previously walked a * `ts.SourceFile` to derive imports should read the metadata delivered * through `Reflect.getMetadata("nestia/OperationMetadata", …)` instead. See * `packages/core/MIGRATION.md`. */ ImportAnalyzer.analyze = () => { throw new Error("ImportAnalyzer.analyze was removed in @nestia/sdk@next. " + "Imports are now attached by the native transformer; read them from " + "IOperationMetadata.imports (see packages/core/MIGRATION.md)."); }; ImportAnalyzer.merge = (imports) => { // group by files const fileGroups = new Map(); for (const imp of imports) { const array = MapUtil_1.MapUtil.take(fileGroups, imp.file, () => []); array.push(imp); } return Array.from(fileGroups.entries()) .map(([key, value]) => mergeGroup(key, value)) .flat(); }; function mergeGroup(file, imports) { var _a; var _b, _c; const allStarImports = Array.from(new Set(imports .filter((imp) => imp.asterisk !== null) .map((imp) => imp.asterisk))) .sort() .map((allStarImport) => ({ file, elements: [], default: null, asterisk: allStarImport, })); const defaultImports = Array.from(new Set(imports .filter((imp) => imp.default !== null) .map((imp) => imp.default))) .sort() .map((defaultImport) => ({ file, elements: [], default: defaultImport, asterisk: null, })); const instances = new Map(); for (const imp of imports) for (const local of imp.elements) instances.set(local, (_b = (_a = imp.elementAliases) === null || _a === void 0 ? void 0 : _a[local]) !== null && _b !== void 0 ? _b : local); if (instances.size !== 0) { const elements = Array.from(instances.keys()).sort(); const elementAliases = {}; for (const local of elements) { const imported = instances.get(local); if (imported !== local) elementAliases[local] = imported; } const target = (_c = defaultImports[0]) !== null && _c !== void 0 ? _c : { file, elements: [], default: null, asterisk: null, }; target.elements = elements; if (Object.keys(elementAliases).length !== 0) target.elementAliases = elementAliases; if (defaultImports.length === 0) defaultImports.push(target); } return [...allStarImports, ...defaultImports]; } })(ImportAnalyzer || (exports.ImportAnalyzer = ImportAnalyzer = {})); //# sourceMappingURL=ImportAnalyzer.js.map