Skip to content
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"prettier": "^3.4.1",
"rollup": "^4.16.2",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-delete": "^3.0.1",
"typescript": "^5.4.5",
"yorkie": "^2.0.0"
},
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy from "rollup-plugin-copy";
import del from "rollup-plugin-delete";

export default {
input: "src/index.js",
Expand All @@ -14,6 +15,7 @@ export default {
},
],
plugins: [
del({ targets: "dist/*" }),
copy({
targets: [
{ src: "src/types.ts", dest: "dist/cjs", rename: "types.cts" },
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ const plugin = {
}

export default plugin;
export { JSONLanguage, JSONSourceCode };
export { JSONSourceCode };
export * from "./languages/json-language.js";
Comment on lines -60 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? I don't see any diff in dist/ this change is causing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because JSONLanguageOptions type is defined in json-language.js, it can only be exported from a JS file using *. (If this were a .ts file we could export it directly.)

24 changes: 13 additions & 11 deletions src/languages/json-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ import { visitorKeys } from "@humanwhocodes/momoa";
// Types
//-----------------------------------------------------------------------------

/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
/** @typedef {import("@eslint/core").Language} Language */
/** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
/** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
/** @typedef {import("@eslint/core").File} File */
/** @typedef {import("../types.ts").IJSONLanguage} IJSONLanguage */
/** @typedef {import("../types.ts").JSONLanguageOptions} JSONLanguageOptions */
/**
* @import { DocumentNode, AnyNode } from "@humanwhocodes/momoa";
* @import { Language, OkParseResult, ParseResult, File } from "@eslint/core";
*
* @typedef {OkParseResult<DocumentNode>} JSONOkParseResult
* @typedef {ParseResult<DocumentNode>} JSONParseResult
*
* @typedef {Object} JSONLanguageOptions
* @property {boolean} [allowTrailingCommas] Whether to allow trailing commas in JSONC mode.
*/

//-----------------------------------------------------------------------------
// Exports
//-----------------------------------------------------------------------------

/**
* JSON Language Object
* @implements {IJSONLanguage}
* @implements {Language<{ LangOptions: JSONLanguageOptions; Code: JSONSourceCode; RootNode: DocumentNode; Node: AnyNode }>}
*/
export class JSONLanguage {
/**
Expand Down Expand Up @@ -107,7 +109,7 @@ export class JSONLanguage {
* Parses the given file into an AST.
* @param {File} file The virtual file to parse.
* @param {{languageOptions: JSONLanguageOptions}} context The options to use for parsing.
* @returns {ParseResult} The result of parsing.
* @returns {JSONParseResult} The result of parsing.
*/
parse(file, context) {
// Note: BOM already removed
Expand Down Expand Up @@ -155,7 +157,7 @@ export class JSONLanguage {
/**
* Creates a new `JSONSourceCode` object from the given information.
* @param {File} file The virtual file to create a `JSONSourceCode` object from.
* @param {OkParseResult} parseResult The result returned from `parse()`.
* @param {JSONOkParseResult} parseResult The result returned from `parse()`.
* @returns {JSONSourceCode} The new `JSONSourceCode` object.
*/
createSourceCode(file, parseResult) {
Expand Down
39 changes: 16 additions & 23 deletions src/languages/json-source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,12 @@ import {
// Types
//-----------------------------------------------------------------------------

/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
/** @typedef {import("@humanwhocodes/momoa").Token} JSONToken */
/** @typedef {import("@eslint/core").SourceRange} SourceRange */
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
/** @typedef {import("@eslint/core").File} File */
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
/** @typedef {import("@eslint/core").FileProblem} FileProblem */
/** @typedef {import("@eslint/core").DirectiveType} DirectiveType */
/** @typedef {import("@eslint/core").RulesConfig} RulesConfig */
/** @typedef {import("../types.ts").IJSONSourceCode} IJSONSourceCode */
/** @typedef {import("../types.ts").JSONSyntaxElement} JSONSyntaxElement */
/**
* @import { DocumentNode, Node, Token } from "@humanwhocodes/momoa";
* @import { SourceLocation, FileProblem, DirectiveType, RulesConfig, TextSourceCode} from "@eslint/core";
* @import { JSONSyntaxElement } from "../types.ts";
* @import { JSONLanguageOptions } from "./json-language.js";
*/

//-----------------------------------------------------------------------------
// Helpers
Expand All @@ -48,14 +41,14 @@ const INLINE_CONFIG =
class JSONTraversalStep extends VisitNodeStep {
/**
* The target of the step.
* @type {JSONNode}
* @type {Node}
*/
target = undefined;

/**
* Creates a new instance.
* @param {Object} options The options for the step.
* @param {JSONNode} options.target The target of the step.
* @param {Node} options.target The target of the step.
* @param {1|2} options.phase The phase of the step.
* @param {Array<any>} options.args The arguments of the step.
*/
Expand All @@ -72,7 +65,7 @@ class JSONTraversalStep extends VisitNodeStep {

/**
* JSON Source Code Object
* @implements {IJSONSourceCode}
* @implements {TextSourceCode<{LangOptions: JSONLanguageOptions, RootNode: DocumentNode, SyntaxElementWithLoc: JSONSyntaxElement, ConfigNode: Token}>}
*/
export class JSONSourceCode extends TextSourceCodeBase {
/**
Expand All @@ -83,13 +76,13 @@ export class JSONSourceCode extends TextSourceCodeBase {

/**
* Cache of parent nodes.
* @type {WeakMap<JSONNode, JSONNode>}
* @type {WeakMap<Node, Node>}
*/
#parents = new WeakMap();

/**
* Collection of inline configuration comments.
* @type {Array<JSONToken>}
* @type {Array<Token>}
*/
#inlineConfigComments;

Expand All @@ -101,7 +94,7 @@ export class JSONSourceCode extends TextSourceCodeBase {

/**
* The comment node in the source code.
* @type {Array<JSONToken>|undefined}
* @type {Array<Token>|undefined}
*/
comments;

Expand All @@ -121,7 +114,7 @@ export class JSONSourceCode extends TextSourceCodeBase {

/**
* Returns the value of the given comment.
* @param {JSONToken} comment The comment to get the value of.
* @param {Token} comment The comment to get the value of.
* @returns {string} The value of the comment.
* @throws {Error} When an unexpected comment type is passed.
*/
Expand All @@ -140,7 +133,7 @@ export class JSONSourceCode extends TextSourceCodeBase {
/**
* Returns an array of all inline configuration nodes found in the
* source code.
* @returns {Array<JSONToken>} An array of all inline configuration nodes.
* @returns {Array<Token>} An array of all inline configuration nodes.
*/
getInlineConfigNodes() {
if (!this.#inlineConfigComments) {
Expand Down Expand Up @@ -251,8 +244,8 @@ export class JSONSourceCode extends TextSourceCodeBase {

/**
* Returns the parent of the given node.
* @param {JSONNode} node The node to get the parent of.
* @returns {JSONNode|undefined} The parent of the node.
* @param {Node} node The node to get the parent of.
* @returns {Node|undefined} The parent of the node.
*/
getParent(node) {
return this.#parents.get(node);
Expand Down
10 changes: 7 additions & 3 deletions src/rules/no-duplicate-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {"duplicateKey"} NoDuplicateKeysMessageIds */
/** @typedef {import("../types.ts").JSONRuleDefinition<[], NoDuplicateKeysMessageIds>} NoDuplicateKeysRuleDefinition */
/** @typedef {import("@humanwhocodes/momoa").MemberNode} MemberNode */
/**
* @import { MemberNode } from "@humanwhocodes/momoa";
* @import { JSONRuleDefinition } from "../types.ts";
*
* @typedef {"duplicateKey"} NoDuplicateKeysMessageIds
* @typedef {JSONRuleDefinition<{ MessageIds: NoDuplicateKeysMessageIds }>} NoDuplicateKeysRuleDefinition
*/

//-----------------------------------------------------------------------------
// Rule Definition
Expand Down
8 changes: 6 additions & 2 deletions src/rules/no-empty-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {"emptyKey"} NoEmptyKeysMessageIds */
/** @typedef {import("../types.ts").JSONRuleDefinition<[], NoEmptyKeysMessageIds>} NoEmptyKeysRuleDefinition */
/**
* @import { JSONRuleDefinition } from "../types.ts";
*
* @typedef {"emptyKey"} NoEmptyKeysMessageIds
* @typedef {JSONRuleDefinition<{ MessageIds: NoEmptyKeysMessageIds }>} NoEmptyKeysRuleDefinition
*/

//-----------------------------------------------------------------------------
// Rule Definition
Expand Down
9 changes: 7 additions & 2 deletions src/rules/no-unnormalized-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {"unnormalizedKey"} NoUnnormalizedKeysMessageIds */
/** @typedef {import("../types.ts").JSONRuleDefinition<[{form:string}], NoUnnormalizedKeysMessageIds>} NoUnnormalizedKeysRuleDefinition */
/**
* @import { JSONRuleDefinition } from "../types.ts";
*
* @typedef {"unnormalizedKey"} NoUnnormalizedKeysMessageIds
* @typedef {{ form: string }} NoUnnormalizedKeysOptions
* @typedef {JSONRuleDefinition<{ RuleOptions: [NoUnnormalizedKeysOptions], MessageIds: NoUnnormalizedKeysMessageIds }>} NoUnnormalizedKeysRuleDefinition
*/

//-----------------------------------------------------------------------------
// Rule Definition
Expand Down
8 changes: 6 additions & 2 deletions src/rules/no-unsafe-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {"unsafeNumber"|"unsafeInteger"|"unsafeZero"|"subnormal"|"loneSurrogate"} NoUnsafeValuesMessageIds */
/** @typedef {import("../types.ts").JSONRuleDefinition<[], NoUnsafeValuesMessageIds>} NoUnsafeValuesRuleDefinition */
/**
* @import { JSONRuleDefinition } from "../types.ts";
*
* @typedef {"unsafeNumber"|"unsafeInteger"|"unsafeZero"|"subnormal"|"loneSurrogate"} NoUnsafeValuesMessageIds
* @typedef {JSONRuleDefinition<{ MessageIds: NoUnsafeValuesMessageIds }>} NoUnsafeValuesRuleDefinition
*/

//-----------------------------------------------------------------------------
// Helpers
Expand Down
17 changes: 9 additions & 8 deletions src/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import naturalCompare from "natural-compare";
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {"sortKeys"} SortKeysMessageIds */

/**
* @import { JSONRuleDefinition } from "../types.ts";
* @import { MemberNode } from "@humanwhocodes/momoa";
*
* @typedef {Object} SortOptions
* @property {boolean} caseSensitive
* @property {boolean} natural
* @property {number} minKeys
* @property {boolean} allowLineSeparatedGroups
*
* @typedef {"sortKeys"} SortKeysMessageIds
* @typedef {"asc"|"desc"} SortDirection
* @typedef {[SortDirection, SortOptions]} SortKeysRuleOptions
* @typedef {JSONRuleDefinition<{ RuleOptions: SortKeysRuleOptions, MessageIds: SortKeysMessageIds }>} SortKeysRuleDefinition
* @typedef {(a:string,b:string) => boolean} Comparator
*/

/** @typedef {"asc"|"desc"} SortDirection */
/** @typedef {[SortDirection, SortOptions]} SortKeysRuleOptions */
/** @typedef {import("../types.ts").JSONRuleDefinition<SortKeysRuleOptions, SortKeysMessageIds>} SortKeysRuleDefinition */
/** @typedef {(a:string,b:string) => boolean} Comparator */
/** @typedef {import("@humanwhocodes/momoa").MemberNode} MemberNode */

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions src/rules/top-level-interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {"topLevel"} TopLevelInteropMessageIds */
/** @typedef {import("../types.ts").JSONRuleDefinition<[], TopLevelInteropMessageIds>} TopLevelInteropRuleDefinition */
/**
* @import { JSONRuleDefinition } from "../types.ts";
*
* @typedef {"topLevel"} TopLevelInteropMessageIds
* @typedef {JSONRuleDefinition<{ MessageIds: TopLevelInteropMessageIds }>} TopLevelInteropRuleDefinition
*/

//-----------------------------------------------------------------------------
// Rule Definition
Expand Down
Loading