Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: add description of no-sync allowAtRootLevel option (fixes #8902) #8906

Merged
merged 1 commit into from
Jul 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 30 additions & 4 deletions docs/rules/no-sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ In Node.js, most I/O is done through asynchronous methods. However, there are of

This rule is aimed at preventing synchronous methods from being called in Node.js. It looks specifically for the method suffix "`Sync`" (as is the convention with Node.js operations).

Examples of **incorrect** code for this rule:
## Options

This rule has an optional object option `{ allowAtRootLevel: <boolean> }`, which determines whether synchronous methods should be allowed at the top level of a file, outside of any functions. This option defaults to `false`.

Examples of **incorrect** code for this rule with the default `{ allowAtRootLevel: false }` option:

```js
/*eslint no-sync: "error"*/

fs.existsSync(somePath);

var contents = fs.readFileSync(somePath).toString();
function foo() {
var contents = fs.readFileSync(somePath).toString();
}
```

Examples of **correct** code for this rule:
Examples of **correct** code for this rule with the default `{ allowAtRootLevel: false }` option:

```js
/*eslint no-sync: "error"*/
Expand All @@ -28,6 +34,26 @@ async(function() {
});
```

Examples of **incorrect** code for this rule with the `{ allowAtRootLevel: true }` option

```js
/*eslint no-sync: ["error", { allowAtRootLevel: true }]*/

function foo() {
var contents = fs.readFileSync(somePath).toString();
}

var bar = baz => fs.readFileSync(qux);
```

Examples of **correct** code for this rule with the `{ allowAtRootLevel: true }` option

```js
/*eslint no-sync: ["error", { allowAtRootLevel: true }]*/

fs.readFileSync(somePath).toString();
```

## When Not To Use It

If you want to allow synchronous operations in your script.
If you want to allow synchronous operations in your script, do not enable this rule.