From a8bc57eae4b47e398602995c255c09a5c6bbbec4 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 5 Aug 2023 20:47:33 +0200 Subject: [PATCH] Flat config: CLI, tests, docs (#261) --- .eslintignore | 4 +- .eslintrc.base.js | 6 +- .eslintrc.js | 4 +- .github/workflows/check.yml | 7 +- .github/workflows/test.yml | 23 +++- README.md | 166 +++++++++++++++++++++++----- bin/cli.js | 39 ++++++- eslint.base.config.js | 90 +++++++++++++++ eslint.config.js | 33 ++++++ package-lock.json | 152 ++++++++++--------------- package.json | 12 +- test-lint/{vue.vue => vue-file.vue} | 0 test/lint-verify-fail.test.js | 19 +++- test/rules.test.js | 47 ++++---- 14 files changed, 438 insertions(+), 164 deletions(-) create mode 100644 eslint.base.config.js create mode 100644 eslint.config.js rename test-lint/{vue.vue => vue-file.vue} (100%) diff --git a/.eslintignore b/.eslintignore index 3f593a7..c53e5fb 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,3 @@ !/.* -/build/ -/test-config/ +build/** +test-config/** diff --git a/.eslintrc.base.js b/.eslintrc.base.js index c1b767f..0efd2b1 100644 --- a/.eslintrc.base.js +++ b/.eslintrc.base.js @@ -2,6 +2,8 @@ // This file is only used in `./.eslintrc.js` and in the tests – it’s not part // of the eslint-config-prettier npm package. +// +// NOTE: Keep this file in sync with `./eslint.base.config.js`! const config = require("."); @@ -40,7 +42,7 @@ module.exports = { "indent": "off", "linebreak-style": "off", "no-dupe-keys": "error", - "strict": "error", + "strict": ["error", "global"], "prefer-spread": "off", "require-jsdoc": "off", "prettier/prettier": "error", @@ -74,7 +76,7 @@ module.exports = { }, overrides: [ { - files: ["*.ts", "*.tsx"], + files: ["**/*.{ts,tsx}"], parserOptions: { parser: "@typescript-eslint/parser" }, rules: { // Force a conflict with Prettier in test-lint/typescript.js. diff --git a/.eslintrc.js b/.eslintrc.js index 6a90f58..5ecbea2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,6 +5,8 @@ // sharable config from npm and then include the configs exposed by this package // as an “eat your own dogfood” test. That feels like a good test, but // complicates things a little sometimes. +// +// NOTE: Keep this file in sync with `./eslint.config.js`! module.exports = { extends: ["./.eslintrc.base.js", "./index.js", "./prettier.js"], @@ -32,7 +34,7 @@ module.exports = { }, }, { - files: ["*.test.js"], + files: ["**/*.test.js"], env: { jest: true }, }, ], diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 5602d31..d6be38d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -38,9 +38,14 @@ jobs: if: steps.cache-node_modules.outputs.cache-hit != 'true' run: npm ci --no-audit - - name: ESLint + - name: ESLint - flat run: npx eslint . + - name: ESLint - eslintrc + run: npx eslint . + env: + ESLINT_USE_FLAT_CONFIG: "false" + - name: Prettier run: npx prettier --check . diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a4cd2ae..a05b18f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,11 +38,28 @@ jobs: if: steps.cache-node_modules.outputs.cache-hit != 'true' run: npm ci --no-audit - - name: Jest + - name: Jest - flat run: npx jest + env: + ESLINT_CONFIG_PRETTIER_NO_DEPRECATED: "true" - - name: CLI sanity + - name: Jest - eslintrc + run: npx jest + env: + ESLINT_USE_FLAT_CONFIG: "false" + + - name: CLI sanity - flat + run: npm run test:cli-sanity + + - name: CLI sanity - eslintrc run: npm run test:cli-sanity + env: + ESLINT_USE_FLAT_CONFIG: "false" + + - name: CLI sanity warning - flat + run: npm run test:cli-sanity-warning - - name: CLI sanity warning + - name: CLI sanity warning - eslintrc run: npm run test:cli-sanity-warning + env: + ESLINT_USE_FLAT_CONFIG: "false" diff --git a/README.md b/README.md index 2f65fcd..8935ac0 100644 --- a/README.md +++ b/README.md @@ -10,27 +10,46 @@ Note that this config _only_ turns rules _off,_ so it only makes sense using it ## Installation -Install eslint-config-prettier: +1. Install eslint-config-prettier: -``` -npm install --save-dev eslint-config-prettier -``` + ``` + npm install --save-dev eslint-config-prettier + ``` -Then, add `"prettier"` to the "extends" array in your `.eslintrc.*` file. Make sure to put it **last,** so it gets the chance to override other configs. +2. Add eslint-config-prettier to your ESLint configuration – either to [eslintrc] or to [eslint.config.js (flat config)]. - -```json -{ - "extends": [ - "some-other-config-you-use", - "prettier" - ] -} -``` + - eslintrc: Add `"prettier"` to the "extends" array in your `.eslintrc.*` file. Make sure to put it **last,** so it gets the chance to override other configs. + + + ```json + { + "extends": [ + "some-other-config-you-use", + "prettier" + ] + } + ``` + + - eslint.config.js (flat config): Import eslint-config-prettier, and put it in the configuration array – **after** other configs that you want to override. + + + ```js + import someConfig from "some-other-config-you-use"; + import eslintConfigPrettier from "eslint-config-prettier"; -Finally, run the [CLI helper tool](#cli-helper-tool) to find problems in the `"rules"` section of your `.eslintrc.*` file. (Remember, `"rules"` always “wins” over `"extends"`!) + export default [ + someConfig, + eslintConfigPrettier, + ]; + ``` -Extending `"prettier"` turns off a bunch of core ESLint rules, as well as a few rules from these plugins: +3. Finally, run the [CLI helper tool](#cli-helper-tool) to find problems in the `"rules"` sections of your config. + +> 👉 Using [eslint-plugin-prettier]? Check out [eslint-plugin-prettier’s recommended config][eslint-plugin-prettier-recommended]. + +### Plugins + +eslint-config-prettier not only turns off _core_ rules, but also some from these plugins automatically: - [@typescript-eslint/eslint-plugin] - [@babel/eslint-plugin] @@ -41,10 +60,43 @@ Extending `"prettier"` turns off a bunch of core ESLint rules, as well as a few - [eslint-plugin-unicorn] - [eslint-plugin-vue] -> 👉 Using [eslint-plugin-prettier]? Check out [eslint-plugin-prettier’s recommended config][eslint-plugin-prettier-recommended]. - > ℹ️ Note: You might find guides on the Internet saying you should also extend stuff like `"prettier/react"`. Since version 8.0.0 of eslint-config-prettier, all you need to extend is `"prettier"`! That includes all plugins. +#### eslint.config.js (flat config) plugin caveat + +With flat config, _you_ get to decide the plugin name! For example: + +```js +import typescriptEslint from "@typescript-eslint/eslint-plugin"; +import eslintConfigPrettier from "eslint-config-prettier"; + +export default [ + { + plugins: { + // You’d typically use one of the following two: + // typescriptEslint: typescriptEslint, + // typescriptEslint, + // But in this example we give it another name. + // It might be tempting to use something shorter like “ts”: + ts: typescriptEslint, // 🚨 Don’t do this! + }, + rules: { + // With eslintrc, this is _always_ called: + // @typescript-eslint/indent + // But in eslint.config.js (flat config), the name chosen above in `plugins` is used. + "ts/indent": "error", // 🚨 Don’t do this! + }, + }, + eslintConfigPrettier, +]; +``` + +You might expect eslint-config-prettier to turn off `ts/indent`, but it won’t! Because eslint-config-prettier only turns off `@typescript-eslint/indent`. It cannot know what you chose to call the plugin. Same thing for the CLI helper tool. + +Simply stick to the official plugin names and you’ll be all good. + +If you encounter a shared _config_ that uses a non-standard plugin name, please ask them to use the standard name instead. + ### Excluding deprecated rules Some of the rules that eslint-config-prettier turns off may be deprecated, or even removed from ESLint. **This is perfectly fine,** but if you really need to omit the deprecated and removed rules, you can do so by setting the `ESLINT_CONFIG_PRETTIER_NO_DEPRECATED` environment variable to a non-empty value. For example: @@ -55,9 +107,19 @@ env ESLINT_CONFIG_PRETTIER_NO_DEPRECATED=true npx eslint-find-rules --deprecated ## CLI helper tool -eslint-config-prettier also ships with a little CLI tool to help you check if your configuration contains any rules that are unnecessary or conflict with Prettier. +eslint-config-prettier also ships with a little CLI tool to help you check if your configuration contains any rules that are unnecessary or conflict with Prettier. Here’s how to run it: + +``` +npx eslint-config-prettier path/to/main.js +``` + +(Change `path/to/main.js` to a file that exists in your project.) + +### What and why + +Now, let’s have a look at what it does and why you might want to use it. -🚨 This example has a **conflicting rule** `"indent"` enabled: +🚨 This eslintrc example has a **conflicting rule** `"indent"` enabled: ```json @@ -72,28 +134,69 @@ eslint-config-prettier also ships with a little CLI tool to help you check if yo } ``` -While the `"prettier"` config can disable problematic rules in `"some-other-config-you-use"`, it cannot touch `"rules"`! (That’s how ESLint works – it lets you override configs you extend.) The CLI helper tool reports that `"indent"` conflicts with Prettier, so you can remove it. (Which is nice – simplifying your config!) +For eslintrc, while the `"prettier"` config can disable problematic rules in `"some-other-config-you-use"`, it cannot touch `"rules"`! (That’s how ESLint works – it lets you override configs you extend.) The CLI helper tool reports that `"indent"` conflicts with Prettier, so you can remove it. (Which is nice – simplifying your config!) -You can run it using `npx`: +🚨 This eslint.config.js (flat config) example also has a **conflicting rule** `"indent"` enabled: +```js +import someConfig from "some-other-config-you-use"; +import eslintConfigPrettier from "eslint-config-prettier"; + +export default [ + someConfig, + eslintConfigPrettier, + { + rules: { + indent: "error", + }, + }, +]; ``` -npx eslint-config-prettier path/to/main.js + +With the new ESLint “flat config” format, you can control what things override what yourself. One way of solving the above conflict is to reorder the config objects so that eslint-config-prettier is last: + +```js +import someConfig from "some-other-config-you-use"; +import eslintConfigPrettier from "eslint-config-prettier"; + +export default [ + someConfig, + { + rules: { + indent: "error", + }, + }, + eslintConfigPrettier, // eslint-config-prettier last +]; ``` -(Change `path/to/main.js` to a file that exists in your project.) +However, looking at the above config might feel confusing. It looks like we enable the `indent` rule, but in reality it’s disabled thanks to the `eslintConfigPrettier` line below it. Instead you might want to actually have your own rules _after_ eslint-config-prettier and run the CLI helper tool to find out about problems, so you can remove conflicting rules from the config file altogether (simplifying your config). + +### Checking multiple files -In theory you need to run the tool for every single file in your project to be 100% sure that there are no conflicting rules, because ESLint supports having different rules for different files. Usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use [multiple configuration files] or [overrides], you can provide several files check: +In theory you need to run the tool for every single file in your project to be 100% sure that there are no conflicting rules, because ESLint supports having different rules for different files. Usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use [multiple configuration files] or [overrides], you can provide several files to check: ``` npx eslint-config-prettier index.js test/index.js legacy/main.js ``` -Exit codes: +### Exit codes - 0: No problems found. - 1: Unexpected error. - 2: Conflicting rules found. +### ESLINT_USE_FLAT_CONFIG environment variable + +Just like ESLint itself, you can control the eslint-config-prettier CLI helper tool using the `ESLINT_USE_FLAT_CONFIG` environment variable: + +- `ESLINT_USE_FLAT_CONFIG=true`: Only use eslint.config.js (flat config). +- `ESLINT_USE_FLAT_CONFIG=false`: Only use eslintrc files. +- Unset or any other value: First try eslint.config.js, then eslintrc. + +> **Warning** +> For eslint.config.js (flat config), the CLI helper tool imports `eslint/use-at-your-own-risk` which may break at any time. + ### Legacy eslint-config-prettier versions before 7.0.0 had a slightly different CLI tool that was run in a different way. For example: @@ -664,17 +767,18 @@ Then, create `test-lint/foobar.js`: console.log(); ``` -`test-lint/foobar.js` must fail when used with eslint-plugin-foobar and eslint-plugin-prettier at the same time – until `"prettier/foobar"` is added to the "extends" property of an ESLint config. The file should be formatted according to Prettier, and that formatting should disagree with the plugin. +`test-lint/foobar.js` must fail when used with eslint-plugin-foobar and eslint-plugin-prettier at the same time – until eslint-config-prettier is added to the ESLint config. The file should be formatted according to Prettier, and that formatting should disagree with the plugin. Finally, you need to mention the plugin in several places: - Add eslint-plugin-foobar to the "devDependencies" field in `package.json`. -- Make sure that at least one rule from eslint-plugin-foobar gets used in `.eslintrc.base.js`. -- Add it to the lists of supported plugins and in this `README.md`. +- Make sure that at least one rule from eslint-plugin-foobar gets used in `.eslintrc.base.js` and `eslint.base.config.js`. +- Add it to the lists of supported plugins in this `README.md`. When you’re done, run `npm test` to verify that you got it all right. It runs several other npm scripts: -- `"test:lint"` makes sure that the files in `test-lint/` pass ESLint when the exclusions from eslint-config-prettier are used. It also lints the code of eslint-config-prettier itself, and checks that Prettier has been run on all files. +- `"test:prettier"` checks that Prettier has been run on all files. +- `"test:eslint"` makes sure that the files in `test-lint/` pass ESLint when the exclusions from eslint-config-prettier are used. It also lints the code of eslint-config-prettier itself. - `"test:lint-verify-fail"` is run by a test in `test/lint-verify-fail.test.js`. - `"test:lint-rules"` is run by a test in `test/rules.test.js`. - `"test:jest"` runs unit tests that check a number of things: @@ -703,6 +807,8 @@ When you’re done, run `npm test` to verify that you got it all right. It runs [eslint-plugin-standard]: https://github.com/xjamundx/eslint-plugin-standard [eslint-plugin-unicorn]: https://github.com/sindresorhus/eslint-plugin-unicorn [eslint-plugin-vue]: https://github.com/vuejs/eslint-plugin-vue +[eslint.config.js (flat config)]: https://eslint.org/docs/latest/use/configure/configuration-files-new +[eslintrc]: https://eslint.org/docs/latest/use/configure/configuration-files [lines-around-comment]: https://eslint.org/docs/rules/lines-around-comment [max-len]: https://eslint.org/docs/rules/max-len [multiple configuration files]: https://eslint.org/docs/user-guide/configuring#configuration-cascading-and-hierarchy diff --git a/bin/cli.js b/bin/cli.js index 46fc561..9a47456 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -8,9 +8,19 @@ const prettier = require("../prettier"); // Require locally installed eslint, for `npx eslint-config-prettier` support // with no local eslint-config-prettier installation. -const { ESLint } = require(require.resolve("eslint", { - paths: [process.cwd(), ...require.resolve.paths("eslint")], -})); +const localRequire = (request) => + require(require.resolve(request, { + paths: [process.cwd(), ...require.resolve.paths("eslint")], + })); + +let experimentalApi = {}; +try { + experimentalApi = localRequire("eslint/use-at-your-own-risk"); + // eslint-disable-next-line unicorn/prefer-optional-catch-binding +} catch (_error) {} + +const { ESLint, FlatESLint = experimentalApi.FlatESLint } = + localRequire("eslint"); const SPECIAL_RULES_URL = "https://github.com/prettier/eslint-config-prettier#special-rules"; @@ -27,8 +37,27 @@ if (module === require.main) { } const eslint = new ESLint(); - - Promise.all(args.map((file) => eslint.calculateConfigForFile(file))) + const flatESLint = FlatESLint === undefined ? undefined : new FlatESLint(); + + Promise.all( + args.map((file) => { + switch (process.env.ESLINT_USE_FLAT_CONFIG) { + case "true": { + return flatESLint.calculateConfigForFile(file); + } + case "false": { + return eslint.calculateConfigForFile(file); + } + default: { + // This turns synchronous errors (such as `.calculateConfigForFile` not existing) + // and turns them into promise rejections. + return Promise.resolve() + .then(() => flatESLint.calculateConfigForFile(file)) + .catch(() => eslint.calculateConfigForFile(file)); + } + } + }) + ) .then((configs) => { const rules = configs.flatMap(({ rules }, index) => Object.entries(rules).map((entry) => [...entry, args[index]]) diff --git a/eslint.base.config.js b/eslint.base.config.js new file mode 100644 index 0000000..b9c25e8 --- /dev/null +++ b/eslint.base.config.js @@ -0,0 +1,90 @@ +"use strict"; + +// This file is only used in `./eslint.config.js` and in the tests – it’s not part +// of the eslint-config-prettier npm package. +// +// NOTE: Keep this file in sync with `./.eslintrc.base.js`! + +const fs = require("fs"); +const path = require("path"); +const babelOld = require("eslint-plugin-babel"); +const babelNew = require("@babel/eslint-plugin"); +const babelParser = require("@babel/eslint-parser"); +const flowtype = require("eslint-plugin-flowtype"); +const globals = require("globals"); +const google = require("eslint-config-google"); +const prettier = require("eslint-plugin-prettier"); +const react = require("eslint-plugin-react"); +const standard = require("eslint-plugin-standard"); +const typescriptEslint = require("@typescript-eslint/eslint-plugin"); +const unicorn = require("eslint-plugin-unicorn"); +const vue = require("eslint-plugin-vue"); +const vueParser = require("vue-eslint-parser"); +const eslintrcBase = require("./.eslintrc.base"); + +module.exports = [ + { + ignores: fs + .readFileSync(path.join(__dirname, ".eslintignore"), "utf8") + .trim() + .split("\n"), + }, + { + // TODO: Figure out how to get flowtype running in flat config. + ignores: ["test-lint/flowtype.js"], + }, + google, + { + plugins: { + "@typescript-eslint": typescriptEslint, + "babel": babelOld, + "@babel": babelNew, + flowtype, + prettier, + react, + standard, + unicorn, + vue, + }, + }, + { + rules: flowtype.configs.recommended.rules, + settings: flowtype.configs.recommended.settings, + }, + { + rules: react.configs.all.rules, + }, + { + rules: unicorn.configs.recommended.rules, + }, + { + languageOptions: { + ecmaVersion: eslintrcBase.parserOptions.ecmaVersion, + sourceType: eslintrcBase.parserOptions.sourceType, + globals: { + ...globals.es6, + ...globals.node, + }, + parser: babelParser, + }, + rules: eslintrcBase.rules, + settings: eslintrcBase.settings, + }, + { + files: ["**/*.vue"], + processor: vue.processors[".vue"], + languageOptions: { + parser: vueParser, + }, + rules: { + ...vue.configs.base.rules, + ...vue.configs.essential.rules, + ...vue.configs["strongly-recommended"].rules, + ...vue.configs.recommended.rules, + }, + }, + ...eslintrcBase.overrides.map(({ parserOptions, ...override }) => ({ + ...override, + languageOptions: { parser: require(parserOptions.parser) }, + })), +]; diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..3678394 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,33 @@ +"use strict"; + +// This is the internal ESLint config for this project itself – it’s not part of +// the eslint-config-prettier npm package. The idea here is to extend some +// sharable config from npm and then include the configs exposed by this package +// as an “eat your own dogfood” test. That feels like a good test, but +// complicates things a little sometimes. +// +// NOTE: Keep this file in sync with `./.eslintrc.js`! + +const globals = require("globals"); +const base = require("./eslint.base.config"); +const index = require("."); +const prettier = require("./prettier"); +const eslintrc = require("./.eslintrc"); + +module.exports = [ + ...base, + index, + prettier, + { + rules: eslintrc.rules, + }, + ...eslintrc.overrides.map(({ env = {}, ...override }) => ({ + ...override, + languageOptions: { + globals: Object.entries(env).reduce( + (acc, [key, enabled]) => (enabled ? { ...acc, ...globals[key] } : acc), + {} + ), + }, + })), +]; diff --git a/package-lock.json b/package-lock.json index 01ceefb..359e998 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,9 +22,11 @@ "eslint-plugin-standard": "4.1.0", "eslint-plugin-unicorn": "48.0.1", "eslint-plugin-vue": "9.15.1", + "globals": "13.20.0", "jest": "29.6.2", "prettier": "3.0.0", - "typescript": "5.1.6" + "typescript": "5.1.6", + "vue-eslint-parser": "9.3.1" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -592,6 +594,15 @@ "node": ">=6.9.0" } }, + "node_modules/@babel/traverse/node_modules/globals": { + "version": "11.12.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/@babel/types": { "version": "7.22.5", "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.5.tgz", @@ -671,33 +682,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/@eslint/eslintrc/node_modules/globals": { - "version": "13.20.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.20.0.tgz", - "integrity": "sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ==", - "dev": true, - "dependencies": { - "type-fest": "^0.20.2" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/@eslint/eslintrc/node_modules/type-fest": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", - "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", - "dev": true, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/@eslint/js": { "version": "8.44.0", "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.44.0.tgz", @@ -3427,21 +3411,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/eslint/node_modules/globals": { - "version": "13.19.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.19.0.tgz", - "integrity": "sha512-dkQ957uSRWHw7CFXLUtUHQI3g3aWApYhfNR2O6jn/907riyTYKVBmxYVROkBcY614FSSeSJh7Xm7SrUWCxvJMQ==", - "dev": true, - "dependencies": { - "type-fest": "^0.20.2" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/eslint/node_modules/has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -3508,18 +3477,6 @@ "node": ">=8" } }, - "node_modules/eslint/node_modules/type-fest": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", - "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", - "dev": true, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/espree": { "version": "9.6.1", "resolved": "https://registry.npmjs.org/espree/-/espree-9.6.1.tgz", @@ -3949,12 +3906,30 @@ } }, "node_modules/globals": { - "version": "11.12.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", - "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "version": "13.20.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.20.0.tgz", + "integrity": "sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ==", + "dev": true, + "dependencies": { + "type-fest": "^0.20.2" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/globals/node_modules/type-fest": { + "version": "0.20.2", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", + "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", "dev": true, "engines": { - "node": ">=4" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/globby": { @@ -8628,6 +8603,14 @@ "@babel/types": "^7.16.8", "debug": "^4.1.0", "globals": "^11.1.0" + }, + "dependencies": { + "globals": { + "version": "11.12.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "dev": true + } } }, "@babel/types": { @@ -8685,23 +8668,6 @@ "js-yaml": "^4.1.0", "minimatch": "^3.1.2", "strip-json-comments": "^3.1.1" - }, - "dependencies": { - "globals": { - "version": "13.20.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.20.0.tgz", - "integrity": "sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ==", - "dev": true, - "requires": { - "type-fest": "^0.20.2" - } - }, - "type-fest": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", - "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", - "dev": true - } } }, "@eslint/js": { @@ -10460,15 +10426,6 @@ "path-exists": "^4.0.0" } }, - "globals": { - "version": "13.19.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.19.0.tgz", - "integrity": "sha512-dkQ957uSRWHw7CFXLUtUHQI3g3aWApYhfNR2O6jn/907riyTYKVBmxYVROkBcY614FSSeSJh7Xm7SrUWCxvJMQ==", - "dev": true, - "requires": { - "type-fest": "^0.20.2" - } - }, "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -10510,12 +10467,6 @@ "requires": { "has-flag": "^4.0.0" } - }, - "type-fest": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", - "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", - "dev": true } } }, @@ -11048,10 +10999,21 @@ } }, "globals": { - "version": "11.12.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", - "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", - "dev": true + "version": "13.20.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.20.0.tgz", + "integrity": "sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ==", + "dev": true, + "requires": { + "type-fest": "^0.20.2" + }, + "dependencies": { + "type-fest": { + "version": "0.20.2", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", + "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==", + "dev": true + } + } }, "globby": { "version": "11.1.0", diff --git a/package.json b/package.json index a4bc2fa..ff5e7e8 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,18 @@ "private": true, "scripts": { "prettier": "prettier --write .", - "test:lint": "eslint . && prettier --check .", + "test:prettier": "prettier --check .", + "test:eslint": "eslint .", "test:lint-verify-fail": "eslint \"test-lint/*.{js,ts,vue}\" --config .eslintrc.base.js --format json", + "test:lint-verify-fail:flat": "eslint \"test-lint/*.{js,ts,vue}\" --config eslint.base.config.js --format json", "test:lint-rules": "eslint index.js --config test-config/.eslintrc.js --format json", + "test:lint-rules:flat": "eslint index.js --config test-config/eslint.config.js --format json", "test:deprecated": "eslint-find-rules --deprecated index.js", "test:jest": "jest", "test:cli-sanity": "node ./bin/cli.js index.js", "test:cli-sanity-warning": "node ./bin/cli.js react.js ./bin/cli.js", - "test": "npm run test:lint && npm run test:jest && npm run test:cli-sanity && npm run test:cli-sanity-warning && npm run build", + "test:with-env": "npm run test:eslint && npm run test:jest && npm run test:cli-sanity && npm run test:cli-sanity-warning", + "test": "npm run test:prettier && ESLINT_CONFIG_PRETTIER_NO_DEPRECATED=true npm run test:with-env && ESLINT_USE_FLAT_CONFIG=false npm run test:with-env && npm run build", "build": "node scripts/build.js" }, "devDependencies": { @@ -30,8 +34,10 @@ "eslint-plugin-standard": "4.1.0", "eslint-plugin-unicorn": "48.0.1", "eslint-plugin-vue": "9.15.1", + "globals": "13.20.0", "jest": "29.6.2", "prettier": "3.0.0", - "typescript": "5.1.6" + "typescript": "5.1.6", + "vue-eslint-parser": "9.3.1" } } diff --git a/test-lint/vue.vue b/test-lint/vue-file.vue similarity index 100% rename from test-lint/vue.vue rename to test-lint/vue-file.vue diff --git a/test/lint-verify-fail.test.js b/test/lint-verify-fail.test.js index 993ee18..048a5f2 100644 --- a/test/lint-verify-fail.test.js +++ b/test/lint-verify-fail.test.js @@ -6,7 +6,12 @@ const path = require("path"); const testLintFiles = fs .readdirSync(path.join(__dirname, "..", "test-lint")) - .filter((name) => !name.startsWith(".")); + .filter( + (name) => + !name.startsWith(".") && + // TODO: Figure out how to get flowtype running in flat config. + (process.env.ESLINT_USE_FLAT_CONFIG === "false" || name !== "flowtype.js") + ); function parseJson(result) { try { @@ -29,7 +34,13 @@ ${result.stderr} describe("test-lint/ causes errors without eslint-config-prettier", () => { const result = childProcess.spawnSync( "npm", - ["run", "test:lint-verify-fail", "--silent"], + [ + "run", + process.env.ESLINT_USE_FLAT_CONFIG === "false" + ? "test:lint-verify-fail" + : "test:lint-verify-fail:flat", + "--silent", + ], { encoding: "utf8", shell: true } ); const output = parseJson(result); @@ -39,7 +50,9 @@ describe("test-lint/ causes errors without eslint-config-prettier", () => { }); output.forEach((data) => { - const name = path.basename(data.filePath).replace(/\.(?:js|ts|vue)$/, ""); + const name = path + .basename(data.filePath) + .replace(/\.(?:js|ts)$|-file\.vue$/, ""); const ruleIds = data.messages.map((message) => message.ruleId); describe(name, () => { diff --git a/test/rules.test.js b/test/rules.test.js index b51bcd6..a4c5850 100644 --- a/test/rules.test.js +++ b/test/rules.test.js @@ -4,8 +4,6 @@ const childProcess = require("child_process"); const fs = require("fs"); const path = require("path"); const config = require("../"); -const eslintConfig = require("../.eslintrc"); -const eslintConfigBase = require("../.eslintrc.base"); const ROOT = path.join(__dirname, ".."); const TEST_CONFIG_DIR = path.join(ROOT, "test-config"); @@ -46,6 +44,8 @@ function createTestConfigDir() { fs.mkdirSync(TEST_CONFIG_DIR); // Change all rules to "warn", so that ESLint warns about unknown rules. + // Note: With flat config, ESLint throws errors for _removed_ rules if + // set to anything other than "off". const newRules = Object.fromEntries( Object.entries(config.rules).map(([ruleName]) => [ruleName, "warn"]) ); @@ -57,20 +57,21 @@ function createTestConfigDir() { `module.exports = ${JSON.stringify(newConfig, null, 2)};` ); - fs.copyFileSync( - path.join(ROOT, "prettier.js"), - path.join(TEST_CONFIG_DIR, "prettier.js") - ); - - fs.writeFileSync( - path.join(TEST_CONFIG_DIR, ".eslintrc.js"), - `module.exports = ${JSON.stringify(eslintConfig, null, 2)};` - ); - - fs.writeFileSync( - path.join(TEST_CONFIG_DIR, ".eslintrc.base.js"), - `module.exports = ${JSON.stringify(eslintConfigBase, null, 2)};` - ); + const filesToCopy = [ + "prettier.js", + ".eslintrc.js", + ".eslintrc.base.js", + "eslint.config.js", + "eslint.base.config.js", + ".eslintignore", + ]; + + for (const fileToCopy of filesToCopy) { + fs.copyFileSync( + path.join(ROOT, fileToCopy), + path.join(TEST_CONFIG_DIR, fileToCopy) + ); + } } describe("all plugins have tests in test-lint/", () => { @@ -78,7 +79,7 @@ describe("all plugins have tests in test-lint/", () => { test(plugin, () => { const testFileName = plugin === "vue" - ? "vue.vue" + ? "vue-file.vue" : plugin === "@typescript-eslint" ? `${plugin}.ts` : `${plugin}.js`; @@ -126,7 +127,13 @@ describe('all rules are set to "off" or 0', () => { test("there are no unknown rules", () => { const result = childProcess.spawnSync( "npm", - ["run", "test:lint-rules", "--silent"], + [ + "run", + process.env.ESLINT_USE_FLAT_CONFIG === "false" + ? "test:lint-rules" + : "test:lint-rules:flat", + "--silent", + ], { encoding: "utf8", shell: true } ); const output = parseJson(result); @@ -147,7 +154,9 @@ test("support omitting all deprecated rules", () => { }, }); - const result1 = run(); + const result1 = run({ + ESLINT_CONFIG_PRETTIER_NO_DEPRECATED: undefined, + }); const result2 = run({ ESLINT_CONFIG_PRETTIER_NO_DEPRECATED: "true", });