Skip to content

Commit

Permalink
feat: add loadESLint 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 c0d0cb6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
18 changes: 16 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.
*/
async function loadESLint(options = {}) {
const useFlatConfig = options.useFlatConfig ?? (await shouldUseFlatConfig());

return useFlatConfig ? ESLint : LegacyESLint;
}

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

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

0 comments on commit c0d0cb6

Please sign in to comment.