check-existence.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const exists = require("./exists")
  7. const getAllowModules = require("./get-allow-modules")
  8. /**
  9. * Checks whether or not each requirement target exists.
  10. *
  11. * It looks up the target according to the logic of Node.js.
  12. * See Also: https://nodejs.org/api/modules.html
  13. *
  14. * @param {RuleContext} context - A context to report.
  15. * @param {ImportTarget[]} targets - A list of target information to check.
  16. * @returns {void}
  17. */
  18. module.exports = function checkForExistence(context, targets) {
  19. const allowed = new Set(getAllowModules(context))
  20. for (const target of targets) {
  21. const missingModule =
  22. target.moduleName != null &&
  23. !allowed.has(target.moduleName) &&
  24. target.filePath == null
  25. const missingFile =
  26. target.moduleName == null && !exists(target.filePath)
  27. if (missingModule || missingFile) {
  28. context.report({
  29. node: target.node,
  30. loc: target.node.loc,
  31. message: '"{{name}}" is not found.',
  32. data: target,
  33. })
  34. }
  35. }
  36. }