From 35215b2898874c31295f97373939a0db980ad055 Mon Sep 17 00:00:00 2001 From: Chrilleweb Date: Tue, 31 Mar 2026 21:20:53 +0200 Subject: [PATCH] chore: minor refactor --- packages/cli/src/config/types.ts | 4 +++- packages/cli/src/core/scan/patterns.ts | 16 +++++++++------- packages/cli/src/core/scan/scanFile.ts | 21 ++++++++------------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/config/types.ts b/packages/cli/src/config/types.ts index bcc77c8..17241cf 100644 --- a/packages/cli/src/config/types.ts +++ b/packages/cli/src/config/types.ts @@ -137,6 +137,8 @@ export interface Options { inconsistentNamingWarnings: boolean; } +export type EnvPatternName = 'process.env' | 'import.meta.env' | 'sveltekit'; + /** * Represents a single usage of an environment variable in the codebase. */ @@ -150,7 +152,7 @@ export interface EnvUsage { /** The column number where the usage was detected */ column: number; /** The pattern used to access the environment variable */ - pattern: 'process.env' | 'import.meta.env' | 'sveltekit'; + pattern: EnvPatternName; /** List of imported env modules (for sveltekit) */ imports?: string[]; /** The actual line content where the usage was detected */ diff --git a/packages/cli/src/core/scan/patterns.ts b/packages/cli/src/core/scan/patterns.ts index 0fb8fe7..314e25b 100644 --- a/packages/cli/src/core/scan/patterns.ts +++ b/packages/cli/src/core/scan/patterns.ts @@ -1,7 +1,9 @@ +import type { EnvPatternName } from '../../config/types.js'; + type Pattern = { - name: 'process.env' | 'import.meta.env' | 'sveltekit'; + name: EnvPatternName; regex: RegExp; - processor?: (match: RegExpExecArray) => string[]; + processor?: (match: RegExpMatchArray) => string[]; }; /** @@ -82,7 +84,7 @@ export const ENV_PATTERNS: Pattern[] = [ // import { SECRET } from '$env/static/private'; // import { PUBLIC_URL } from '$env/static/public'; { - name: 'sveltekit' as const, + name: 'sveltekit', regex: /import\s*\{\s*([A-Z_][A-Z0-9_]*)\s*\}\s*from\s*['"]\$env\/static\/(?:private|public)['"]/g, }, @@ -90,14 +92,14 @@ export const ENV_PATTERNS: Pattern[] = [ // SvelteKit dynamic env object // env.SECRET (Only matches .env variables accessed via env.VAR syntax) { - name: 'sveltekit' as const, + name: 'sveltekit', regex: /(? { const content = match[1]; @@ -121,7 +123,7 @@ export const ENV_PATTERNS: Pattern[] = [ // named import from dynamic is invalid in SvelteKit // import { env } from '$env/dynamic/private'; { - name: 'sveltekit' as const, + name: 'sveltekit', regex: /import\s*\{\s*([A-Z_][A-Z0-9_]*)\s*\}\s*from\s*['"]\$env\/dynamic\/(?:private|public)['"]/g, }, @@ -129,7 +131,7 @@ export const ENV_PATTERNS: Pattern[] = [ // default import from any $env module is invalid in SvelteKit // import SECRET from '$env/...'; { - name: 'sveltekit' as const, + name: 'sveltekit', regex: /import\s+([A-Z_][A-Z0-9_]*)\s+from\s+['"]\$env\/(?:static|dynamic)\/(?:private|public)['"]/g, }, diff --git a/packages/cli/src/core/scan/scanFile.ts b/packages/cli/src/core/scan/scanFile.ts index b6ef9a5..3c70170 100644 --- a/packages/cli/src/core/scan/scanFile.ts +++ b/packages/cli/src/core/scan/scanFile.ts @@ -24,36 +24,31 @@ export function scanFile( const relativePath = normalizePath(path.relative(opts.cwd, filePath)); // Collect all $env imports used in this file - const envImports: string[] = []; - const importRegex = /import\s+(?:\{[^}]*\}|\w+)\s+from\s+['"](\$env\/(?:static|dynamic)\/(?:private|public))['"]/g; - let importMatch: RegExpExecArray | null; - - while ((importMatch = importRegex.exec(content)) !== null) { - envImports.push(importMatch[1]!); - } + const envImports = [...content.matchAll(importRegex)] + .map((m) => m[1]) + .filter((m): m is string => m != null); for (const pattern of ENV_PATTERNS) { - let match: RegExpExecArray | null; const regex = new RegExp(pattern.regex.source, pattern.regex.flags); - while ((match = regex.exec(content)) !== null) { + for (const match of content.matchAll(regex)) { const variables = pattern.processor ? pattern.processor(match) - : [match[1]!]; + : [match[1] ?? '']; for (const variable of variables) { if (!variable) continue; - const matchIndex = match.index; + const matchIndex = match.index!; // Find line and column // Note: For destructured variables, this points to the start of the destructuring block // not the specific variable location. Ideally we'd search within the match. const beforeMatch = content.substring(0, matchIndex); - const lineNumber = beforeMatch.split('\n').length; + const lineNumber = (beforeMatch.match(/\n/g)?.length ?? 0) + 1; const lastNewlineIndex = beforeMatch.lastIndexOf('\n'); const column = lastNewlineIndex === -1 @@ -61,7 +56,7 @@ export function scanFile( : matchIndex - lastNewlineIndex; // Get the context (the actual line) - const contextLine = lines[lineNumber - 1]!; + const contextLine = lines[lineNumber - 1] ?? ''; // Ignore likely minified / bundled lines to avoid scan false positives if (isLikelyMinified(contextLine)) continue;