Skip to content

Latest commit

Β 

History

History
44 lines (31 loc) Β· 1.44 KB

File metadata and controls

44 lines (31 loc) Β· 1.44 KB

prefer-json-parse-buffer

πŸ“ Prefer reading a JSON file as a buffer.

🚫 This rule is disabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

When reading and parsing a JSON file, it's unnecessary to read it as a string, because JSON.parse() can also parse Buffer.

Passing in a buffer may not be performant and is not compatible with TypeScript.

Examples

// ❌
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));

// ❌
const promise = fs.readFile('./package.json', {encoding: 'utf8'});
const packageJson = JSON.parse(await promise);

// βœ…
const packageJson = JSON.parse(await fs.readFile('./package.json'));
// βœ…
const promise = fs.readFile('./package.json', {encoding: 'utf8', signal});
const packageJson = JSON.parse(await promise);
// βœ…
const data = JSON.parse(await fs.readFile('./file.json', 'buffer'));
// βœ…
const data = JSON.parse(await fs.readFile('./file.json', 'gbk'));