#!/usr/bin/env node /** * Uni-app-x UTS 兼容性测试脚本 * 用于验证关键文件的语法正确性和编译兼容性 */ const fs = require('fs'); const path = require('path'); // 需要检查的关键文件 const criticalFiles = [ 'pages/sport/teacher/analytics.uvue', 'pages/sport/teacher/project-create.uvue', 'uni_modules/ak-charts/components/ak-charts.uvue', 'components/simple-icon/simple-icon.uvue' ]; // UTS 兼容性检查模式 const utsCompatibilityChecks = [ { name: 'UTSJSONObject 安全访问', pattern: /\w+\['[\w_]+'\]/g, description: '检查是否使用了不安全的括号访问方式', severity: 'error' }, { name: 'Number() 构造函数使用', pattern: /Number\(/g, description: '检查是否使用了 Number() 构造函数(应使用 parseFloat)', severity: 'warning' }, { name: 'responsiveState 调用', pattern: /responsiveState\(/g, description: '检查是否使用了已废弃的 responsiveState', severity: 'error' }, { name: 'text 元素 flex 属性', pattern: /]*(?:justify-content|align-items)/g, description: '检查 text 元素是否使用了不支持的 flex 属性', severity: 'error' }, { name: 'view 元素字体属性', pattern: /]*font-/g, description: '检查 view 元素是否使用了不推荐的字体属性', severity: 'warning' } ]; console.log('🔍 开始 Uni-app-x UTS 兼容性检测...\n'); let totalIssues = 0; let totalErrors = 0; let totalWarnings = 0; criticalFiles.forEach(filePath => { const fullPath = path.join(__dirname, filePath); if (!fs.existsSync(fullPath)) { console.log(`❌ 文件不存在: ${filePath}`); totalErrors++; return; } console.log(`📄 检查文件: ${filePath}`); const content = fs.readFileSync(fullPath, 'utf8'); let fileIssues = 0; utsCompatibilityChecks.forEach(check => { const matches = content.match(check.pattern); if (matches && matches.length > 0) { const icon = check.severity === 'error' ? '🚫' : '⚠️'; console.log(` ${icon} ${check.name}: 发现 ${matches.length} 个问题`); console.log(` ${check.description}`); // 显示前几个匹配的示例 matches.slice(0, 3).forEach((match, index) => { console.log(` 示例 ${index + 1}: ${match}`); }); fileIssues += matches.length; if (check.severity === 'error') { totalErrors += matches.length; } else { totalWarnings += matches.length; } } }); if (fileIssues === 0) { console.log(` ✅ 通过所有兼容性检查`); } totalIssues += fileIssues; console.log(''); }); // 生成测试报告 console.log('📊 检测结果汇总:'); console.log(` 总检查文件: ${criticalFiles.length}`); console.log(` 发现问题总数: ${totalIssues}`); console.log(` 错误数量: ${totalErrors}`); console.log(` 警告数量: ${totalWarnings}`); if (totalErrors === 0) { console.log('\n🎉 恭喜!所有关键文件都通过了 UTS 兼容性检查!'); console.log('✅ 项目已准备好进行 uni-app-x 编译和部署'); } else { console.log('\n❌ 发现兼容性问题,需要进一步修复'); process.exit(1); } // 额外的语法检查 console.log('\n🔧 进行基础语法检查...'); criticalFiles.forEach(filePath => { const fullPath = path.join(__dirname, filePath); const content = fs.readFileSync(fullPath, 'utf8'); // 检查基本语法问题 const syntaxIssues = []; // 检查未闭合的标签 const openTags = content.match(/<(?!\/)[^>]+(?/g) || []; const closeTags = content.match(/<\/[^>]+>/g) || []; // 检查 computed 类型推断 if (content.includes('computed({') && !content.includes('computed<')) { syntaxIssues.push('可能存在 computed 类型推断问题'); } // 检查 UTSJSONObject 使用 if (content.includes('UTSJSONObject') && content.includes('[')) { const jsonObjectAccess = content.match(/\w+\[['"][^'"]+['"]\]/g); if (jsonObjectAccess) { syntaxIssues.push(`发现可能的 UTSJSONObject 不安全访问: ${jsonObjectAccess.length} 处`); } } if (syntaxIssues.length > 0) { console.log(`⚠️ ${filePath}:`); syntaxIssues.forEach(issue => { console.log(` - ${issue}`); }); } else { console.log(`✅ ${filePath}: 语法检查通过`); } }); console.log('\n✨ UTS 兼容性检测完成!');