env-sentinel
Version:
Zero-dependency tool that auto-validates .env files against schema.env, with optional fallback and secure warnings.
38 lines (37 loc) • 1.39 kB
JavaScript
const VALID_NAME_REGEX = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
export function noInvalidReferenceSyntaxCheck(lineNumber, lineContent) {
const regex = /(?<!\\)(\$\{([^}]*)}|\$(?!\$)[a-zA-Z_][a-zA-Z0-9_]*\b)/g;
let match;
while ((match = regex.exec(lineContent)) !== null) {
const [fullMatch, , inner] = match;
if (fullMatch.startsWith('${') && (inner === undefined || inner === '')) {
return {
line: lineNumber,
content: lineContent,
severity: 'error',
issue: 'Empty variable reference `${}` is invalid',
};
}
const content = fullMatch.startsWith('${') ? inner : fullMatch.slice(1);
const varName = content.split(/[:=?#%]/)[0];
if (!varName)
continue;
if (/^\d/.test(varName)) {
return {
line: lineNumber,
content: lineContent,
severity: 'error',
issue: `Invalid variable name: must not start with a digit → "${varName}"`,
};
}
if (!VALID_NAME_REGEX.test(varName)) {
return {
line: lineNumber,
content: lineContent,
severity: 'error',
issue: `Invalid character in variable name: "${varName}"`,
};
}
}
return undefined;
}