Skip to content

Commit fd84a33

Browse files
scagoodaladdin-add
authored andcommitted
feat(no-sync): Add ignores option (eslint-community#354)
1 parent 74269ec commit fd84a33

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

docs/rules/no-sync.md

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This rule is aimed at preventing synchronous methods from being called in Node.j
1010

1111
### Options
1212

13+
#### allowAtRootLevel
14+
1315
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`.
1416

1517
Examples of **incorrect** code for this rule with the default `{ allowAtRootLevel: false }` option:
@@ -56,6 +58,26 @@ Examples of **correct** code for this rule with the `{ allowAtRootLevel: true }`
5658
fs.readFileSync(somePath).toString();
5759
```
5860

61+
#### ignores
62+
63+
You can `ignore` specific function names using this option.
64+
65+
Examples of **incorrect** code for this rule with the `{ ignores: ['readFileSync'] }` option:
66+
67+
```js
68+
/*eslint n/no-sync: ["error", { ignores: ['readFileSync'] }] */
69+
70+
fs.readdirSync(somePath);
71+
```
72+
73+
Examples of **correct** code for this rule with the `{ ignores: ['readFileSync'] }` option:
74+
75+
```js
76+
/*eslint n/no-sync: ["error", { ignores: ['readFileSync'] }] */
77+
78+
fs.readFileSync(somePath);
79+
```
80+
5981
## 🔎 Implementation
6082

6183
- [Rule source](../../lib/rules/no-sync.js)

lib/rules/no-sync.js

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ module.exports = {
3030
type: "boolean",
3131
default: false,
3232
},
33+
ignores: {
34+
type: "array",
35+
items: { type: "string" },
36+
default: [],
37+
},
3338
},
3439
additionalProperties: false,
3540
},
@@ -41,6 +46,7 @@ module.exports = {
4146

4247
create(context) {
4348
const options = context.options[0] ?? {}
49+
const ignores = options.ignores ?? []
4450

4551
const selector = options.allowAtRootLevel
4652
? selectors.map(selector => `:function ${selector}`)
@@ -51,6 +57,10 @@ module.exports = {
5157
* @returns {void}
5258
*/
5359
[selector.join(",")](node) {
60+
if (ignores.includes(node.name)) {
61+
return
62+
}
63+
5464
context.report({
5565
node: node.parent,
5666
messageId: "noSync",

tests/lib/rules/no-sync.js

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ new RuleTester().run("no-sync", rule, {
2222
code: "if (true) {fooSync();}",
2323
options: [{ allowAtRootLevel: true }],
2424
},
25+
// ignores
26+
{
27+
code: "fooSync();",
28+
options: [{ ignores: ["fooSync"] }],
29+
},
2530
],
2631
invalid: [
2732
{
@@ -117,5 +122,22 @@ new RuleTester().run("no-sync", rule, {
117122
},
118123
],
119124
},
125+
// ignores
126+
{
127+
code: "() => {fs.fooSync();}",
128+
options: [
129+
{
130+
allowAtRootLevel: true,
131+
ignores: ["barSync"],
132+
},
133+
],
134+
errors: [
135+
{
136+
messageId: "noSync",
137+
data: { propertyName: "fooSync" },
138+
type: "MemberExpression",
139+
},
140+
],
141+
},
120142
],
121143
})

0 commit comments

Comments
 (0)