|
| 1 | +import assert from "node:assert"; |
| 2 | +import { Linter } from "eslint"; |
| 3 | +import plugin from "../../../lib/index.ts"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Creates a config array for testing with the specified jsonSyntax and rules. |
| 7 | + */ |
| 8 | +function createConfig( |
| 9 | + jsonSyntax: "JSON" | "JSONC" | "JSON5" | null | undefined, |
| 10 | + rules: Linter.RulesRecord = {}, |
| 11 | +): Linter.Config[] { |
| 12 | + return [ |
| 13 | + { |
| 14 | + files: ["**/*.json", "**/*.jsonc", "**/*.json5"], |
| 15 | + plugins: { jsonc: plugin }, |
| 16 | + language: "jsonc/x", |
| 17 | + languageOptions: |
| 18 | + jsonSyntax != null ? { parserOptions: { jsonSyntax } } : {}, |
| 19 | + rules, |
| 20 | + }, |
| 21 | + ]; |
| 22 | +} |
| 23 | + |
| 24 | +describe("JSONC Language", () => { |
| 25 | + let linter: Linter; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + linter = new Linter(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("Normal", () => { |
| 32 | + it("should not have parse errors for valid JSON", () => { |
| 33 | + const code = `{"key": "value"}`; |
| 34 | + const messages = linter.verify(code, createConfig("JSON"), "test.json"); |
| 35 | + |
| 36 | + assert.deepStrictEqual(messages, []); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("Errors", () => { |
| 41 | + it("should have parse errors for invalid JSON", () => { |
| 42 | + const code = `{"key": "value",}`; |
| 43 | + const messages = linter.verify(code, createConfig("JSON"), "test.json"); |
| 44 | + |
| 45 | + assert.deepStrictEqual(messages, [ |
| 46 | + { |
| 47 | + fatal: true, |
| 48 | + message: "Parsing error: Unexpected token ','.", |
| 49 | + line: 1, |
| 50 | + column: 16, |
| 51 | + ruleId: null, |
| 52 | + severity: 2, |
| 53 | + }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments