Skip to content

Commit dce4601

Browse files
nzakasfasttime
andauthored
feat!: Update package types for better reuse (#91)
* feat!: Update package types for better reuse * Fix type cleanup * Fix build * Fix CommonJS types * Update tools/dedupe-types.js Co-authored-by: Francesco Trotta <[email protected]> --------- Co-authored-by: Francesco Trotta <[email protected]>
1 parent 897f3b5 commit dce4601

14 files changed

+148
-133
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"prettier": "^3.4.1",
9191
"rollup": "^4.16.2",
9292
"rollup-plugin-copy": "^3.5.0",
93+
"rollup-plugin-delete": "^3.0.1",
9394
"typescript": "^5.4.5",
9495
"yorkie": "^2.0.0"
9596
},

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy from "rollup-plugin-copy";
2+
import del from "rollup-plugin-delete";
23

34
export default {
45
input: "src/index.js",
@@ -14,6 +15,7 @@ export default {
1415
},
1516
],
1617
plugins: [
18+
del({ targets: "dist/*" }),
1719
copy({
1820
targets: [
1921
{ src: "src/types.ts", dest: "dist/cjs", rename: "types.cts" },

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ const plugin = {
5757
}
5858

5959
export default plugin;
60-
export { JSONLanguage, JSONSourceCode };
60+
export { JSONSourceCode };
61+
export * from "./languages/json-language.js";

src/languages/json-language.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ import { visitorKeys } from "@humanwhocodes/momoa";
1515
// Types
1616
//-----------------------------------------------------------------------------
1717

18-
/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
19-
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
20-
/** @typedef {import("@eslint/core").Language} Language */
21-
/** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
22-
/** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
23-
/** @typedef {import("@eslint/core").File} File */
24-
/** @typedef {import("../types.ts").IJSONLanguage} IJSONLanguage */
25-
/** @typedef {import("../types.ts").JSONLanguageOptions} JSONLanguageOptions */
18+
/**
19+
* @import { DocumentNode, AnyNode } from "@humanwhocodes/momoa";
20+
* @import { Language, OkParseResult, ParseResult, File } from "@eslint/core";
21+
*
22+
* @typedef {OkParseResult<DocumentNode>} JSONOkParseResult
23+
* @typedef {ParseResult<DocumentNode>} JSONParseResult
24+
*
25+
* @typedef {Object} JSONLanguageOptions
26+
* @property {boolean} [allowTrailingCommas] Whether to allow trailing commas in JSONC mode.
27+
*/
2628

2729
//-----------------------------------------------------------------------------
2830
// Exports
2931
//-----------------------------------------------------------------------------
3032

3133
/**
3234
* JSON Language Object
33-
* @implements {IJSONLanguage}
35+
* @implements {Language<{ LangOptions: JSONLanguageOptions; Code: JSONSourceCode; RootNode: DocumentNode; Node: AnyNode }>}
3436
*/
3537
export class JSONLanguage {
3638
/**
@@ -107,7 +109,7 @@ export class JSONLanguage {
107109
* Parses the given file into an AST.
108110
* @param {File} file The virtual file to parse.
109111
* @param {{languageOptions: JSONLanguageOptions}} context The options to use for parsing.
110-
* @returns {ParseResult} The result of parsing.
112+
* @returns {JSONParseResult} The result of parsing.
111113
*/
112114
parse(file, context) {
113115
// Note: BOM already removed
@@ -155,7 +157,7 @@ export class JSONLanguage {
155157
/**
156158
* Creates a new `JSONSourceCode` object from the given information.
157159
* @param {File} file The virtual file to create a `JSONSourceCode` object from.
158-
* @param {OkParseResult} parseResult The result returned from `parse()`.
160+
* @param {JSONOkParseResult} parseResult The result returned from `parse()`.
159161
* @returns {JSONSourceCode} The new `JSONSourceCode` object.
160162
*/
161163
createSourceCode(file, parseResult) {

src/languages/json-source-code.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ import {
1919
// Types
2020
//-----------------------------------------------------------------------------
2121

22-
/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
23-
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
24-
/** @typedef {import("@humanwhocodes/momoa").Token} JSONToken */
25-
/** @typedef {import("@eslint/core").SourceRange} SourceRange */
26-
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
27-
/** @typedef {import("@eslint/core").File} File */
28-
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
29-
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
30-
/** @typedef {import("@eslint/core").FileProblem} FileProblem */
31-
/** @typedef {import("@eslint/core").DirectiveType} DirectiveType */
32-
/** @typedef {import("@eslint/core").RulesConfig} RulesConfig */
33-
/** @typedef {import("../types.ts").IJSONSourceCode} IJSONSourceCode */
34-
/** @typedef {import("../types.ts").JSONSyntaxElement} JSONSyntaxElement */
22+
/**
23+
* @import { DocumentNode, Node, Token } from "@humanwhocodes/momoa";
24+
* @import { SourceLocation, FileProblem, DirectiveType, RulesConfig, TextSourceCode} from "@eslint/core";
25+
* @import { JSONSyntaxElement } from "../types.ts";
26+
* @import { JSONLanguageOptions } from "./json-language.js";
27+
*/
3528

3629
//-----------------------------------------------------------------------------
3730
// Helpers
@@ -48,14 +41,14 @@ const INLINE_CONFIG =
4841
class JSONTraversalStep extends VisitNodeStep {
4942
/**
5043
* The target of the step.
51-
* @type {JSONNode}
44+
* @type {Node}
5245
*/
5346
target = undefined;
5447

5548
/**
5649
* Creates a new instance.
5750
* @param {Object} options The options for the step.
58-
* @param {JSONNode} options.target The target of the step.
51+
* @param {Node} options.target The target of the step.
5952
* @param {1|2} options.phase The phase of the step.
6053
* @param {Array<any>} options.args The arguments of the step.
6154
*/
@@ -72,7 +65,7 @@ class JSONTraversalStep extends VisitNodeStep {
7265

7366
/**
7467
* JSON Source Code Object
75-
* @implements {IJSONSourceCode}
68+
* @implements {TextSourceCode<{LangOptions: JSONLanguageOptions, RootNode: DocumentNode, SyntaxElementWithLoc: JSONSyntaxElement, ConfigNode: Token}>}
7669
*/
7770
export class JSONSourceCode extends TextSourceCodeBase {
7871
/**
@@ -83,13 +76,13 @@ export class JSONSourceCode extends TextSourceCodeBase {
8376

8477
/**
8578
* Cache of parent nodes.
86-
* @type {WeakMap<JSONNode, JSONNode>}
79+
* @type {WeakMap<Node, Node>}
8780
*/
8881
#parents = new WeakMap();
8982

9083
/**
9184
* Collection of inline configuration comments.
92-
* @type {Array<JSONToken>}
85+
* @type {Array<Token>}
9386
*/
9487
#inlineConfigComments;
9588

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

10295
/**
10396
* The comment node in the source code.
104-
* @type {Array<JSONToken>|undefined}
97+
* @type {Array<Token>|undefined}
10598
*/
10699
comments;
107100

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

122115
/**
123116
* Returns the value of the given comment.
124-
* @param {JSONToken} comment The comment to get the value of.
117+
* @param {Token} comment The comment to get the value of.
125118
* @returns {string} The value of the comment.
126119
* @throws {Error} When an unexpected comment type is passed.
127120
*/
@@ -140,7 +133,7 @@ export class JSONSourceCode extends TextSourceCodeBase {
140133
/**
141134
* Returns an array of all inline configuration nodes found in the
142135
* source code.
143-
* @returns {Array<JSONToken>} An array of all inline configuration nodes.
136+
* @returns {Array<Token>} An array of all inline configuration nodes.
144137
*/
145138
getInlineConfigNodes() {
146139
if (!this.#inlineConfigComments) {
@@ -251,8 +244,8 @@ export class JSONSourceCode extends TextSourceCodeBase {
251244

252245
/**
253246
* Returns the parent of the given node.
254-
* @param {JSONNode} node The node to get the parent of.
255-
* @returns {JSONNode|undefined} The parent of the node.
247+
* @param {Node} node The node to get the parent of.
248+
* @returns {Node|undefined} The parent of the node.
256249
*/
257250
getParent(node) {
258251
return this.#parents.get(node);

src/rules/no-duplicate-keys.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
// Type Definitions
88
//-----------------------------------------------------------------------------
99

10-
/** @typedef {"duplicateKey"} NoDuplicateKeysMessageIds */
11-
/** @typedef {import("../types.ts").JSONRuleDefinition<[], NoDuplicateKeysMessageIds>} NoDuplicateKeysRuleDefinition */
12-
/** @typedef {import("@humanwhocodes/momoa").MemberNode} MemberNode */
10+
/**
11+
* @import { MemberNode } from "@humanwhocodes/momoa";
12+
* @import { JSONRuleDefinition } from "../types.ts";
13+
*
14+
* @typedef {"duplicateKey"} NoDuplicateKeysMessageIds
15+
* @typedef {JSONRuleDefinition<{ MessageIds: NoDuplicateKeysMessageIds }>} NoDuplicateKeysRuleDefinition
16+
*/
1317

1418
//-----------------------------------------------------------------------------
1519
// Rule Definition

src/rules/no-empty-keys.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
// Type Definitions
88
//-----------------------------------------------------------------------------
99

10-
/** @typedef {"emptyKey"} NoEmptyKeysMessageIds */
11-
/** @typedef {import("../types.ts").JSONRuleDefinition<[], NoEmptyKeysMessageIds>} NoEmptyKeysRuleDefinition */
10+
/**
11+
* @import { JSONRuleDefinition } from "../types.ts";
12+
*
13+
* @typedef {"emptyKey"} NoEmptyKeysMessageIds
14+
* @typedef {JSONRuleDefinition<{ MessageIds: NoEmptyKeysMessageIds }>} NoEmptyKeysRuleDefinition
15+
*/
1216

1317
//-----------------------------------------------------------------------------
1418
// Rule Definition

src/rules/no-unnormalized-keys.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
// Type Definitions
88
//-----------------------------------------------------------------------------
99

10-
/** @typedef {"unnormalizedKey"} NoUnnormalizedKeysMessageIds */
11-
/** @typedef {import("../types.ts").JSONRuleDefinition<[{form:string}], NoUnnormalizedKeysMessageIds>} NoUnnormalizedKeysRuleDefinition */
10+
/**
11+
* @import { JSONRuleDefinition } from "../types.ts";
12+
*
13+
* @typedef {"unnormalizedKey"} NoUnnormalizedKeysMessageIds
14+
* @typedef {{ form: string }} NoUnnormalizedKeysOptions
15+
* @typedef {JSONRuleDefinition<{ RuleOptions: [NoUnnormalizedKeysOptions], MessageIds: NoUnnormalizedKeysMessageIds }>} NoUnnormalizedKeysRuleDefinition
16+
*/
1217

1318
//-----------------------------------------------------------------------------
1419
// Rule Definition

src/rules/no-unsafe-values.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
// Type Definitions
88
//-----------------------------------------------------------------------------
99

10-
/** @typedef {"unsafeNumber"|"unsafeInteger"|"unsafeZero"|"subnormal"|"loneSurrogate"} NoUnsafeValuesMessageIds */
11-
/** @typedef {import("../types.ts").JSONRuleDefinition<[], NoUnsafeValuesMessageIds>} NoUnsafeValuesRuleDefinition */
10+
/**
11+
* @import { JSONRuleDefinition } from "../types.ts";
12+
*
13+
* @typedef {"unsafeNumber"|"unsafeInteger"|"unsafeZero"|"subnormal"|"loneSurrogate"} NoUnsafeValuesMessageIds
14+
* @typedef {JSONRuleDefinition<{ MessageIds: NoUnsafeValuesMessageIds }>} NoUnsafeValuesRuleDefinition
15+
*/
1216

1317
//-----------------------------------------------------------------------------
1418
// Helpers

src/rules/sort-keys.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ import naturalCompare from "natural-compare";
1414
// Type Definitions
1515
//-----------------------------------------------------------------------------
1616

17-
/** @typedef {"sortKeys"} SortKeysMessageIds */
18-
1917
/**
18+
* @import { JSONRuleDefinition } from "../types.ts";
19+
* @import { MemberNode } from "@humanwhocodes/momoa";
20+
*
2021
* @typedef {Object} SortOptions
2122
* @property {boolean} caseSensitive
2223
* @property {boolean} natural
2324
* @property {number} minKeys
2425
* @property {boolean} allowLineSeparatedGroups
26+
*
27+
* @typedef {"sortKeys"} SortKeysMessageIds
28+
* @typedef {"asc"|"desc"} SortDirection
29+
* @typedef {[SortDirection, SortOptions]} SortKeysRuleOptions
30+
* @typedef {JSONRuleDefinition<{ RuleOptions: SortKeysRuleOptions, MessageIds: SortKeysMessageIds }>} SortKeysRuleDefinition
31+
* @typedef {(a:string,b:string) => boolean} Comparator
2532
*/
2633

27-
/** @typedef {"asc"|"desc"} SortDirection */
28-
/** @typedef {[SortDirection, SortOptions]} SortKeysRuleOptions */
29-
/** @typedef {import("../types.ts").JSONRuleDefinition<SortKeysRuleOptions, SortKeysMessageIds>} SortKeysRuleDefinition */
30-
/** @typedef {(a:string,b:string) => boolean} Comparator */
31-
/** @typedef {import("@humanwhocodes/momoa").MemberNode} MemberNode */
32-
3334
//-----------------------------------------------------------------------------
3435
// Helpers
3536
//-----------------------------------------------------------------------------

src/rules/top-level-interop.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
// Type Definitions
88
//-----------------------------------------------------------------------------
99

10-
/** @typedef {"topLevel"} TopLevelInteropMessageIds */
11-
/** @typedef {import("../types.ts").JSONRuleDefinition<[], TopLevelInteropMessageIds>} TopLevelInteropRuleDefinition */
10+
/**
11+
* @import { JSONRuleDefinition } from "../types.ts";
12+
*
13+
* @typedef {"topLevel"} TopLevelInteropMessageIds
14+
* @typedef {JSONRuleDefinition<{ MessageIds: TopLevelInteropMessageIds }>} TopLevelInteropRuleDefinition
15+
*/
1216

1317
//-----------------------------------------------------------------------------
1418
// Rule Definition

0 commit comments

Comments
 (0)