From 51cd143b97234d8234672fa04c73b8d03fd20aad Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Sun, 10 Dec 2023 18:07:38 -0500 Subject: [PATCH] feat: support eslint flat config --- README.md | 6 +++--- index.js | 12 +++++++++++- lib/configs/recommended.js | 8 ++++++++ package.json | 5 ++++- tests/index.js | 31 +++++++++++++++++++++++-------- 5 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 lib/configs/recommended.js diff --git a/README.md b/README.md index f61a7ee0..e29fb0b7 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ For more details on how to extend your configuration from a plugin configuration -| | Name | Description | -| :- | :------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| ✅ | `recommended` | This configuration includes rules which I recommend to avoid QUnit runtime errors or incorrect behavior, some of which can be difficult to debug. Some of these rules also encourage best practices that help QUnit work better for you. You can use this configuration by extending from `"plugin:qunit/recommended"` in your configuration file. | +| | Name | Description | +| :- | :------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| ✅ | `recommended` | This configuration includes rules which I recommend to avoid QUnit runtime errors or incorrect behavior, some of which can be difficult to debug. Some of these rules also encourage best practices that help QUnit work better for you. For ESLint `.eslintrc.js` legacy config, extend from `"plugin:qunit/recommended"`. For ESLint `eslint.config.js` flat config, load from `require('eslint-plugin-qunit/configs/recommended')`. | diff --git a/index.js b/index.js index 411b8300..0c14ff2b 100644 --- a/index.js +++ b/index.js @@ -8,14 +8,24 @@ "use strict"; const requireIndex = require("requireindex"); +const pkg = require("./package.json"); module.exports = { + meta: { + name: pkg.name, + version: pkg.version + }, + rules: requireIndex(`${__dirname}/lib/rules`), // eslint-disable-next-line sort-keys configs: { recommended: { - description: "This configuration includes rules which I recommend to avoid QUnit runtime errors or incorrect behavior, some of which can be difficult to debug. Some of these rules also encourage best practices that help QUnit work better for you. You can use this configuration by extending from `\"plugin:qunit/recommended\"` in your configuration file.", + description: [ + "This configuration includes rules which I recommend to avoid QUnit runtime errors or incorrect behavior, some of which can be difficult to debug. Some of these rules also encourage best practices that help QUnit work better for you.", + "For ESLint `.eslintrc.js` legacy config, extend from `\"plugin:qunit/recommended\"`.", + "For ESLint `eslint.config.js` flat config, load from `require('eslint-plugin-qunit/configs/recommended')`." + ].join(" "), plugins: ["qunit"], rules: { "qunit/assert-args": "error", diff --git a/lib/configs/recommended.js b/lib/configs/recommended.js new file mode 100644 index 00000000..e5c31c18 --- /dev/null +++ b/lib/configs/recommended.js @@ -0,0 +1,8 @@ +"use strict"; + +const plugin = require("../../index.js"); + +module.exports = { + plugins: { qunit: plugin }, + rules: plugin.configs.recommended.rules +}; diff --git a/package.json b/package.json index 68f03466..af1736b0 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,10 @@ "name": "eslint-plugin-qunit", "version": "8.0.1", "description": "ESLint plugin containing rules useful for QUnit tests.", - "exports": "./index.js", + "exports": { + ".": "./index.js", + "./configs/*": "./lib/configs/*.js" + }, "main": "./index.js", "scripts": { "lint": "npm-run-all --continue-on-error --aggregate-output --parallel lint:*", diff --git a/tests/index.js b/tests/index.js index f49c0910..b921b39f 100644 --- a/tests/index.js +++ b/tests/index.js @@ -11,7 +11,9 @@ const assert = require("chai").assert, { rules, configs } = require("../index"), fs = require("node:fs"), - path = require("node:path"); + path = require("node:path"), + requireIndex = require("requireindex"), + plugin = require("../index.js"); //------------------------------------------------------------------------------ // Tests @@ -59,13 +61,26 @@ describe("index.js", function () { }); describe("configs", function () { - // eslint-disable-next-line mocha/no-setup-in-describe -- rule doesn't like function calls like `Object.entries()` - for (const [configName, config] of Object.entries(configs)) { - describe(configName, function () { - it("has the right plugins", function () { - assert.deepStrictEqual(config.plugins, ["qunit"]); + describe("legacy", function () { + // eslint-disable-next-line mocha/no-setup-in-describe -- rule doesn't like function calls like `Object.entries()` + for (const [configName, config] of Object.entries(configs)) { + describe(configName, function () { + it("has the right plugins", function () { + assert.deepStrictEqual(config.plugins, ["qunit"]); + }); }); - }); - } + } + }); + + describe("flat", function () { + // eslint-disable-next-line mocha/no-setup-in-describe -- rule doesn't like function calls like `Object.entries()` + for (const [configName, config] of Object.entries(requireIndex(`${__dirname}/../lib/configs`))) { + describe(configName, function () { + it("has the right plugins", function () { + assert.deepStrictEqual(config.plugins, { qunit: plugin }); + }); + }); + } + }); }); });