lcw
This commit is contained in:
94
check_unused_dicts.js
Normal file
94
check_unused_dicts.js
Normal file
@ -0,0 +1,94 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const baseDir = path.join(__dirname, 'src/views/backOfficeSystem');
|
||||
|
||||
// Find all .vue files with proxy.$dict
|
||||
function findVueFiles(dir) {
|
||||
let results = [];
|
||||
const files = fs.readdirSync(dir);
|
||||
for (const file of files) {
|
||||
const filePath = path.join(dir, file);
|
||||
const stat = fs.statSync(filePath);
|
||||
if (stat.isDirectory()) {
|
||||
results = results.concat(findVueFiles(filePath));
|
||||
} else if (file.endsWith('.vue')) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
if (content.includes('proxy.$dict')) {
|
||||
results.push(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// Parse dict variables and their usage
|
||||
function analyzeFile(filePath) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
|
||||
// Find the dict destructuring pattern
|
||||
// Pattern: const { VAR1, VAR2, ... } = proxy.$dict("KEY1", "KEY2", ...)
|
||||
|
||||
// Extract the destructured variable names
|
||||
const destructMatch = content.match(/const\s*\{([^}]+)\}\s*=\s*proxy\.\$dict\s*\(/);
|
||||
if (!destructMatch) return null;
|
||||
|
||||
const varsStr = destructMatch[1];
|
||||
const dictVars = varsStr.split(',').map(v => v.trim().replace(/\/\/.*$/, '').trim()).filter(v => v && !v.startsWith('//'));
|
||||
|
||||
// Extract the dict keys
|
||||
const dictCallMatch = content.match(/proxy\.\$dict\s*\(([^)]+)\)/s);
|
||||
if (!dictCallMatch) return null;
|
||||
|
||||
const dictKeysStr = dictCallMatch[1];
|
||||
const dictKeys = dictKeysStr.split(',').map(k => k.trim().replace(/['"]/g, '').replace(/\/\/.*$/, '').trim()).filter(k => k && !k.startsWith('//'));
|
||||
|
||||
// Now check which dict vars are actually used in the file
|
||||
// Remove the dict declaration part first
|
||||
const scriptContent = content.replace(/const\s*\{[^}]+\}\s*=\s*proxy\.\$dict\s*\([^)]+\)[^;\n]*;?/s, '');
|
||||
|
||||
const unusedVars = [];
|
||||
const usedVars = [];
|
||||
|
||||
for (const varName of dictVars) {
|
||||
if (!varName) continue;
|
||||
// Check if the variable name appears elsewhere in the file (outside the dict declaration)
|
||||
// Look for: varName in template, searchConfiger, getMultiDictVal, DictTag :options, :dict= etc.
|
||||
const regex = new RegExp('\\b' + varName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\b');
|
||||
const matches = scriptContent.match(regex);
|
||||
if (matches && matches.length > 0) {
|
||||
usedVars.push(varName);
|
||||
} else {
|
||||
unusedVars.push(varName);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
filePath,
|
||||
dictVars,
|
||||
dictKeys,
|
||||
unusedVars,
|
||||
usedVars
|
||||
};
|
||||
}
|
||||
|
||||
const vueFiles = findVueFiles(baseDir);
|
||||
console.log(`Found ${vueFiles.length} files with proxy.$dict\n`);
|
||||
|
||||
let totalUnused = 0;
|
||||
const filesWithUnused = [];
|
||||
|
||||
for (const filePath of vueFiles) {
|
||||
const result = analyzeFile(filePath);
|
||||
if (result && result.unusedVars.length > 0) {
|
||||
const relPath = path.relative(__dirname, filePath).replace(/\\/g, '/');
|
||||
console.log(`\n${relPath}:`);
|
||||
console.log(` Unused: ${result.unusedVars.join(', ')}`);
|
||||
filesWithUnused.push(result);
|
||||
totalUnused += result.unusedVars.length;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n\n=== Summary ===`);
|
||||
console.log(`Total files with unused dicts: ${filesWithUnused.length}`);
|
||||
console.log(`Total unused dict variables: ${totalUnused}`);
|
||||
Reference in New Issue
Block a user