forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
fs.js
82 lines (76 loc) · 2.65 KB
/
fs.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
75
76
77
78
79
80
81
82
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"
const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils")
/** @type {import('@eslint-community/eslint-utils').TraceMap<boolean>} */
const traceMap = {
fs: {
access: { [CALL]: true },
copyFile: { [CALL]: true },
open: { [CALL]: true },
rename: { [CALL]: true },
truncate: { [CALL]: true },
rmdir: { [CALL]: true },
mkdir: { [CALL]: true },
readdir: { [CALL]: true },
readlink: { [CALL]: true },
symlink: { [CALL]: true },
lstat: { [CALL]: true },
stat: { [CALL]: true },
link: { [CALL]: true },
unlink: { [CALL]: true },
chmod: { [CALL]: true },
lchmod: { [CALL]: true },
lchown: { [CALL]: true },
chown: { [CALL]: true },
utimes: { [CALL]: true },
realpath: { [CALL]: true },
mkdtemp: { [CALL]: true },
writeFile: { [CALL]: true },
appendFile: { [CALL]: true },
readFile: { [CALL]: true },
},
}
traceMap["node:fs"] = traceMap.fs
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description: 'enforce `require("fs").promises`',
recommended: false,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/prefer-promises/fs.md",
},
fixable: null,
messages: {
preferPromises: "Use 'fs.promises.{{name}}()' instead.",
},
schema: [],
type: "suggestion",
},
create(context) {
return {
"Program:exit"(node) {
const sourceCode = context.sourceCode ?? context.getSourceCode() // TODO: just use context.sourceCode when dropping eslint < v9
const scope = sourceCode.getScope?.(node) ?? context.getScope() //TODO: remove context.getScope() when dropping support for ESLint < v9
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(traceMap),
...tracker.iterateEsmReferences(traceMap),
]
for (const { node, path } of references) {
const name = path.at(-1)
if (name == null) {
continue
}
context.report({
node,
messageId: "preferPromises",
data: { name },
})
}
},
}
},
}