|
| 1 | +/** |
| 2 | + * @author Toru Nagashima |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +"use strict" |
| 6 | + |
| 7 | +const { CALL, ReferenceTracker } = require("eslint-utils") |
| 8 | + |
| 9 | +const trackMap = { |
| 10 | + fs: { |
| 11 | + access: { [CALL]: true }, |
| 12 | + copyFile: { [CALL]: true }, |
| 13 | + open: { [CALL]: true }, |
| 14 | + rename: { [CALL]: true }, |
| 15 | + truncate: { [CALL]: true }, |
| 16 | + rmdir: { [CALL]: true }, |
| 17 | + mkdir: { [CALL]: true }, |
| 18 | + readdir: { [CALL]: true }, |
| 19 | + readlink: { [CALL]: true }, |
| 20 | + symlink: { [CALL]: true }, |
| 21 | + lstat: { [CALL]: true }, |
| 22 | + stat: { [CALL]: true }, |
| 23 | + link: { [CALL]: true }, |
| 24 | + unlink: { [CALL]: true }, |
| 25 | + chmod: { [CALL]: true }, |
| 26 | + lchmod: { [CALL]: true }, |
| 27 | + lchown: { [CALL]: true }, |
| 28 | + chown: { [CALL]: true }, |
| 29 | + utimes: { [CALL]: true }, |
| 30 | + realpath: { [CALL]: true }, |
| 31 | + mkdtemp: { [CALL]: true }, |
| 32 | + writeFile: { [CALL]: true }, |
| 33 | + appendFile: { [CALL]: true }, |
| 34 | + readFile: { [CALL]: true }, |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +module.exports = { |
| 39 | + meta: { |
| 40 | + docs: { |
| 41 | + description: 'enforce `require("fs").promises`', |
| 42 | + category: "Stylistic Issues", |
| 43 | + recommended: false, |
| 44 | + url: |
| 45 | + "https://github.com/mysticatea/eslint-plugin-node/blob/v8.0.1/docs/rules/prefer-promises/fs.md", |
| 46 | + }, |
| 47 | + fixable: null, |
| 48 | + messages: { |
| 49 | + preferPromises: "Use 'fs.promises.{{name}}()' instead.", |
| 50 | + }, |
| 51 | + schema: [], |
| 52 | + type: "suggestion", |
| 53 | + }, |
| 54 | + |
| 55 | + create(context) { |
| 56 | + return { |
| 57 | + "Program:exit"() { |
| 58 | + const scope = context.getScope() |
| 59 | + const tracker = new ReferenceTracker(scope, { mode: "legacy" }) |
| 60 | + const references = [ |
| 61 | + ...tracker.iterateCjsReferences(trackMap), |
| 62 | + ...tracker.iterateEsmReferences(trackMap), |
| 63 | + ] |
| 64 | + |
| 65 | + for (const { node, path } of references) { |
| 66 | + const name = path[path.length - 1] |
| 67 | + context.report({ |
| 68 | + node, |
| 69 | + messageId: "preferPromises", |
| 70 | + data: { name }, |
| 71 | + }) |
| 72 | + } |
| 73 | + }, |
| 74 | + } |
| 75 | + }, |
| 76 | +} |
0 commit comments