Skip to content

Commit

Permalink
fix: flat config issues (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 18, 2024
1 parent 016fa43 commit 36b9fc7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-plums-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": patch
---

fix: flat config issues
14 changes: 8 additions & 6 deletions lib/configs/flat/base.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/configs/flat/base.ts
Original file line number Diff line number Diff line change
@@ -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,
})),
[],
);
});
});
12 changes: 12 additions & 0 deletions tests/lib/configs/recommended-with-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})),
[],
);
});
});

0 comments on commit 36b9fc7

Please sign in to comment.