UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

307 lines (299 loc) 16.3 kB
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import path from 'path'; export function parseImports(_x, _x2) { return _parseImports.apply(this, arguments); } function _parseImports() { _parseImports = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(code, _filePath) { var result, externals, importRegex, sideEffectImportRegex, match, _loop, _ret, sideEffectMatch, _sideEffectMatch, _sideEffectMatch2, modulePathRaw, modulePath, isRelative, resolvedPath; return _regeneratorRuntime().wrap(function _callee$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: result = {}; externals = {}; // Regex to extract regular import statements with 'from' clause importRegex = /import(?:\s+type)?\s*([^'"]*)\s*from\s*['"]([^'"]+)['"][;]?/g; // Regex to extract side-effect imports (without 'from' clause) sideEffectImportRegex = /import\s*['"]([^'"]+)['"][;]?/g; // eslint-disable-next-line no-cond-assign _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() { var _match, _match2, fullMatch, importSpecifierRaw, modulePathRaw, includeTypeDefs, importSpecifier, modulePath, isRelative, _resolvedPath, _resolvedPath2, defaultImportMatch, namespaceImportMatch, namedImportsMatch, defaultImport, namespaceImport, namedImportsStr, existing, _existing, cleanedImportsStr, namedImports, _defaultImportMatch, _namespaceImportMatch, _namedImportsMatch, _defaultImport, _namespaceImport, _namedImportsStr, _existing3, _existing4, _cleanedImportsStr, _namedImports; return _regeneratorRuntime().wrap(function _loop$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _match = match, _match2 = _slicedToArray(_match, 3), fullMatch = _match2[0], importSpecifierRaw = _match2[1], modulePathRaw = _match2[2]; // Determine if this is a type import by checking if the full match contains "import type" includeTypeDefs = fullMatch.includes('import type'); importSpecifier = importSpecifierRaw == null ? void 0 : importSpecifierRaw.trim(); modulePath = modulePathRaw == null ? void 0 : modulePathRaw.trim(); if (modulePath) { _context.next = 6; break; } return _context.abrupt("return", 0); case 6: // Check if this is a relative import isRelative = modulePath.startsWith('./') || modulePath.startsWith('../'); // Handle side-effect imports (no import specifier) if (importSpecifier) { _context.next = 10; break; } if (isRelative) { // Resolve the relative import to an absolute path _resolvedPath = path.resolve(path.dirname(_filePath), modulePath); if (!result[modulePath]) { result[modulePath] = { path: _resolvedPath, names: [] // Empty names array for side-effect imports }; } } else if (!externals[modulePath]) { // External side-effect import externals[modulePath] = { names: [] }; } return _context.abrupt("return", 0); case 10: if (isRelative) { // Resolve the relative import to an absolute path _resolvedPath2 = path.resolve(path.dirname(_filePath), modulePath); if (!result[modulePath]) { result[modulePath] = _objectSpread({ path: _resolvedPath2, names: [] }, includeTypeDefs && { includeTypeDefs: true }); } else if (includeTypeDefs && !result[modulePath].includeTypeDefs) { result[modulePath].includeTypeDefs = true; } // Parse the import specifier to determine the type of import defaultImportMatch = importSpecifier.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)\s*(?:,|$)/); namespaceImportMatch = importSpecifier.match(/\*\s+as\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/); namedImportsMatch = importSpecifier.match(/\{([^}]+)\}/); defaultImport = defaultImportMatch == null ? void 0 : defaultImportMatch[1]; namespaceImport = namespaceImportMatch == null ? void 0 : namespaceImportMatch[1]; namedImportsStr = namedImportsMatch == null ? void 0 : namedImportsMatch[1]; if (defaultImport) { // Check if we already have this default import existing = result[modulePath].names.find(function (n) { return n.name === defaultImport && n.type === 'default'; }); if (!existing) { result[modulePath].names.push(_objectSpread({ name: defaultImport, type: 'default' }, includeTypeDefs && { isType: true })); } } if (namespaceImport) { // Check if we already have this namespace import _existing = result[modulePath].names.find(function (n) { return n.name === namespaceImport && n.type === 'namespace'; }); if (!_existing) { result[modulePath].names.push(_objectSpread({ name: namespaceImport, type: 'namespace' }, includeTypeDefs && { isType: true })); } } if (namedImportsStr) { // Handle named imports like { ComponentName, Component2 as Alias, type TypeName } // Clean up the string by removing comments and extra whitespace cleanedImportsStr = namedImportsStr.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* comment */ blocks .replace(/\/\/.*$/gm, '') // Remove // line comments .trim(); if (cleanedImportsStr) { namedImports = cleanedImportsStr.split(',').map(function (s) { return s.trim(); }).filter(function (s) { return s.length > 0; }); namedImports.forEach(function (namedImport) { // Check if this specific import is a type import var isTypeImport = namedImport.trim().startsWith('type '); // Clean up the import name (remove 'type' keyword and handle aliases) var cleanImport = namedImport.replace(/^type\s+/, ''); // Remove leading 'type' // Parse original name and alias var aliasMatch = cleanImport.match(/(.+?)\s+as\s+(.+)/); var originalName = aliasMatch ? aliasMatch[1].trim() : cleanImport.trim(); var alias = aliasMatch ? aliasMatch[2].trim() : undefined; // Only add if we have a valid name if (originalName && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(originalName)) { // Check if we already have this named import var _existing2 = result[modulePath].names.find(function (n) { return n.name === originalName && n.type === 'named' && n.alias === alias; }); if (!_existing2) { result[modulePath].names.push(_objectSpread(_objectSpread({ name: originalName }, alias && { alias: alias }), {}, { type: 'named' }, (includeTypeDefs || isTypeImport) && { isType: true })); } } }); } } } else { // This is an external import if (!externals[modulePath]) { externals[modulePath] = { names: [] }; } // Parse the import specifier to determine the type of import _defaultImportMatch = importSpecifier.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)\s*(?:,|$)/); _namespaceImportMatch = importSpecifier.match(/\*\s+as\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/); _namedImportsMatch = importSpecifier.match(/\{([^}]+)\}/); _defaultImport = _defaultImportMatch == null ? void 0 : _defaultImportMatch[1]; _namespaceImport = _namespaceImportMatch == null ? void 0 : _namespaceImportMatch[1]; _namedImportsStr = _namedImportsMatch == null ? void 0 : _namedImportsMatch[1]; if (_defaultImport) { // Check if we already have this default import _existing3 = externals[modulePath].names.find(function (n) { return n.name === _defaultImport && n.type === 'default'; }); if (!_existing3) { externals[modulePath].names.push(_objectSpread({ name: _defaultImport, type: 'default' }, includeTypeDefs && { isType: true })); } } if (_namespaceImport) { // Check if we already have this namespace import _existing4 = externals[modulePath].names.find(function (n) { return n.name === _namespaceImport && n.type === 'namespace'; }); if (!_existing4) { externals[modulePath].names.push(_objectSpread({ name: _namespaceImport, type: 'namespace' }, includeTypeDefs && { isType: true })); } } if (_namedImportsStr) { // Handle named imports like { ComponentName, Component2 as Alias, type TypeName } // Clean up the string by removing comments and extra whitespace _cleanedImportsStr = _namedImportsStr.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* comment */ blocks .replace(/\/\/.*$/gm, '') // Remove // line comments .trim(); if (_cleanedImportsStr) { _namedImports = _cleanedImportsStr.split(',').map(function (s) { return s.trim(); }).filter(function (s) { return s.length > 0; }); _namedImports.forEach(function (namedImport) { // Check if this specific import is a type import var isTypeImport = namedImport.trim().startsWith('type '); // Clean up the import name (remove 'type' keyword and handle aliases) var cleanImport = namedImport.replace(/^type\s+/, ''); // Remove leading 'type' // Parse original name and alias - for externals we track both var aliasMatch = cleanImport.match(/(.+?)\s+as\s+(.+)/); var originalName = aliasMatch ? aliasMatch[1].trim() : cleanImport.trim(); var alias = aliasMatch ? aliasMatch[2].trim() : undefined; // Only add if we have a valid name if (originalName && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(originalName)) { // Check if we already have this named import var _existing5 = externals[modulePath].names.find(function (n) { return n.name === originalName && n.type === 'named' && n.alias === alias; }); if (!_existing5) { externals[modulePath].names.push(_objectSpread(_objectSpread({ name: originalName }, alias && { alias: alias }), {}, { type: 'named' }, (includeTypeDefs || isTypeImport) && { isType: true })); } } }); } } } case 11: case "end": return _context.stop(); } }, _loop); }); case 5: if (!((match = importRegex.exec(code)) !== null)) { _context2.next = 12; break; } return _context2.delegateYield(_loop(), "t0", 7); case 7: _ret = _context2.t0; if (!(_ret === 0)) { _context2.next = 10; break; } return _context2.abrupt("continue", 5); case 10: _context2.next = 5; break; case 12: if (!((sideEffectMatch = sideEffectImportRegex.exec(code)) !== null)) { _context2.next = 21; break; } _sideEffectMatch = sideEffectMatch, _sideEffectMatch2 = _slicedToArray(_sideEffectMatch, 2), modulePathRaw = _sideEffectMatch2[1]; modulePath = modulePathRaw == null ? void 0 : modulePathRaw.trim(); if (modulePath) { _context2.next = 17; break; } return _context2.abrupt("continue", 12); case 17: // Check if this is a relative import isRelative = modulePath.startsWith('./') || modulePath.startsWith('../'); if (isRelative) { // Resolve the relative import to an absolute path resolvedPath = path.resolve(path.dirname(_filePath), modulePath); if (!result[modulePath]) { result[modulePath] = { path: resolvedPath, names: [] // Empty names array for side-effect imports }; } } else if (!externals[modulePath]) { // External side-effect import externals[modulePath] = { names: [] }; } _context2.next = 12; break; case 21: return _context2.abrupt("return", { relative: result, externals: externals }); case 22: case "end": return _context2.stop(); } }, _callee); })); return _parseImports.apply(this, arguments); }