no-missing-require.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2015 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. "use strict"
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const checkExistence = require("../util/check-existence")
  11. const getAllowModules = require("../util/get-allow-modules")
  12. const getRequireTargets = require("../util/get-require-targets")
  13. const getResolvePaths = require("../util/get-resolve-paths")
  14. const getTryExtensions = require("../util/get-try-extensions")
  15. //------------------------------------------------------------------------------
  16. // Helpers
  17. //------------------------------------------------------------------------------
  18. /**
  19. * The definition of this rule.
  20. *
  21. * @param {RuleContext} context - The rule context to check.
  22. * @returns {object} The definition of this rule.
  23. */
  24. function create(context) {
  25. const filePath = context.getFilename()
  26. if (filePath === "<input>") {
  27. return {}
  28. }
  29. return {
  30. "Program:exit"() {
  31. checkExistence(
  32. context,
  33. getRequireTargets(context)
  34. )
  35. },
  36. }
  37. }
  38. //------------------------------------------------------------------------------
  39. // Rule Definition
  40. //------------------------------------------------------------------------------
  41. module.exports = {
  42. create,
  43. meta: {
  44. docs: {
  45. description: "disallow `require()` expressions of missing files",
  46. category: "Possible Errors",
  47. recommended: true,
  48. },
  49. fixable: false,
  50. schema: [
  51. {
  52. type: "object",
  53. properties: {
  54. allowModules: getAllowModules.schema,
  55. tryExtensions: getTryExtensions.schema,
  56. resolvePaths: getResolvePaths.schema,
  57. },
  58. additionalProperties: false,
  59. },
  60. ],
  61. },
  62. }