Skip to content

Commit

Permalink
feat(no-sync): Add ignores option (eslint-community#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood committed Oct 18, 2024
1 parent 8d8284d commit bb55a55
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/rules/no-sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This rule is aimed at preventing synchronous methods from being called in Node.j

### Options

#### allowAtRootLevel

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:
Expand Down Expand Up @@ -56,6 +58,26 @@ Examples of **correct** code for this rule with the `{ allowAtRootLevel: true }`
fs.readFileSync(somePath).toString();
```

#### ignores

You can `ignore` specific function names using this option.

Examples of **incorrect** code for this rule with the `{ ignores: ['readFileSync'] }` option:

```js
/*eslint n/no-sync: ["error", { ignores: ['readFileSync'] }] */

fs.readdirSync(somePath);
```

Examples of **correct** code for this rule with the `{ ignores: ['readFileSync'] }` option:

```js
/*eslint n/no-sync: ["error", { ignores: ['readFileSync'] }] */

fs.readFileSync(somePath);
```

## 🔎 Implementation

- [Rule source](../../lib/rules/no-sync.js)
Expand Down
10 changes: 10 additions & 0 deletions lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ module.exports = {
type: "boolean",
default: false,
},
ignores: {
type: "array",
items: { type: "string" },
default: [],
},
},
additionalProperties: false,
},
Expand All @@ -41,6 +46,7 @@ module.exports = {

create(context) {
const options = context.options[0] ?? {}
const ignores = options.ignores ?? []

const selector = options.allowAtRootLevel
? selectors.map(selector => `:function ${selector}`)
Expand All @@ -51,6 +57,10 @@ module.exports = {
* @returns {void}
*/
[selector.join(",")](node) {
if (ignores.includes(node.name)) {
return
}

context.report({
node: node.parent,
messageId: "noSync",
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ new RuleTester().run("no-sync", rule, {
code: "if (true) {fooSync();}",
options: [{ allowAtRootLevel: true }],
},
// ignores
{
code: "fooSync();",
options: [{ ignores: ["fooSync"] }],
},
],
invalid: [
{
Expand Down Expand Up @@ -117,5 +122,22 @@ new RuleTester().run("no-sync", rule, {
},
],
},
// ignores
{
code: "() => {fs.fooSync();}",
options: [
{
allowAtRootLevel: true,
ignores: ["barSync"],
},
],
errors: [
{
messageId: "noSync",
data: { propertyName: "fooSync" },
type: "MemberExpression",
},
],
},
],
})

0 comments on commit bb55a55

Please sign in to comment.