79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
const fs = require('fs');
|
||
const path = 'D:\\project\\zhizhu\\components\\PhotoUploadForm.vue';
|
||
const content = fs.readFileSync(path, 'utf8');
|
||
const lines = content.split('\n');
|
||
|
||
// 从 export default 开始追踪
|
||
let exportStart = -1;
|
||
for (let i = 0; i < lines.length; i++) {
|
||
if (lines[i].includes('export default {')) {
|
||
exportStart = i;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 从 export default + 1 开始计数
|
||
let braceCount = 0;
|
||
let prevCount = 0;
|
||
let inImport = false;
|
||
|
||
for (let i = exportStart; i < lines.length; i++) {
|
||
const line = lines[i];
|
||
|
||
// 跳过 import(不应该出现在 export 内部,但以防万一)
|
||
if (line.includes('import ')) continue;
|
||
|
||
// 跳过注释
|
||
const codeOnly = line.replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
|
||
// 跳过字符串中的括号
|
||
const noStrings = codeOnly.replace(/'[^']*'/g, '""').replace(/"[^"]*"/g, '""').replace(/`[^`]*`/g, '""');
|
||
|
||
const opens = (noStrings.match(/{/g) || []).length;
|
||
const closes = (noStrings.match(/}/g) || []).length;
|
||
prevCount = braceCount;
|
||
braceCount += opens - closes;
|
||
|
||
// 当 braceCount 从 1 变到 2 时,记录区域开始
|
||
if (prevCount === 1 && braceCount === 2) {
|
||
console.log(`\n>>> Section starts at line ${i + 1}: ${line.trim()}`);
|
||
}
|
||
|
||
// 当 braceCount 从 2 变到 1 时,记录区域结束
|
||
if (prevCount === 2 && braceCount === 1) {
|
||
console.log(`<<< Section ends at line ${i + 1}: ${line.trim()}`);
|
||
}
|
||
}
|
||
|
||
console.log(`\nFinal braceCount: ${braceCount}`);
|
||
|
||
// 更详细地检查每个属性区域
|
||
console.log('\n=== Detailed section analysis ===\n');
|
||
|
||
// 找到各个属性的行号
|
||
const sections = [];
|
||
const sectionNames = ['name', 'components', 'props', 'computed', 'data', 'watch', 'methods'];
|
||
for (const name of sectionNames) {
|
||
for (let i = exportStart; i < lines.length; i++) {
|
||
if (lines[i].match(new RegExp(`^\\s+${name}\\s*:`))) {
|
||
sections.push({ name, line: i + 1 });
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 追踪每个 sections 的括号平衡
|
||
for (let s = 0; s < sections.length; s++) {
|
||
const startLine = sections[s].line - 1;
|
||
const endLine = s + 1 < sections.length ? sections[s + 1].line - 1 : lines.length;
|
||
|
||
let sectionBraces = 0;
|
||
for (let i = startLine; i < endLine; i++) {
|
||
const codeOnly = lines[i].replace(/\/\/.*$/, '').replace(/\/\*.*?\*\//g, '');
|
||
const noStrings = codeOnly.replace(/'[^']*'/g, '""').replace(/"[^"]*"/g, '""').replace(/`[^`]*`/g, '""');
|
||
sectionBraces += (noStrings.match(/{/g) || []).length;
|
||
sectionBraces -= (noStrings.match(/}/g) || []).length;
|
||
}
|
||
|
||
console.log(`${sections[s].name} (lines ${sections[s].line}-${endLine}): brace balance = ${sectionBraces}`);
|
||
}
|