@@ -383618,6 +383618,14 @@ var FileHeaderManager = class {
383618383618 headerRegex: /@header\(([\s\S]*?)\)/,
383619383619 createComment: (content) => `"""\n${content}\n"""`,
383620383620 topCommentsRegex: /^(\s*(#[^\n]*\n|'''[\s\S]*?'''|"""[\s\S]*?""")\s*)+/
383621+ },
383622+ ".php": {
383623+ start: "/*",
383624+ end: "*/",
383625+ regex: /(<\?php\s*)?(\s*\/\*[\s\S]*?\*\/)/,
383626+ headerRegex: /@header\(([\s\S]*?)\)/,
383627+ createComment: (content) => `/*\n${content}\n*/`,
383628+ topCommentsRegex: /(<\?php\s*)?(\s*(\/\/[^\n]*\n|\/\*[\s\S]*?\*\/)\s*)+/
383621383629 }
383622383630 };
383623383631 /**
@@ -383635,12 +383643,20 @@ var FileHeaderManager = class {
383635383643 let stringChar = "";
383636383644 let escape = false;
383637383645 let inLineComment = false;
383646+ let inBlockComment = false;
383638383647 for (; index < text.length; index++) {
383639383648 const char = text[index];
383640383649 if (inLineComment) {
383641383650 if (char === "\n") inLineComment = false;
383642383651 continue;
383643383652 }
383653+ if (inBlockComment) {
383654+ if (char === "*" && text[index + 1] === "/") {
383655+ inBlockComment = false;
383656+ index++;
383657+ }
383658+ continue;
383659+ }
383644383660 if (inString) {
383645383661 if (escape) escape = false;
383646383662 else if (char === "\\") escape = true;
@@ -383650,6 +383666,9 @@ var FileHeaderManager = class {
383650383666 if (char === "/" && text[index + 1] === "/") {
383651383667 inLineComment = true;
383652383668 index++;
383669+ } else if (char === "/" && text[index + 1] === "*") {
383670+ inBlockComment = true;
383671+ index++;
383653383672 } else if (ext === ".py" && char === "#") inLineComment = true;
383654383673 else if (char === "\"" || char === "'") {
383655383674 inString = true;
@@ -383667,19 +383686,18 @@ var FileHeaderManager = class {
383667383686 return null;
383668383687 }
383669383688 /**
383670- * 解析JavaScript对象字面量(支持无引号属性名)
383689+ * 解析对象字符串
383671383690 * @param {string} str 对象字符串
383672383691 * @returns {Object} 解析后的对象
383673383692 */
383674383693 static parseObjectLiteral(str) {
383675- const normalized = str.replace(/([{,]\s*)([a-zA-Z_$][\w$]*)(\s*:)/g, "$1\"$2\"$3").replace(/'([^']+)'/g, "\"$1\"");
383676383694 try {
383677- return JSON .parse(normalized );
383695+ return JSON5 .parse(str );
383678383696 } catch (e) {
383679383697 try {
383680383698 return new Function(`return ${str}`)();
383681- } catch {
383682- throw new Error(`Invalid header object: ${str}`);
383699+ } catch (evalError) {
383700+ throw new Error(`Invalid header object: ${str}. Error: ${evalError.message} `);
383683383701 }
383684383702 }
383685383703 }
@@ -383753,34 +383771,51 @@ var FileHeaderManager = class {
383753383771 const ext = path.extname(filePath);
383754383772 const config = this.COMMENT_CONFIG[ext];
383755383773 if (!config) throw new Error(`Unsupported file type: ${ext}`);
383756- const headerStr = `@header(${JSON .stringify(headerObj, null, 2).replace(/"([a-zA-Z_$][\w$]*)":/g, "$1:").replace(/"/g, "'" )})`;
383774+ const headerStr = `@header(${JSON5 .stringify(headerObj, null, 2)})`;
383757383775 const match = content.match(config.regex);
383758383776 let newContent;
383759383777 if (match) {
383760383778 const [fullComment] = match;
383761383779 const commentStartIndex = content.indexOf(fullComment);
383762383780 const commentEndIndex = commentStartIndex + fullComment.length;
383763- if (content.substring(0, commentStartIndex).trim() !== "") newContent = config.createComment(headerStr) + "\n\n" + content;
383764- else {
383781+ const beforeComment = content.substring(0, commentStartIndex);
383782+ if (!(beforeComment.trim() === "" || ext === ".php" && beforeComment.trim() === "<?php")) {
383783+ let newComment = config.createComment(headerStr) + "\n\n";
383784+ if (ext === ".php") if (content.trim().startsWith("<?php")) newContent = content.replace(/<\?php\s*/, `<?php\n\n${newComment.trim()}\n\n`);
383785+ else newContent = `<?php\n\n${newComment.trim()}\n\n` + content;
383786+ else newContent = newComment + content;
383787+ } else {
383765383788 const headerBlock = this.findHeaderBlock(fullComment, ext);
383766383789 if (headerBlock) {
383767383790 const updatedComment = fullComment.substring(0, headerBlock.start) + headerStr + fullComment.substring(headerBlock.end);
383768383791 newContent = content.substring(0, commentStartIndex) + updatedComment + content.substring(commentEndIndex);
383769383792 } else {
383770- const updatedComment = fullComment.replace(config.end, "").trim() + `\n${headerStr}\n${config.end}`;
383793+ const trimmedComment = fullComment.trim();
383794+ let updatedComment;
383795+ if (ext === ".php" && trimmedComment.startsWith("<?php")) {
383796+ const endMarker = config.end;
383797+ const lastIndex = fullComment.lastIndexOf(endMarker);
383798+ if (lastIndex !== -1) updatedComment = fullComment.substring(0, lastIndex).trim() + `\n${headerStr}\n${endMarker}` + fullComment.substring(lastIndex + endMarker.length);
383799+ else updatedComment = fullComment + `\n${config.createComment(headerStr)}`;
383800+ } else updatedComment = fullComment.replace(config.end, "").trim() + `\n${headerStr}\n${config.end}`;
383771383801 newContent = content.substring(0, commentStartIndex) + updatedComment + content.substring(commentEndIndex);
383772383802 }
383773383803 }
383774- } else newContent = config.createComment(headerStr) + "\n\n" + content;
383804+ } else {
383805+ let newComment = config.createComment(headerStr) + "\n\n";
383806+ if (ext === ".php") if (content.trim().startsWith("<?php")) newContent = content.replace(/<\?php\s*/, `<?php\n\n${newComment.trim()}\n\n`);
383807+ else newContent = `<?php\n\n${newComment.trim()}\n\n` + content;
383808+ else newContent = newComment + content;
383809+ }
383775383810 if (!newContent.replace(config.regex, "").trim()) throw new Error("写入失败:内容不能只包含文件头而无原始内容");
383776383811 if (!newContent || newContent.trim().length === 0) throw new Error("Generated content is empty, operation aborted");
383777383812 const originalCodeLines = originalContent.split("\n").filter((line) => {
383778383813 const trimmed = line.trim();
383779- return trimmed && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*") && !trimmed.startsWith("*/") && !trimmed.startsWith("#") && !trimmed.startsWith("\"\"\"") && !trimmed.startsWith("'''");
383814+ return trimmed && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*") && !trimmed.startsWith("*/") && !trimmed.startsWith("#") && !trimmed.startsWith("\"\"\"") && !trimmed.startsWith("'''") && !trimmed.startsWith("<?php") ;
383780383815 });
383781383816 const newCodeLines = newContent.split("\n").filter((line) => {
383782383817 const trimmed = line.trim();
383783- return trimmed && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*") && !trimmed.startsWith("*/") && !trimmed.startsWith("#") && !trimmed.startsWith("\"\"\"") && !trimmed.startsWith("'''") && !trimmed.includes("@header(");
383818+ return trimmed && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*") && !trimmed.startsWith("*/") && !trimmed.startsWith("#") && !trimmed.startsWith("\"\"\"") && !trimmed.startsWith("'''") && !trimmed.startsWith("<?php") && !trimmed. includes("@header(");
383784383819 });
383785383820 if (originalCodeLines.length > 5 && newCodeLines.length < originalCodeLines.length * .8) throw new Error("Content integrity check failed: significant code loss detected, operation aborted");
383786383821 let backupPath = null;
@@ -383840,11 +383875,18 @@ var FileHeaderManager = class {
383840383875 let [fullComment] = match;
383841383876 const headerBlock = this.findHeaderBlock(fullComment, ext);
383842383877 if (headerBlock) {
383843- let cleanedInner = (fullComment.substring(0, headerBlock.start) + fullComment.substring(headerBlock.end)).replace(config.start, "").replace(config.end, "").split("\n").filter((line) => line.trim().length > 0).join("\n");
383844- if (!cleanedInner.trim()) content = content.replace(fullComment, "");
383878+ let beforeHeader = fullComment.substring(0, headerBlock.start);
383879+ let afterHeader = fullComment.substring(headerBlock.end);
383880+ beforeHeader = beforeHeader.replace(config.start, "");
383881+ afterHeader = afterHeader.replace(config.end, "");
383882+ if (ext === ".php") beforeHeader = beforeHeader.replace(/<\?php\s*/, "");
383883+ let cleanedInner = (beforeHeader + "\n" + afterHeader).split("\n").map((line) => line.trim()).filter((line) => line.length > 0).join("\n");
383884+ if (!cleanedInner) if (ext === ".php" && fullComment.trim().startsWith("<?php")) content = content.replace(fullComment, "<?php\n");
383885+ else content = content.replace(fullComment, "");
383845383886 else {
383846- const newComment = `${config.start}\n${cleanedInner}\n${config.end}`;
383847- content = content.replace(fullComment, newComment);
383887+ let newComment = `${config.start}\n${cleanedInner}\n${config.end}`;
383888+ if (ext === ".php" && fullComment.trim().startsWith("<?php")) newComment = `<?php\n\n${newComment}`;
383889+ content = content.replace(fullComment, () => newComment);
383848383890 }
383849383891 }
383850383892 return content.trim();
0 commit comments