Some checks failed
Node.js CI - Dockge / ${{ matrix.os }} (22, ubuntu-latest) (push) Successful in 1m47s
json-yaml-validate / json-yaml-validate (push) Successful in 12s
Nightly Release / release-nightly (push) Failing after 44s
Node.js CI - Dockge / ${{ matrix.os }} (22, macos-latest) (push) Has been cancelled
Node.js CI - Dockge / ${{ matrix.os }} (22, windows-latest) (push) Has been cancelled
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
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<string>();
|
|
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;
|
|
}
|