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 = useFlatConfig ?? (await shouldUseFlatConfig());
  29. return shouldESLintUseFlatConfig ? ESLint : LegacyESLint;
  30. }
  31. //-----------------------------------------------------------------------------
  32. // Exports
  33. //-----------------------------------------------------------------------------
  34. module.exports = {
  35. Linter,
  36. loadESLint,
  37. ESLint,
  38. RuleTester,
  39. SourceCode
  40. };