()
| 316 | } |
| 317 | |
| 318 | export default function createJsoncLoader(): ILoader< |
| 319 | string, |
| 320 | Record<string, any> |
| 321 | > { |
| 322 | return createLoader({ |
| 323 | pull: async (locale, input) => { |
| 324 | const jsoncString = input || "{}"; |
| 325 | const errors: ParseError[] = []; |
| 326 | const result = parse(jsoncString, errors, { |
| 327 | allowTrailingComma: true, |
| 328 | disallowComments: false, |
| 329 | allowEmptyContent: true, |
| 330 | }); |
| 331 | |
| 332 | if (errors.length > 0) { |
| 333 | throw new Error(`Failed to parse JSONC: ${errors[0].error}`); |
| 334 | } |
| 335 | |
| 336 | return result || {}; |
| 337 | }, |
| 338 | push: async (locale, data) => { |
| 339 | // JSONC parser's stringify preserves formatting but doesn't add comments |
| 340 | // We'll use standard JSON.stringify with pretty formatting for output |
| 341 | const serializedData = JSON.stringify(data, null, 2); |
| 342 | return serializedData; |
| 343 | }, |
| 344 | pullHints: async (input) => { |
| 345 | if (!input || typeof input !== "string") { |
| 346 | return {}; |
| 347 | } |
| 348 | |
| 349 | try { |
| 350 | return extractCommentsFromJsonc(input); |
| 351 | } catch (error) { |
| 352 | console.warn("Failed to extract comments from JSONC:", error); |
| 353 | return {}; |
| 354 | } |
| 355 | }, |
| 356 | }); |
| 357 | } |
no test coverage detected