From 0697f726986948ef06618ef12e4534c00d890362 Mon Sep 17 00:00:00 2001 From: lumir Date: Mon, 17 Aug 2026 21:24:28 +0900 Subject: [PATCH] fix: allow additional properties in `JSONLanguageOptions` --- src/languages/json-language.js | 3 +-- src/languages/json-source-code.js | 3 +-- src/types.ts | 15 +++++++++++++-- tests/types/types.test.ts | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/languages/json-language.js b/src/languages/json-language.js index 68f21951..a31ea092 100644 --- a/src/languages/json-language.js +++ b/src/languages/json-language.js @@ -18,10 +18,9 @@ import { visitorKeys } from "@humanwhocodes/momoa"; /** * @import { DocumentNode, AnyNode } from "@humanwhocodes/momoa"; * @import { Language, OkParseResult, ParseResult, File } from "@eslint/core"; + * @import { JSONLanguageOptions } from "../types.js"; * @typedef {OkParseResult} JSONOkParseResult * @typedef {ParseResult} JSONParseResult - * @typedef {Object} JSONLanguageOptions - * @property {boolean} [allowTrailingCommas] Whether to allow trailing commas in JSONC mode. */ //----------------------------------------------------------------------------- diff --git a/src/languages/json-source-code.js b/src/languages/json-source-code.js index 2472cfd7..4d8e6b27 100644 --- a/src/languages/json-source-code.js +++ b/src/languages/json-source-code.js @@ -22,8 +22,7 @@ import { /** * @import { DocumentNode, AnyNode, Token, LocationRange } from "@humanwhocodes/momoa"; * @import { FileProblem, DirectiveType, RulesConfig } from "@eslint/core"; - * @import { JSONSyntaxElement } from "../types.js"; - * @import { JSONLanguageOptions } from "./json-language.js"; + * @import { JSONLanguageOptions, JSONSyntaxElement } from "../types.js"; */ //----------------------------------------------------------------------------- diff --git a/src/types.ts b/src/types.ts index 83c56b53..221d3d70 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,7 +7,7 @@ // Imports //------------------------------------------------------------------------------ -import type { RuleVisitor } from "@eslint/core"; +import type { LanguageOptions, RuleVisitor } from "@eslint/core"; import type { CustomRuleDefinitionType, CustomRuleTypeDefinitions, @@ -29,7 +29,7 @@ import type { AnyNode, Token, } from "@humanwhocodes/momoa"; -import type { JSONLanguageOptions, JSONSourceCode } from "./index.js"; +import type { JSONSourceCode } from "./index.js"; //------------------------------------------------------------------------------ // Types @@ -37,6 +37,17 @@ import type { JSONLanguageOptions, JSONSourceCode } from "./index.js"; type ValueNodeParent = DocumentNode | MemberNode | ElementNode; +/** + * Language options provided for JSON files. + */ +export interface JSONLanguageOptions extends LanguageOptions { + /** + * Whether to allow trailing commas in JSONC mode. + * @default false + */ + allowTrailingCommas?: boolean; +} + /** * A JSON syntax element, including nodes and tokens. */ diff --git a/tests/types/types.test.ts b/tests/types/types.test.ts index a858a988..9ae29be3 100644 --- a/tests/types/types.test.ts +++ b/tests/types/types.test.ts @@ -1,5 +1,6 @@ import json from "@eslint/json"; import type { + JSONLanguageOptions, JSONRuleDefinition, JSONRuleVisitor, JSONSourceCode, @@ -75,6 +76,23 @@ json.configs.recommended.plugins satisfies object; end: { line: 1, column: 1, offset: 1 }, }) satisfies JSONSyntaxElement["loc"]; +const validLanguageOptions1: JSONLanguageOptions = {}; +const validLanguageOptions2: JSONLanguageOptions = { + allowTrailingCommas: true, +}; +const validLanguageOptions3: JSONLanguageOptions = { + allowTrailingCommas: false, +}; +const validLanguageOptions4: JSONLanguageOptions = { + allowTrailingCommas: true, + unknownOption: "unknown", +}; + +const invalidLanguageOptions1: JSONLanguageOptions = { + // @ts-expect-error -- Invalid value for `allowTrailingCommas` + allowTrailingCommas: "true", +}; + (): JSONRuleDefinition => ({ create({ sourceCode }): JSONRuleVisitor { sourceCode satisfies JSONSourceCode;