check-existence.js 1.7 KB

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