From 36b9fc7aaa934883dc37c059ef02f1e9ba24ddb5 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 19 Mar 2024 06:59:29 +0900 Subject: [PATCH] fix: flat config issues (#315) --- .changeset/rude-plums-relate.md | 5 +++ lib/configs/flat/base.ts | 14 +++++---- lib/index.ts | 4 +-- tests/lib/configs/flat/base.ts | 36 ++++++++++++++++++++++ tests/lib/configs/recommended-with-json.ts | 12 ++++++++ 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 .changeset/rude-plums-relate.md create mode 100644 tests/lib/configs/flat/base.ts diff --git a/.changeset/rude-plums-relate.md b/.changeset/rude-plums-relate.md new file mode 100644 index 00000000..8c9b1cfb --- /dev/null +++ b/.changeset/rude-plums-relate.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-jsonc": patch +--- + +fix: flat config issues diff --git a/lib/configs/flat/base.ts b/lib/configs/flat/base.ts index 9f75848e..7fee4ee9 100644 --- a/lib/configs/flat/base.ts +++ b/lib/configs/flat/base.ts @@ -1,6 +1,14 @@ import type { ESLint } from "eslint"; import * as parser from "jsonc-eslint-parser"; export default [ + { + plugins: { + get jsonc(): ESLint.Plugin { + // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore + return require("../../index"); + }, + }, + }, { files: [ "*.json", @@ -10,12 +18,6 @@ export default [ "*.jsonc", "**/*.jsonc", ], - plugins: { - get jsonc(): ESLint.Plugin { - // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore - return require("../../index"); - }, - }, languageOptions: { parser, }, diff --git a/lib/index.ts b/lib/index.ts index e22757fb..2ba7fe8b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -7,12 +7,12 @@ import recommendedWithJsonc from "./configs/recommended-with-jsonc"; import recommendedWithJson5 from "./configs/recommended-with-json5"; import prettier from "./configs/prettier"; import all from "./configs/all"; -import flatBase from "./configs/base"; +import flatBase from "./configs/flat/base"; import flatRecommendedWithJson from "./configs/flat/recommended-with-json"; import flatRecommendedWithJsonc from "./configs/flat/recommended-with-jsonc"; import flatRecommendedWithJson5 from "./configs/flat/recommended-with-json5"; import flatPrettier from "./configs/flat/prettier"; -import flatAll from "./configs/all"; +import flatAll from "./configs/flat/all"; import * as meta from "./meta"; // backward compatibility diff --git a/tests/lib/configs/flat/base.ts b/tests/lib/configs/flat/base.ts new file mode 100644 index 00000000..1f249b1a --- /dev/null +++ b/tests/lib/configs/flat/base.ts @@ -0,0 +1,36 @@ +import assert from "assert"; +import plugin from "../../../../lib/index"; +import { ESLint } from "../../test-lib/eslint-compat"; + +const code = `{ foo: 42 }`; +describe("`flat/base` config", () => { + it("`flat/base` config should work. ", async () => { + const linter = new ESLint({ + overrideConfigFile: true as never, + overrideConfig: plugin.configs["flat/base"] as never, + }); + const result = await linter.lintText(code, { filePath: "test.json" }); + const messages = result[0].messages; + + assert.deepStrictEqual( + messages.map((m) => ({ + ruleId: m.ruleId, + line: m.line, + message: m.message, + })), + [], + ); + + const resultWithJs = await linter.lintText(";", { filePath: "test.js" }); + const messagesWithJs = resultWithJs[0].messages; + + assert.deepStrictEqual( + messagesWithJs.map((m) => ({ + ruleId: m.ruleId, + line: m.line, + message: m.message, + })), + [], + ); + }); +}); diff --git a/tests/lib/configs/recommended-with-json.ts b/tests/lib/configs/recommended-with-json.ts index e5dd44aa..47940207 100644 --- a/tests/lib/configs/recommended-with-json.ts +++ b/tests/lib/configs/recommended-with-json.ts @@ -57,5 +57,17 @@ describe("`recommended-with-json` config", () => { }, ], ); + + const resultWithJs = await linter.lintText(";", { filePath: "test.js" }); + const messagesWithJs = resultWithJs[0].messages; + + assert.deepStrictEqual( + messagesWithJs.map((m) => ({ + ruleId: m.ruleId, + line: m.line, + message: m.message, + })), + [], + ); }); });