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: Enable eslint.config.js lookup from CLI #16235

Merged
merged 16 commits into from Aug 26, 2022
17 changes: 17 additions & 0 deletions .eslintrc.js
@@ -1,3 +1,20 @@
/*
* IMPORTANT!
*
* Any changes made to this file must also be made to eslint.config.js.
*
* Internally, ESLint is using the eslint.config.js file to lint itself.
* This file is needed too, because:
*
* 1. There are tests that expect .eslintrc.js to be present to actually run.
* 2. ESLint VS Code extension expects eslintrc config files to be
* present to work correctly.
*
* Once we no longer need to support both eslintrc and flat config, we will
* remove this file.
*/


"use strict";

const path = require("path");
Expand Down
6 changes: 2 additions & 4 deletions bin/eslint.js
Expand Up @@ -9,9 +9,6 @@

"use strict";

// to use V8's code cache to speed up instantiation time
require("v8-compile-cache");
nzakas marked this conversation as resolved.
Show resolved Hide resolved

// must do this initialization *before* other requires in order to work
if (process.argv.includes("--debug")) {
require("debug").enable("eslint:*,-eslint:code-path,eslintrc:*");
Expand Down Expand Up @@ -137,6 +134,7 @@ ${message}`);
// Otherwise, call the CLI.
process.exitCode = await require("../lib/cli").execute(
process.argv,
process.argv.includes("--stdin") ? await readStdin() : null
process.argv.includes("--stdin") ? await readStdin() : null,
true
);
}()).catch(onFatalError);
Expand Up @@ -10,7 +10,7 @@ eleventyNavigation:
---

::: warning
This is an experimental feature that is not enabled by default. You can use the configuration system described on this page by using the `FlatESLint` class, the `FlatRuleTester` class, or by setting `configType: "flat"` in the `Linter` class.
This is an experimental feature. To opt-in, place a `eslint.config.js` file in the root of your project. If you are using the API, you can use the configuration system described on this page by using the `FlatESLint` class, the `FlatRuleTester` class, or by setting `configType: "flat"` in the `Linter` class.
nzakas marked this conversation as resolved.
Show resolved Hide resolved
:::

## Configuration File
Expand Down
71 changes: 44 additions & 27 deletions eslint.config.js
Expand Up @@ -5,12 +5,29 @@

"use strict";

/*
* IMPORTANT!
*
* Any changes made to this file must also be made to .eslintrc.js.
*
* Internally, ESLint is using the eslint.config.js file to lint itself.
* The .eslintrc.js file is needed too, because:
*
* 1. There are tests that expect .eslintrc.js to be present to actually run.
* 2. ESLint VS Code extension expects eslintrc config files to be
* present to work correctly.
*
* Once we no longer need to support both eslintrc and flat config, we will
* remove .eslintrc.js.
*/

//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------

const path = require("path");
const internalPlugin = require("eslint-plugin-internal-rules");
const eslintPlugin = require("eslint-plugin-eslint-plugin");
const { FlatCompat } = require("@eslint/eslintrc");
const globals = require("globals");

Expand All @@ -24,7 +41,6 @@ const compat = new FlatCompat({

const INTERNAL_FILES = {
CLI_ENGINE_PATTERN: "lib/cli-engine/**/*",
INIT_PATTERN: "lib/init/**/*",
LINTER_PATTERN: "lib/linter/**/*",
RULE_TESTER_PATTERN: "lib/rule-tester/**/*",
RULES_PATTERN: "lib/rules/**/*",
Expand Down Expand Up @@ -60,11 +76,6 @@ function createInternalFilesPatterns(pattern = null) {
}));
}


//-----------------------------------------------------------------------------
// Config
//-----------------------------------------------------------------------------

module.exports = [
...compat.extends("eslint", "plugin:eslint-plugin/recommended"),
{
Expand Down Expand Up @@ -101,13 +112,25 @@ module.exports = [
"eslint-plugin/test-case-shorthand-strings": "error",
"internal-rules/multiline-comment-style": "error"
}

},
{
files: ["tools/*.js"],
rules: {
"no-console": "off"
}
},
{
files: ["lib/rules/*", "tools/internal-rules/*"],
ignores: ["index.js"],
ignores: ["**/index.js"],
rules: {
"eslint-plugin/prefer-object-rule": "error",
...eslintPlugin.configs["rules-recommended"].rules,
"eslint-plugin/no-missing-message-ids": "error",
"eslint-plugin/no-unused-message-ids": "error",
"eslint-plugin/prefer-message-ids": "error",
"eslint-plugin/prefer-placeholders": "error",
"eslint-plugin/prefer-replace-text": "error",
"eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"],
"eslint-plugin/require-meta-docs-description": ["error", { pattern: "^(Enforce|Require|Disallow)" }],
"internal-rules/no-invalid-meta": "error"
}
},
Expand All @@ -119,7 +142,16 @@ module.exports = [
}
},
{
files: ["tests/**/*"],
files: ["tests/lib/rules/*", "tests/tools/internal-rules/*"],
rules: {
...eslintPlugin.configs["tests-recommended"].rules,
"eslint-plugin/prefer-output-null": "error",
"eslint-plugin/test-case-property-ordering": "error",
"eslint-plugin/test-case-shorthand-strings": "error"
}
},
{
files: ["tests/**/*.js"],
languageOptions: {
globals: {
...globals.mocha
Expand Down Expand Up @@ -147,17 +179,7 @@ module.exports = [
files: [INTERNAL_FILES.CLI_ENGINE_PATTERN],
rules: {
"n/no-restricted-require": ["error", [
...createInternalFilesPatterns(INTERNAL_FILES.CLI_ENGINE_PATTERN),
resolveAbsolutePath("lib/init/index.js")
]]
}
},
{
files: [INTERNAL_FILES.INIT_PATTERN],
rules: {
"n/no-restricted-require": ["error", [
...createInternalFilesPatterns(INTERNAL_FILES.INIT_PATTERN),
resolveAbsolutePath("lib/rule-tester/index.js")
...createInternalFilesPatterns(INTERNAL_FILES.CLI_ENGINE_PATTERN)
]]
}
},
Expand All @@ -168,7 +190,6 @@ module.exports = [
...createInternalFilesPatterns(INTERNAL_FILES.LINTER_PATTERN),
"fs",
resolveAbsolutePath("lib/cli-engine/index.js"),
resolveAbsolutePath("lib/init/index.js"),
resolveAbsolutePath("lib/rule-tester/index.js")
]]
}
Expand All @@ -180,7 +201,6 @@ module.exports = [
...createInternalFilesPatterns(INTERNAL_FILES.RULES_PATTERN),
"fs",
resolveAbsolutePath("lib/cli-engine/index.js"),
resolveAbsolutePath("lib/init/index.js"),
resolveAbsolutePath("lib/linter/index.js"),
resolveAbsolutePath("lib/rule-tester/index.js"),
resolveAbsolutePath("lib/source-code/index.js")
Expand All @@ -193,7 +213,6 @@ module.exports = [
"n/no-restricted-require": ["error", [
...createInternalFilesPatterns(),
resolveAbsolutePath("lib/cli-engine/index.js"),
resolveAbsolutePath("lib/init/index.js"),
resolveAbsolutePath("lib/linter/index.js"),
resolveAbsolutePath("lib/rule-tester/index.js"),
resolveAbsolutePath("lib/source-code/index.js")
Expand All @@ -207,7 +226,6 @@ module.exports = [
...createInternalFilesPatterns(INTERNAL_FILES.SOURCE_CODE_PATTERN),
"fs",
resolveAbsolutePath("lib/cli-engine/index.js"),
resolveAbsolutePath("lib/init/index.js"),
resolveAbsolutePath("lib/linter/index.js"),
resolveAbsolutePath("lib/rule-tester/index.js"),
resolveAbsolutePath("lib/rules/index.js")
Expand All @@ -219,8 +237,7 @@ module.exports = [
rules: {
"n/no-restricted-require": ["error", [
...createInternalFilesPatterns(INTERNAL_FILES.RULE_TESTER_PATTERN),
resolveAbsolutePath("lib/cli-engine/index.js"),
resolveAbsolutePath("lib/init/index.js")
resolveAbsolutePath("lib/cli-engine/index.js")
]]
}
}
Expand Down