forked from eslint-community/eslint-plugin-n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-sync.js
74 lines (69 loc) · 2.1 KB
/
no-sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @author Matt DuVall<http://mattduvall.com/>
* See LICENSE file in root directory for full license.
*/
"use strict"
const selectors = [
// fs.readFileSync()
// readFileSync.call(null, 'path')
"CallExpression > MemberExpression.callee Identifier[name=/Sync$/]",
// readFileSync()
"CallExpression > Identifier[name=/Sync$/]",
]
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow synchronous methods",
recommended: false,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/no-sync.md",
},
fixable: null,
schema: [
{
type: "object",
properties: {
allowAtRootLevel: {
type: "boolean",
default: false,
},
ignores: {
type: "array",
items: { type: "string" },
default: [],
},
},
additionalProperties: false,
},
],
messages: {
noSync: "Unexpected sync method: '{{propertyName}}'.",
},
},
create(context) {
const options = context.options[0] ?? {}
const ignores = options.ignores ?? []
const selector = options.allowAtRootLevel
? selectors.map(selector => `:function ${selector}`)
: selectors
return {
/**
* @param {import('estree').Identifier & {parent: import('estree').Node}} node
* @returns {void}
*/
[selector.join(",")](node) {
if (ignores.includes(node.name)) {
return
}
context.report({
node: node.parent,
messageId: "noSync",
data: {
propertyName: node.name,
},
})
},
}
},
}