๐ Enforce consistent JSON file reads before JSON.parse().
๐ผ๐ซ This rule is enabled in the โ
recommended config. This rule is disabled in the โ๏ธ unopinionated config.
๐ง This rule is automatically fixable by the --fix CLI option.
When reading and parsing a JSON file, consistently read it using the configured style. By default, the rule prefers reading it as a string.
The default keeps the code compatible with TypeScript, where JSON.parse() accepts a string.
The rule only checks and fixes direct no-option reads and option objects where encoding is the only property. Reads with additional options are left unchanged to avoid dropping options.
// โ
const packageJson = JSON.parse(await fs.readFile('./package.json'));
// โ
const promise = fs.readFile('./package.json');
const packageJson = JSON.parse(await promise);
// โ
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));// โ
const promise = fs.readFile('./package.json', {encoding: 'utf8', signal});
const packageJson = JSON.parse(await promise);Type: 'string' | 'buffer'
Default: 'string'
Prefer reading JSON files as buffers.
// eslint unicorn/consistent-json-file-read: ["error", "buffer"]
// โ
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));
// โ
const packageJson = JSON.parse(await fs.readFile('./package.json'));