sindresorhus/eslint-plugin-unicorn

Re-enable `prefer-json-parse-buffer`

Open

#2,565 opened on Feb 19, 2025

View on GitHub
 (1 comment) (1 reaction) (0 assignees)JavaScript (5,022 stars) (468 forks)user submission
change requesthelp wanted

Description

Description

This rule actually makes sense to JavaScript users, but it's disabled due to compatibility with TypeScript, how about rename to consistent-json-file-read, and default to read as string, but add option to allow read it as buffer?

Examples

// ❌
JOSN.parse(await fs.readFile('/path/to/file.json'));

// ✅
JOSN.parse(await fs.readFile('/path/to/file.json', 'utf8'));
// ❌
// eslint unicorn/consistent-json-file-read: ["error", "buffer"]
JOSN.parse(await fs.readFile('/path/to/file.json', 'utf8'));

// ✅
// eslint unicorn/consistent-json-file-read: ["error", "buffer"]
JOSN.parse(await fs.readFile('/path/to/file.json'));

Additional Info

We may want check other cases in the future, since there are more way to read a JSON file now.

createRequire(import.meta.url)('/path/to/file.json');
await import('./path/to/file.json', {with:{type:'json'}});

Contributor guide

Re-enable `prefer-json-parse-buffer` · sindresorhus/eslint-plugin-unicorn#2565 | Good First Issue