question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `prefer-json-parse-buffer`

See original GitHub issue

When parsing a json file, there is no need to read it as string, because JSON.parse can parse Buffer. Real world case

Fail

JSON.parse(await fs.readFile(file, 'utf8'));

Pass

JSON.parse(await fs.readFile(file));
JSON.parse(await fs.readFile(file, 'gbk'));

This is also faster when use fsPromises, but this is bug, should not be the main reason for this rule. https://github.com/nodejs/node/issues/37583

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

11reactions
crystalfpcommented, Jan 11, 2022

Sorry to disagree, but typescript JSON.parse() does not accept anything but a string. See microsoft/TypeScript #44944. The only solution for me is to disable this rule.

5reactions
fiskercommented, Mar 17, 2022

This rule is still usefully to me, but I agree the reason behind this rule is arguable. Maybe we should disable this rule in recommend?

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-config-ash-nazg - npm
ash-nazg aims to offer defaults which adhere to norms, build upon productive restrictions, and in a few cases, offer greater latitude where some ......
Read more >
sindresorhus/eslint-plugin-unicorn (Raised $2856.00)
Rule proposal : Expiring TODO comments. Rewarded#238created bysindresorhus. 1 pull request. $100.00. Rule proposal: No default parameter options.
Read more >
Comparing main...next · prettier/prettier · GitHub
Note: This method does **not** offer a way to _extend_ the configuration to overwrite some properties from the shared configuration. If you need...
Read more >
JavaScript/TypeScript linter (ESLint wrapper) with great defaults
Uses ESLint underneath, so issues regarding rules should be opened over there. JSX is supported by default, but you'll need eslint-config-xo-react for React ......
Read more >
Yet another GitHub proxy running on Cloudflare Workers
template-indent : Set the rule error instead of warn in recommended preset (#​1781) 09923af; prefer-json-parse-buffer : Remove from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found