no-extraneous-require.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { checkExtraneous, messages } = require("../util/check-extraneous")
  7. const getAllowModules = require("../util/get-allow-modules")
  8. const getConvertPath = require("../util/get-convert-path")
  9. const getResolvePaths = require("../util/get-resolve-paths")
  10. const getTryExtensions = require("../util/get-try-extensions")
  11. const visitRequire = require("../util/visit-require")
  12. module.exports = {
  13. meta: {
  14. docs: {
  15. description:
  16. "disallow `require()` expressions which import extraneous modules",
  17. category: "Possible Errors",
  18. recommended: true,
  19. url: "https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/no-extraneous-require.md",
  20. },
  21. type: "problem",
  22. fixable: null,
  23. schema: [
  24. {
  25. type: "object",
  26. properties: {
  27. allowModules: getAllowModules.schema,
  28. convertPath: getConvertPath.schema,
  29. resolvePaths: getResolvePaths.schema,
  30. tryExtensions: getTryExtensions.schema,
  31. },
  32. additionalProperties: false,
  33. },
  34. ],
  35. messages,
  36. },
  37. create(context) {
  38. const filePath = context.getFilename()
  39. if (filePath === "<input>") {
  40. return {}
  41. }
  42. return visitRequire(context, {}, targets => {
  43. checkExtraneous(context, filePath, targets)
  44. })
  45. },
  46. }