From 3e93aab9a740e79c2f4a8108a1a1e1443a0b049c Mon Sep 17 00:00:00 2001 From: kietdev27 <3122410202@sv.sgu.edu.vn> Date: Thu, 21 May 2026 09:48:23 +0700 Subject: [PATCH] fix(config): validate languages from canonical list --- __tests__/foundation.test.ts | 22 +++++++++++++++++++++- src/config.ts | 22 +++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/__tests__/foundation.test.ts b/__tests__/foundation.test.ts index 4e8f204a..c13f75f2 100644 --- a/__tests__/foundation.test.ts +++ b/__tests__/foundation.test.ts @@ -10,7 +10,7 @@ import * as path from 'path'; import * as os from 'os'; import { CodeGraph } from '../src'; import { DEFAULT_CONFIG, Node, Edge } from '../src/types'; -import { loadConfig, saveConfig } from '../src/config'; +import { loadConfig, saveConfig, validateConfig } from '../src/config'; import { isInitialized, getCodeGraphDir, validateDirectory } from '../src/directory'; import { DatabaseConnection, getDatabasePath } from '../src/db'; @@ -205,6 +205,26 @@ describe('CodeGraph Foundation', () => { const config = loadConfig(tempDir); expect(config.maxFileSize).toBe(999999); }); + + it('should accept every supported language in configuration', () => { + const config = { + ...DEFAULT_CONFIG, + rootDir: tempDir, + languages: ['php', 'tsx', 'vue', 'liquid', 'pascal', 'scala'], + }; + + expect(validateConfig(config)).toBe(true); + }); + + it('should reject unknown languages in configuration', () => { + const config = { + ...DEFAULT_CONFIG, + rootDir: tempDir, + languages: ['typescript', 'not-a-language'], + }; + + expect(validateConfig(config)).toBe(false); + }); }); describe('Directory Management', () => { diff --git a/src/config.ts b/src/config.ts index 9ab1032a..b7b95858 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,7 +7,12 @@ import * as fs from 'fs'; import * as path from 'path'; import picomatch from 'picomatch'; -import { CodeGraphConfig, DEFAULT_CONFIG, Language, NodeKind } from './types'; +import { + CodeGraphConfig, + DEFAULT_CONFIG, + LANGUAGES, + NodeKind, +} from './types'; import { normalizePath } from './utils'; /** @@ -72,18 +77,9 @@ export function validateConfig(config: unknown): config is CodeGraphConfig { if (!c.include.every((p) => typeof p === 'string')) return false; if (!c.exclude.every((p) => typeof p === 'string')) return false; - // Validate languages - const validLanguages: Language[] = [ - 'typescript', - 'javascript', - 'python', - 'go', - 'rust', - 'java', - 'svelte', - 'unknown', - ]; - if (!c.languages.every((l) => validLanguages.includes(l as Language))) return false; + // Validate languages against the canonical list in types.ts. + const validLanguages: ReadonlySet = new Set(LANGUAGES); + if (!c.languages.every((l) => typeof l === 'string' && validLanguages.has(l))) return false; // Validate frameworks for (const fw of c.frameworks) {