π 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.
// β
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'));