fix(jsonc): strip BOM before parsing (#3164)

This commit is contained in:
YeonGyu-Kim
2026-04-07 15:31:10 +09:00
parent e8c8376db4
commit b77c256943
2 changed files with 53 additions and 14 deletions
+6 -2
View File
@@ -9,12 +9,16 @@ export interface JsoncParseResult<T> {
errors: Array<{ message: string; offset: number; length: number }>
}
function stripBom(content: string): string {
return content.charCodeAt(0) === 0xfeff ? content.slice(1) : content
}
export function parseJsonc<T = unknown>(content: string): T {
// Strip UTF-8 BOM if present (Windows UTF-8 with BOM files)
content = content.replace(/^\uFEFF/, "")
const errors: ParseError[] = []
const result = parse(content, errors, {
const result = parse(stripBom(content), errors, {
allowTrailingComma: true,
disallowComments: false,
}) as T
@@ -31,7 +35,7 @@ export function parseJsonc<T = unknown>(content: string): T {
export function parseJsoncSafe<T = unknown>(content: string): JsoncParseResult<T> {
const errors: ParseError[] = []
const data = parse(content, errors, {
const data = parse(stripBom(content), errors, {
allowTrailingComma: true,
disallowComments: false,
}) as T | null