api.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @fileoverview Expose out ESLint and CLI to require.
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Requirements
  8. //-----------------------------------------------------------------------------
  9. const { ESLint, shouldUseFlatConfig } = require("./eslint/eslint");
  10. const { LegacyESLint } = require("./eslint/legacy-eslint");
  11. const { Linter } = require("./linter");
  12. const { RuleTester } = require("./rule-tester");
  13. const { SourceCode } = require("./languages/js/source-code");
  14. //-----------------------------------------------------------------------------
  15. // Functions
  16. //-----------------------------------------------------------------------------
  17. /**
  18. * Loads the correct ESLint constructor given the options.
  19. * @param {Object} [options] The options object
  20. * @param {boolean} [options.useFlatConfig] Whether or not to use a flat config
  21. * @returns {Promise<ESLint|LegacyESLint>} The ESLint constructor
  22. */
  23. async function loadESLint({ useFlatConfig } = {}) {
  24. /*
  25. * Note: The v8.x version of this function also accepted a `cwd` option, but
  26. * it is not used in this implementation so we silently ignore it.
  27. */
  28. const shouldESLintUseFlatConfig =
  29. useFlatConfig ?? (await shouldUseFlatConfig());
  30. return shouldESLintUseFlatConfig ? ESLint : LegacyESLint;
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Exports
  34. //-----------------------------------------------------------------------------
  35. module.exports = {
  36. Linter,
  37. loadESLint,
  38. ESLint,
  39. RuleTester,
  40. SourceCode,
  41. };