UNPKG

a2r

Version:
58 lines (57 loc) 2.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const typescript_1 = __importDefault(require("typescript")); const getNamedImports = (nodes, usedTypes, sourceFile) => { const res = []; for (let i = 0, l = nodes.length; i < l; i++) { const node = nodes[i]; if (typescript_1.default.isIdentifier(node)) { const nodeText = node.getFullText(sourceFile).trim(); if (usedTypes.indexOf(nodeText) !== -1) { res.push(nodeText); } } else { const children = node.getChildren(sourceFile); if (children.length) { res.push(...getNamedImports(children, usedTypes, sourceFile)); } } } return res; }; const getGroupedModelImports = (initialImports, imports, usedTypes) => { const res = initialImports.slice(); for (let i = 0, l = imports.length; i < l; i++) { const { clause, path, sourceFile } = imports[i]; const pathForProxy = path.replace(/([./]+)\/model/, '../model'); let grouped = res.find((g) => g.path === pathForProxy); if (!grouped) { grouped = { path: pathForProxy, }; res.push(grouped); } const children = clause.getChildren(sourceFile); for (let j = 0, k = children.length; j < k; j++) { const child = children[j]; if (typescript_1.default.isIdentifier(child)) { const defaultExport = child.getFullText(sourceFile).trim(); if (usedTypes.indexOf(defaultExport) !== -1) { grouped.def = defaultExport; } } if (typescript_1.default.isNamedImports(child)) { grouped.named = Array.from(new Set([ ...(grouped.named || []), ...getNamedImports(child.getChildren(sourceFile), usedTypes, sourceFile), ])); } } } return res; }; exports.default = getGroupedModelImports;