sindresorhus/eslint-plugin-unicorn

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

已关闭

#2,565 创建于 2025年2月19日

 (1 条评论) (1 个反应) (0 位负责人)JavaScript (468 个派生)user submission
change requesthelp wanted

仓库指标

星标
 (5,022 个星标)
PR 合并指标
 (平均合并 1天 16小时) (30 天内合并 399 个 PR)

描述

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'}});

贡献者指南