export interface EnvIssue { from: number; to: number; severity: "error" | "warning"; message: string; } /** Find malformed quoted values and repeated keys without reporting secret values. */ export function analyzeEnv(source: string): EnvIssue[] { const issues: EnvIssue[] = []; const keys = new Set(); let quote = ""; let quoteStart = 0; let offset = 0; for (const line of source.split("\n")) { let index = 0; if (!quote) { const assignment = /^(?:\uFEFF)?\s*(?:export\s+)?([^\s=:#]+)\s*(?:=|:\s+)/.exec(line); if (assignment && !line.trimStart().startsWith("#")) { if (keys.has(assignment[1])) { issues.push({ from: offset, to: offset + line.length, severity: "warning", message: "editorEnvDuplicate", }); } keys.add(assignment[1]); index = assignment[0].length; while (/\s/.test(line[index] || "") && index < line.length) { index++; } if (line[index] === "'" || line[index] === "\"") { quote = line[index]; quoteStart = offset + index; index++; } } } if (quote) { for (; index < line.length; index++) { if (line[index] === "\\") { index++; } else if (line[index] === quote) { quote = ""; break; } } } offset += line.length + 1; } if (quote) { issues.push({ from: quoteStart, to: Math.min(source.length, quoteStart + 1), severity: "error", message: "editorEnvUnclosedQuote", }); } const nul = source.indexOf("\0"); if (nul >= 0) { issues.push({ from: nul, to: nul + 1, severity: "error", message: "editorEnvNull", }); } return issues; }