Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/languages/json-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode>} JSONOkParseResult
* @typedef {ParseResult<DocumentNode>} JSONParseResult
* @typedef {Object} JSONLanguageOptions
* @property {boolean} [allowTrailingCommas] Whether to allow trailing commas in JSONC mode.
*/

//-----------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions src/languages/json-source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
*/

//-----------------------------------------------------------------------------
Expand Down
15 changes: 13 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Imports
//------------------------------------------------------------------------------

import type { RuleVisitor } from "@eslint/core";
import type { LanguageOptions, RuleVisitor } from "@eslint/core";
import type {
CustomRuleDefinitionType,
CustomRuleTypeDefinitions,
Expand All @@ -29,14 +29,25 @@ import type {
AnyNode,
Token,
} from "@humanwhocodes/momoa";
import type { JSONLanguageOptions, JSONSourceCode } from "./index.js";
import type { JSONSourceCode } from "./index.js";

//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------

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.
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/types/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json from "@eslint/json";
import type {
JSONLanguageOptions,
JSONRuleDefinition,
JSONRuleVisitor,
JSONSourceCode,
Expand Down Expand Up @@ -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;
Expand Down