Skip to content

Commit f879be2

Browse files
authored
feat: export ESLint.defaultConfig (#18983)
* feat: export `ESLint.defaultConfig` * fix: freeze defaultConfig object * fix: update defaultConfig type * refactor: freeze default config while exporting * docs: udpate description
1 parent 5dcbc51 commit f879be2

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

docs/src/integrate/nodejs-api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ The version string of ESLint. E.g. `"7.0.0"`.
330330

331331
This is a static property.
332332

333+
### ◆ ESLint.defaultConfig
334+
335+
```js
336+
const defaultConfig = ESLint.defaultConfig;
337+
```
338+
339+
The default configuration that ESLint uses internally. This is provided for tooling that wants to calculate configurations using the same defaults as ESLint. Keep in mind that the default configuration may change from version to version, so you shouldn't rely on any particular keys or values to be present.
340+
341+
This is a static property.
342+
333343
### ◆ ESLint.outputFixes(results)
334344

335345
```js

lib/config/default-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const Rules = require("../rules");
1515
// Helpers
1616
//-----------------------------------------------------------------------------
1717

18-
exports.defaultConfig = [
18+
exports.defaultConfig = Object.freeze([
1919
{
2020
plugins: {
2121
"@": {
@@ -72,4 +72,4 @@ exports.defaultConfig = [
7272
ecmaVersion: "latest"
7373
}
7474
}
75-
];
75+
]);

lib/eslint/eslint.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const path = require("node:path");
1515
const { version } = require("../../package.json");
1616
const { Linter } = require("../linter");
1717
const { getRuleFromConfig } = require("../config/flat-config-helpers");
18+
const { defaultConfig } = require("../config/default-config");
1819
const {
1920
Legacy: {
2021
ConfigOps: {
@@ -52,6 +53,7 @@ const { ConfigLoader, LegacyConfigLoader } = require("../config/config-loader");
5253
//------------------------------------------------------------------------------
5354

5455
// For VSCode IntelliSense
56+
/** @typedef {import("../cli-engine/cli-engine").ConfigArray} ConfigArray */
5557
/** @typedef {import("../shared/types").ConfigData} ConfigData */
5658
/** @typedef {import("../shared/types").DeprecatedRuleInfo} DeprecatedRuleInfo */
5759
/** @typedef {import("../shared/types").LintMessage} LintMessage */
@@ -522,6 +524,15 @@ class ESLint {
522524
return version;
523525
}
524526

527+
/**
528+
* The default configuration that ESLint uses internally. This is provided for tooling that wants to calculate configurations using the same defaults as ESLint.
529+
* Keep in mind that the default configuration may change from version to version, so you shouldn't rely on any particular keys or values to be present.
530+
* @type {ConfigArray}
531+
*/
532+
static get defaultConfig() {
533+
return defaultConfig;
534+
}
535+
525536
/**
526537
* Outputs fixes from the given results to files.
527538
* @param {LintResult[]} results The lint results.

lib/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,12 @@ export class ESLint {
13961396

13971397
static readonly version: string;
13981398

1399+
/**
1400+
* The default configuration that ESLint uses internally. This is provided for tooling that wants to calculate configurations using the same defaults as ESLint.
1401+
* Keep in mind that the default configuration may change from version to version, so you shouldn't rely on any particular keys or values to be present.
1402+
*/
1403+
static readonly defaultConfig: Linter.Config[];
1404+
13991405
static outputFixes(results: ESLint.LintResult[]): Promise<void>;
14001406

14011407
static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[];

tests/lib/eslint/eslint.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const shell = require("shelljs");
2929
const hash = require("../../../lib/cli-engine/hash");
3030
const { unIndent, createCustomTeardown } = require("../../_utils");
3131
const { shouldUseFlatConfig } = require("../../../lib/eslint/eslint");
32+
const { defaultConfig } = require("../../../lib/config/default-config");
3233
const coreRules = require("../../../lib/rules");
3334
const espree = require("espree");
3435

@@ -158,6 +159,10 @@ describe("ESLint", () => {
158159
assert.strictEqual(ESLint.configType, "flat");
159160
});
160161

162+
it("should have the defaultConfig static property", () => {
163+
assert.deepStrictEqual(ESLint.defaultConfig, defaultConfig);
164+
});
165+
161166
it("the default value of 'options.cwd' should be the current working directory.", async () => {
162167
process.chdir(__dirname);
163168
try {

0 commit comments

Comments
 (0)