dns.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { CALL, CONSTRUCT, ReferenceTracker } = require("eslint-utils")
  7. const trackMap = {
  8. dns: {
  9. lookup: { [CALL]: true },
  10. lookupService: { [CALL]: true },
  11. Resolver: { [CONSTRUCT]: true },
  12. getServers: { [CALL]: true },
  13. resolve: { [CALL]: true },
  14. resolve4: { [CALL]: true },
  15. resolve6: { [CALL]: true },
  16. resolveAny: { [CALL]: true },
  17. resolveCname: { [CALL]: true },
  18. resolveMx: { [CALL]: true },
  19. resolveNaptr: { [CALL]: true },
  20. resolveNs: { [CALL]: true },
  21. resolvePtr: { [CALL]: true },
  22. resolveSoa: { [CALL]: true },
  23. resolveSrv: { [CALL]: true },
  24. resolveTxt: { [CALL]: true },
  25. reverse: { [CALL]: true },
  26. setServers: { [CALL]: true },
  27. },
  28. }
  29. module.exports = {
  30. meta: {
  31. docs: {
  32. description: 'enforce `require("dns").promises`',
  33. category: "Stylistic Issues",
  34. recommended: false,
  35. url:
  36. "https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/prefer-promises/dns.md",
  37. },
  38. fixable: null,
  39. messages: {
  40. preferPromises: "Use 'dns.promises.{{name}}()' instead.",
  41. preferPromisesNew: "Use 'new dns.promises.{{name}}()' instead.",
  42. },
  43. schema: [],
  44. type: "suggestion",
  45. },
  46. create(context) {
  47. return {
  48. "Program:exit"() {
  49. const scope = context.getScope()
  50. const tracker = new ReferenceTracker(scope, { mode: "legacy" })
  51. const references = [
  52. ...tracker.iterateCjsReferences(trackMap),
  53. ...tracker.iterateEsmReferences(trackMap),
  54. ]
  55. for (const { node, path } of references) {
  56. const name = path[path.length - 1]
  57. const isClass = name[0] === name[0].toUpperCase()
  58. context.report({
  59. node,
  60. messageId: isClass
  61. ? "preferPromisesNew"
  62. : "preferPromises",
  63. data: { name },
  64. })
  65. }
  66. },
  67. }
  68. },
  69. }