Skip to content

Commit

Permalink
feat: export ESLint.defaultConfig (eslint#18983)
Browse files Browse the repository at this point in the history
* feat: export `ESLint.defaultConfig`

* fix: freeze defaultConfig object

* fix: update defaultConfig type

* refactor: freeze default config while exporting

* docs: udpate description
  • Loading branch information
snitin315 authored Oct 8, 2024
1 parent 5dcbc51 commit f879be2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/src/integrate/nodejs-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ The version string of ESLint. E.g. `"7.0.0"`.

This is a static property.

### ◆ ESLint.defaultConfig

```js
const defaultConfig = ESLint.defaultConfig;
```

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.

This is a static property.

### ◆ ESLint.outputFixes(results)

```js
Expand Down
4 changes: 2 additions & 2 deletions lib/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Rules = require("../rules");
// Helpers
//-----------------------------------------------------------------------------

exports.defaultConfig = [
exports.defaultConfig = Object.freeze([
{
plugins: {
"@": {
Expand Down Expand Up @@ -72,4 +72,4 @@ exports.defaultConfig = [
ecmaVersion: "latest"
}
}
];
]);
11 changes: 11 additions & 0 deletions lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const path = require("node:path");
const { version } = require("../../package.json");
const { Linter } = require("../linter");
const { getRuleFromConfig } = require("../config/flat-config-helpers");
const { defaultConfig } = require("../config/default-config");
const {
Legacy: {
ConfigOps: {
Expand Down Expand Up @@ -52,6 +53,7 @@ const { ConfigLoader, LegacyConfigLoader } = require("../config/config-loader");
//------------------------------------------------------------------------------

// For VSCode IntelliSense
/** @typedef {import("../cli-engine/cli-engine").ConfigArray} ConfigArray */
/** @typedef {import("../shared/types").ConfigData} ConfigData */
/** @typedef {import("../shared/types").DeprecatedRuleInfo} DeprecatedRuleInfo */
/** @typedef {import("../shared/types").LintMessage} LintMessage */
Expand Down Expand Up @@ -522,6 +524,15 @@ class ESLint {
return version;
}

/**
* 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.
* @type {ConfigArray}
*/
static get defaultConfig() {
return defaultConfig;
}

/**
* Outputs fixes from the given results to files.
* @param {LintResult[]} results The lint results.
Expand Down
6 changes: 6 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,12 @@ export class ESLint {

static readonly version: string;

/**
* 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.
*/
static readonly defaultConfig: Linter.Config[];

static outputFixes(results: ESLint.LintResult[]): Promise<void>;

static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[];
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const shell = require("shelljs");
const hash = require("../../../lib/cli-engine/hash");
const { unIndent, createCustomTeardown } = require("../../_utils");
const { shouldUseFlatConfig } = require("../../../lib/eslint/eslint");
const { defaultConfig } = require("../../../lib/config/default-config");
const coreRules = require("../../../lib/rules");
const espree = require("espree");

Expand Down Expand Up @@ -158,6 +159,10 @@ describe("ESLint", () => {
assert.strictEqual(ESLint.configType, "flat");
});

it("should have the defaultConfig static property", () => {
assert.deepStrictEqual(ESLint.defaultConfig, defaultConfig);
});

it("the default value of 'options.cwd' should be the current working directory.", async () => {
process.chdir(__dirname);
try {
Expand Down

0 comments on commit f879be2

Please sign in to comment.