Skip to content

Commit

Permalink
feat!: add loadESLint api & move shouldUseFlatConfig to official api
Browse files Browse the repository at this point in the history
fixes #18075
  • Loading branch information
aladdin-add committed Feb 7, 2024
1 parent 8c1b8dd commit 85825c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
19 changes: 17 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@
// Requirements
//-----------------------------------------------------------------------------

const { ESLint } = require("./eslint/eslint");
const { ESLint, shouldUseFlatConfig } = require("./eslint/eslint");
const { LegacyESLint } = require("./eslint/legacy-eslint");
const { Linter } = require("./linter");
const { RuleTester } = require("./rule-tester");
const { SourceCode } = require("./source-code");

/**
* Loads the appropriate ESLint class based on the provided options.
* @param {Object} options The options for loading ESLint.
* @param {boolean} options.useFlatConfig Whether to use flat configuration.
* @returns {Object} - The ESLint class to use.
*/
function loadESLint(options = {}) {
const useFlatConfig = options.useFlatConfig ?? shouldUseFlatConfig();

return useFlatConfig ? ESLint : LegacyESLint;
}

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------
Expand All @@ -22,5 +35,7 @@ module.exports = {
Linter,
ESLint,
RuleTester,
SourceCode
SourceCode,
shouldUseFlatConfig,
loadESLint
};
3 changes: 1 addition & 2 deletions lib/unsupported-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//-----------------------------------------------------------------------------

const { FileEnumerator } = require("./cli-engine/file-enumerator");
const { ESLint: FlatESLint, shouldUseFlatConfig } = require("./eslint/eslint");
const { ESLint: FlatESLint } = require("./eslint/eslint");
const { LegacyESLint } = require("./eslint/legacy-eslint");

//-----------------------------------------------------------------------------
Expand All @@ -22,7 +22,6 @@ const { LegacyESLint } = require("./eslint/legacy-eslint");
module.exports = {
builtinRules: require("./rules"),
FlatESLint,
shouldUseFlatConfig,
FileEnumerator,
LegacyESLint
};
8 changes: 8 additions & 0 deletions tests/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ describe("api", () => {
it("should have SourceCode exposed", () => {
assert.isFunction(api.SourceCode);
});

it("should have shouldUseFlatConfig exposed", () => {
assert.isFunction(api.shouldUseFlatConfig);
});

it("should have loadESLint exposed", () => {
assert.isFunction(api.loadESLint);
});
});

0 comments on commit 85825c1

Please sign in to comment.