Add linter for sync function calls
Summary: That's another thing I comment on a lot and is a mostly avoidable source of bad perf. Reviewed By: mweststrate Differential Revision: D30450577 fbshipit-source-id: bb82d8cbd34956fa790243f59cda09ff9c4e7379
This commit is contained in:
committed by
Facebook GitHub Bot
parent
c4ccdc8d9f
commit
e5404d2af3
@@ -100,6 +100,7 @@ module.exports = {
|
|||||||
'import/no-unresolved': [2, {commonjs: true, amd: true}],
|
'import/no-unresolved': [2, {commonjs: true, amd: true}],
|
||||||
'node/no-extraneous-import': [2, {allowModules: builtInModules}],
|
'node/no-extraneous-import': [2, {allowModules: builtInModules}],
|
||||||
'node/no-extraneous-require': [2, {allowModules: builtInModules}],
|
'node/no-extraneous-require': [2, {allowModules: builtInModules}],
|
||||||
|
'node/no-sync': [1],
|
||||||
'flipper/no-relative-imports-across-packages': [2],
|
'flipper/no-relative-imports-across-packages': [2],
|
||||||
'flipper/no-electron-remote-imports': [1],
|
'flipper/no-electron-remote-imports': [1],
|
||||||
'flipper/no-console-error-without-context': [1],
|
'flipper/no-console-error-without-context': [1],
|
||||||
|
|||||||
@@ -192,3 +192,43 @@ const portforwardingClient = getStaticPath(
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `node/no-sync`
|
||||||
|
|
||||||
|
- **Summary**: Use asynchronous methods whereever possible. [More details.](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-sync.md)
|
||||||
|
- **Why**: Synchronous method calls block the event loop. Even innocous calls like `fs.existsSync()` can cause
|
||||||
|
frame drops for users or even long stalls.
|
||||||
|
- **How to fix it**: We have `fs-extra` as a dependency, which provides Promise-based alternatives for all `fs` functions.
|
||||||
|
Most often, replacing a sync call with an async call and adding an `await` is all that's needed.
|
||||||
|
|
||||||
|
*Before*
|
||||||
|
```js
|
||||||
|
import fs from 'fs';
|
||||||
|
function ensureCertsExist() {
|
||||||
|
if (
|
||||||
|
!(
|
||||||
|
fs.existsSync(serverKey) &&
|
||||||
|
fs.existsSync(serverCert) &&
|
||||||
|
fs.existsSync(caCert)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return generateServerCertificate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*After*
|
||||||
|
|
||||||
|
```js
|
||||||
|
import fsExtra from 'fs-extra';
|
||||||
|
async function ensureCertsExist() {
|
||||||
|
const allExist = Promise.all([
|
||||||
|
fsExtra.exists(serverKey),
|
||||||
|
fsExtra.exists(serverCert),
|
||||||
|
fsExtra.exists(caCert),
|
||||||
|
]).then((exist) => exist.every(Boolean));
|
||||||
|
if (!allExist) {
|
||||||
|
return this.generateServerCertificate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user