Skip to content

Commit

Permalink
feat: Move all and recommended configs into package. (#16844)
Browse files Browse the repository at this point in the history
* feat: Move all and recommended configs into package.

Relocates the "eslint:all" and "eslint:recommended" configs into the
@eslint/js package so they can be referenced from there instead of as
strings.

Fixes #16537

* Update tools/update-eslint-all.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update package.json

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update tools/update-eslint-all.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/package.json

Co-authored-by: Mark Friedrich <exodus4d@users.noreply.github.com>

* Update package.json

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/README.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/LICENSE

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/package.json

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/README.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/README.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update packages/js/README.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update docs/src/use/configure/configuration-files-new.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Incorporate feedback

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Co-authored-by: Mark Friedrich <exodus4d@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 23, 2023
1 parent f9f195e commit c8c0c71
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 39 deletions.
6 changes: 3 additions & 3 deletions Makefile.js
Expand Up @@ -835,16 +835,16 @@ target.checkRuleFiles = function() {
}

// check eslint:recommended
const recommended = require("./conf/eslint-recommended");
const recommended = require("./packages/js").configs.recommended;

if (ruleDef.meta.docs.recommended) {
if (recommended.rules[basename] !== "error") {
console.error(`Missing rule from eslint:recommended (./conf/eslint-recommended.js): ${basename}. If you just made a rule recommended then add an entry for it in this file.`);
console.error(`Missing rule from eslint:recommended (./packages/js/src/configs/eslint-recommended.js): ${basename}. If you just made a rule recommended then add an entry for it in this file.`);
errors++;
}
} else {
if (basename in recommended.rules) {
console.error(`Extra rule in eslint:recommended (./conf/eslint-recommended.js): ${basename}. If you just added a rule then don't add an entry for it in this file.`);
console.error(`Extra rule in eslint:recommended (./packages/js/src/configs/eslint-recommended.js): ${basename}. If you just added a rule then don't add an entry for it in this file.`);
errors++;
}
}
Expand Down
27 changes: 21 additions & 6 deletions docs/src/use/configure/configuration-files-new.md
Expand Up @@ -562,16 +562,18 @@ export default [

### Using predefined configurations

ESLint has two predefined configurations:
ESLint has two predefined configurations for JavaScript:

* `eslint:recommended` - enables the rules that ESLint recommends everyone use to avoid potential errors
* `eslint:all` - enables all of the rules shipped with ESLint
* `js.configs.recommended` - enables the rules that ESLint recommends everyone use to avoid potential errors
* `js.configs.all` - enables all of the rules shipped with ESLint

To include these predefined configurations, you can insert the string values into the returned array and then make any modifications to other properties in subsequent configuration objects:
To include these predefined configurations, install the `@eslint/js` package and then make any modifications to other properties in subsequent configuration objects:

```js
import js from "@eslint/js";

export default [
"eslint:recommended",
js.configs.recommended,
{
rules: {
semi: ["warn", "always"]
Expand All @@ -580,7 +582,20 @@ export default [
];
```

Here, the `eslint:recommended` predefined configuration is applied first and then another configuration object adds the desired configuration for `semi`.
Here, the `js.configs.recommended` predefined configuration is applied first and then another configuration object adds the desired configuration for `semi`.

You can apply these predefined configs to just a subset of files by specifying a config object with a `files` key, like this:

```js
import js from "@eslint/js";

export default [
{
files: ["**/src/safe/*.js"],
...js.configs.recommended
}
];
```

## Configuration File Resolution

Expand Down
4 changes: 2 additions & 2 deletions lib/cli-engine/cli-engine.js
Expand Up @@ -615,8 +615,8 @@ class CLIEngine {
useEslintrc: options.useEslintrc,
builtInRules,
loadRules,
getEslintRecommendedConfig: () => require("../../conf/eslint-recommended.js"),
getEslintAllConfig: () => require("../../conf/eslint-all.js")
getEslintRecommendedConfig: () => require("@eslint/js").configs.recommended,
getEslintAllConfig: () => require("@eslint/js").configs.all
});
const fileEnumerator = new FileEnumerator({
configArrayFactory,
Expand Down
4 changes: 2 additions & 2 deletions lib/cli-engine/file-enumerator.js
Expand Up @@ -217,8 +217,8 @@ class FileEnumerator {
cwd = process.cwd(),
configArrayFactory = new CascadingConfigArrayFactory({
cwd,
getEslintRecommendedConfig: () => require("../../conf/eslint-recommended.js"),
getEslintAllConfig: () => require("../../conf/eslint-all.js")
getEslintRecommendedConfig: () => require("@eslint/js").configs.recommended,
getEslintAllConfig: () => require("@eslint/js").configs.all
}),
extensions = null,
globInputPaths = true,
Expand Down
22 changes: 14 additions & 8 deletions lib/config/flat-config-array.js
Expand Up @@ -13,7 +13,7 @@ const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array"
const { flatConfigSchema } = require("./flat-config-schema");
const { RuleValidator } = require("./rule-validator");
const { defaultConfig } = require("./default-config");
const recommendedConfig = require("../../conf/eslint-recommended");
const jsPlugin = require("@eslint/js");

//-----------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -96,17 +96,23 @@ class FlatConfigArray extends ConfigArray {
*/
[ConfigArraySymbol.preprocessConfig](config) {
if (config === "eslint:recommended") {
return recommendedConfig;

// if we are in a Node.js environment warn the user
if (typeof process !== "undefined" && process.emitWarning) {
process.emitWarning("The 'eslint:recommended' string configuration is deprecated and will be replaced by the @eslint/js package's 'recommended' config.");
}

return jsPlugin.configs.recommended;
}

if (config === "eslint:all") {

/*
* Load `eslint-all.js` here instead of at the top level to avoid loading all rule modules
* when it isn't necessary. `eslint-all.js` reads `meta` of rule objects to filter out deprecated ones,
* so requiring `eslint-all.js` module loads all rule modules as a consequence.
*/
return require("../../conf/eslint-all");
// if we are in a Node.js environment warn the user
if (typeof process !== "undefined" && process.emitWarning) {
process.emitWarning("The 'eslint:all' string configuration is deprecated and will be replaced by the @eslint/js package's 'all' config.");
}

return jsPlugin.configs.all;
}

/*
Expand Down
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -37,6 +37,10 @@
"lint-staged": {
"*.js": "eslint --fix",
"*.md": "markdownlint --fix",
"lib/rules/*.js": [
"node tools/update-eslint-all.js",
"git add packages/js/src/configs/eslint-all.js"
],
"docs/src/rules/*.md": [
"node tools/fetch-docs-links.js",
"git add docs/src/_data/further_reading_links.json"
Expand All @@ -57,6 +61,7 @@
"bugs": "https://github.com/eslint/eslint/issues/",
"dependencies": {
"@eslint/eslintrc": "^1.4.1",
"@eslint/js": "8.33.0",
"@humanwhocodes/config-array": "^0.11.8",
"@humanwhocodes/module-importer": "^1.0.1",
"@nodelib/fs.walk": "^1.2.8",
Expand Down
19 changes: 19 additions & 0 deletions packages/js/LICENSE
@@ -0,0 +1,19 @@
Copyright OpenJS Foundation and other contributors, <www.openjsf.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
57 changes: 57 additions & 0 deletions packages/js/README.md
@@ -0,0 +1,57 @@
[![npm version](https://img.shields.io/npm/v/@eslint/js.svg)](https://www.npmjs.com/package/@eslint/js)

# ESLint JavaScript Plugin

[Website](https://eslint.org) | [Configure ESLint](https://eslint.org/docs/latest/use/configure) | [Rules](https://eslint.org/docs/rules/) | [Contributing](https://eslint.org/docs/latest/contribute) | [Twitter](https://twitter.com/geteslint) | [Chatroom](https://eslint.org/chat)

The beginnings of separating out JavaScript-specific functionality from ESLint.

Right now, this plugin contains two configurations:

* `recommended` - enables the rules recommended by the ESLint team (the replacement for `"eslint:recommended"`)
* `all` - enables all ESLint rules (the replacement for `"eslint:all"`)

## Installation

```shell
npm install @eslint/js -D
```

## Usage

Use in your `eslint.config.js` file anytime you want to extend one of the configs:

```js
import js from "@eslint/js";

export default [

// apply recommended rules to JS files
{
files: ["**/*.js"],
rules: js.configs.recommended.rules
},

// apply recommended rules to JS files with an override
{
files: ["**/*.js"],
rules: {
...js.configs.recommended.rules,
"no-unused-vars": "warn"
}
},

// apply all rules to JS files
{
files: ["**/*.js"],
rules: {
...js.configs.all.rules,
"no-unused-vars": "warn"
}
}
]
```

## License

MIT
32 changes: 32 additions & 0 deletions packages/js/package.json
@@ -0,0 +1,32 @@
{
"name": "@eslint/js",
"version": "8.33.0",
"description": "ESLint JavaScript language implementation",
"main": "./src/index.js",
"scripts": {
},
"files": [
"LICENSE",
"README.md",
"src"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/eslint/eslint.git",
"directory": "packages/js"
},
"homepage": "https://eslint.org",
"bugs": "https://github.com/eslint/eslint/issues/",
"keywords": [
"javascript",
"eslint-plugin",
"eslint"
],
"license": "MIT",
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
}
}

0 comments on commit c8c0c71

Please sign in to comment.