Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add loadESLint api #18088

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});