UNPKG

mp-lens

Version:

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

101 lines 4.29 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.lintComponentUsage = lintComponentUsage; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const debug_logger_1 = require("../../utils/debug-logger"); const standard_wxml_tags_1 = require("./standard-wxml-tags"); /** * Performs component usage linting by comparing declarations with usage * * @param wxmlFilePath Path to the WXML file * @param jsonFilePath Path to the corresponding JSON file * @param usedTagToFiles Map<tag, Set<wxmlFile>> * @param globalComponents Object containing globally declared components from app.json * @returns Lint issue object containing any identified problems */ function lintComponentUsage(wxmlFilePath, jsonFilePath, usedTagToFiles, globalComponents) { // Initialize result object const result = { id: '', // will be set by processFilePair wxmlFile: wxmlFilePath, jsonFile: jsonFilePath, declaredNotUsed: [], usedNotDeclared: [], }; try { // Read and parse JSON file const jsonContent = fs.readFileSync(jsonFilePath, 'utf-8'); const jsonData = JSON.parse(jsonContent); // Extract using components const localUsingComponents = jsonData.usingComponents || {}; // Extract component generics if they exist const componentGenerics = jsonData.componentGenerics || {}; // Merge local components with global components to form all declared components const declaredComponents = { ...globalComponents, ...localUsingComponents, ...componentGenerics, }; // Filter out standard WXML tags from used tags const usedCustomTags = Array.from(usedTagToFiles.keys()).filter((tag) => (0, standard_wxml_tags_1.filterStandardWxmlTags)(new Set([tag])).size > 0); // Find components declared but not used for (const [componentTag, componentPath] of Object.entries(declaredComponents)) { if (!usedCustomTags.includes(componentTag)) { result.declaredNotUsed.push({ componentTag, path: componentPath, }); } } // Find tags used but not declared for (const tag of usedCustomTags) { if (!declaredComponents[tag]) { const usedIn = Array.from(usedTagToFiles.get(tag) || []); result.usedNotDeclared.push({ componentTag: tag, usedIn, suggestion: `Consider adding '${tag}' to the usingComponents section in ${path.basename(jsonFilePath)}`, }); } } } catch (err) { debug_logger_1.logger.error(`Error linting component usage for ${wxmlFilePath}: ${err}`); } return result; } //# sourceMappingURL=component-linter.js.map