UNPKG

mp-lens

Version:

微信小程序分析工具 (Unused Code, Dependencies, Visualization)

93 lines 3.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.isPureAmbientDeclarationFile = isPureAmbientDeclarationFile; exports.isDeclarationFile = isDeclarationFile; exports.findPureAmbientDeclarationFiles = findPureAmbientDeclarationFiles; const fs = __importStar(require("fs")); const debug_logger_1 = require("./debug-logger"); /** * Checks if a .d.ts file is a pure ambient declaration file. * Pure ambient declaration files add types to the global scope without using * import/export statements, making them automatically available without explicit imports. * * @param filePath The absolute path to the .d.ts file * @returns true if the file is a pure ambient declaration file, false otherwise */ function isPureAmbientDeclarationFile(filePath) { if (!filePath.endsWith('.d.ts')) { return false; } try { const content = fs.readFileSync(filePath, 'utf-8'); // Check if it contains any imports or exports (module-style) const hasModuleElements = /\b(import|export)\b/.test(content); // If it has module elements, it needs to be imported if (hasModuleElements) { return false; } // Check if it has ambient declarations const hasAmbientDeclarations = /\bdeclare\b/.test(content); return hasAmbientDeclarations; } catch (err) { // Handle error debug_logger_1.logger.debug(`Error reading d.ts file ${filePath}: ${err.message}`); return false; } } /** * Checks if a file is a TypeScript declaration file (.d.ts) */ function isDeclarationFile(filePath) { return filePath.endsWith('.d.ts'); } /** * Finds all .d.ts files in a project that are pure ambient declaration files. * These files should not be marked as unused even if they're not explicitly imported. * * @param projectRoot The absolute path to the project root * @param allFiles An array of all files found in the project * @returns An array of absolute paths to pure ambient declaration files */ function findPureAmbientDeclarationFiles(projectRoot, allFiles) { const declarationFiles = allFiles.filter((file) => file.endsWith('.d.ts')); const ambientDeclarationFiles = declarationFiles.filter(isPureAmbientDeclarationFile); if (ambientDeclarationFiles.length > 0) { debug_logger_1.logger.debug(`Found ${ambientDeclarationFiles.length} pure ambient declaration files that will be preserved`); } return ambientDeclarationFiles; } //# sourceMappingURL=typescript-helper.js.map