fs.js 2.2 KB

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