fix-if-blocks.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const fs = require('fs');
  2. const path = require('path');
  3. // 读取HTML文件
  4. const htmlFilePath = path.join(__dirname, 'src/app/pages/designer/project-detail/project-detail.html');
  5. let htmlContent = fs.readFileSync(htmlFilePath, 'utf8');
  6. console.log('开始修复@if控制流闭合问题...');
  7. function fixIfBlocks(content) {
  8. const lines = content.split('\n');
  9. const stack = [];
  10. const fixedLines = [];
  11. for (let i = 0; i < lines.length; i++) {
  12. const line = lines[i];
  13. const trimmedLine = line.trim();
  14. // 检测@if控制流开始
  15. if (trimmedLine.includes('@if') && trimmedLine.includes('{')) {
  16. stack.push({
  17. type: 'if',
  18. line: i + 1,
  19. indent: line.match(/^\s*/)[0] // 保存缩进
  20. });
  21. fixedLines.push(line);
  22. continue;
  23. }
  24. // 检测控制流结束
  25. if (trimmedLine === '}' && stack.length > 0) {
  26. const lastBlock = stack[stack.length - 1];
  27. if (lastBlock.type === 'if') {
  28. stack.pop();
  29. }
  30. fixedLines.push(line);
  31. continue;
  32. }
  33. fixedLines.push(line);
  34. }
  35. // 为未闭合的@if块添加闭合大括号
  36. while (stack.length > 0) {
  37. const unclosed = stack.pop();
  38. if (unclosed.type === 'if') {
  39. fixedLines.push(unclosed.indent + '}');
  40. console.log(`为第${unclosed.line}行的@if块添加闭合大括号`);
  41. }
  42. }
  43. return fixedLines.join('\n');
  44. }
  45. // 修复@if控制流
  46. const fixedContent = fixIfBlocks(htmlContent);
  47. // 写回文件
  48. fs.writeFileSync(htmlFilePath, fixedContent, 'utf8');
  49. console.log('@if控制流修复完成!');