Skip to content

Commit 90396b4

Browse files
committed
feat: Update to new types
1 parent 9283c10 commit 90396b4

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

rollup.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export default {
1515
],
1616
plugins: [
1717
copy({
18-
targets: [{ src: "src/types.ts", dest: "dist/esm" }],
18+
targets: [
19+
{ src: "src/types.ts", dest: "dist/cjs" },
20+
{ src: "src/types.ts", dest: "dist/esm" },
21+
],
1922
}),
2023
],
2124
};

src/languages/json-language.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ import { visitorKeys } from "@humanwhocodes/momoa";
2525
* @typedef {Object} JSONLanguageOptions
2626
* @property {boolean} [allowTrailingCommas] Whether to allow trailing commas.
2727
*/
28+
/** @typedef {import("../types.ts").IJSONLanguage} IJSONLanguage */
2829

2930
//-----------------------------------------------------------------------------
3031
// Exports
3132
//-----------------------------------------------------------------------------
3233

3334
/**
3435
* JSON Language Object
35-
* @implements {Language}
36+
* @implements {IJSONLanguage}
3637
*/
3738
export class JSONLanguage {
3839
/**

src/languages/json-source-code.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import {
2626
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
2727
/** @typedef {import("@eslint/core").File} File */
2828
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
29-
/** @typedef {import("@eslint/core").TextSourceCode} TextSourceCode */
3029
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
3130
/** @typedef {import("@eslint/core").FileProblem} FileProblem */
3231
/** @typedef {import("@eslint/core").DirectiveType} DirectiveType */
3332
/** @typedef {import("@eslint/core").RulesConfig} RulesConfig */
33+
/** @typedef {import("../types.ts").IJSONSourceCode} IJSONSourceCode */
34+
/** @typedef {import("../types.ts").JSONSyntaxElement} JSONSyntaxElement */
3435

3536
//-----------------------------------------------------------------------------
3637
// Helpers
@@ -71,6 +72,7 @@ class JSONTraversalStep extends VisitNodeStep {
7172

7273
/**
7374
* JSON Source Code Object
75+
* @implements {IJSONSourceCode}
7476
*/
7577
export class JSONSourceCode extends TextSourceCodeBase {
7678
/**
@@ -117,6 +119,26 @@ export class JSONSourceCode extends TextSourceCodeBase {
117119
: [];
118120
}
119121

122+
/* eslint-disable class-methods-use-this -- Required to complete interface. */
123+
124+
/**
125+
* Returns the loc information for the given node or token.
126+
* @param {JSONSyntaxElement} nodeOrToken The node or token to get the loc information for.
127+
* @returns {SourceLocation} The loc information for the node or token.
128+
*/
129+
getLoc(nodeOrToken) {
130+
return nodeOrToken.loc;
131+
}
132+
133+
/**
134+
* Returns the range information for the given node or token.
135+
* @param {JSONSyntaxElement} nodeOrToken The node or token to get the range information for.
136+
* @returns {SourceRange} The range information for the node or token.
137+
*/
138+
getRange(nodeOrToken) {
139+
return nodeOrToken.range;
140+
}
141+
120142
/**
121143
* Returns the value of the given comment.
122144
* @param {JSONToken} comment The comment to get the value of.

src/types.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
// Imports
88
//------------------------------------------------------------------------------
99

10-
import { RuleVisitor } from "../../rewrite/packages/core/src/types.ts";
10+
import type {
11+
RuleVisitor,
12+
TextSourceCode,
13+
Language,
14+
} from "../../rewrite/packages/core/src/types.ts";
1115
import {
1216
DocumentNode,
1317
MemberNode,
@@ -21,6 +25,8 @@ import {
2125
NaNNode,
2226
InfinityNode,
2327
IdentifierNode,
28+
AnyNode,
29+
Token,
2430
} from "@humanwhocodes/momoa";
2531

2632
//------------------------------------------------------------------------------
@@ -29,6 +35,16 @@ import {
2935

3036
type ValueNodeParent = DocumentNode | MemberNode | ElementNode;
3137

38+
/**
39+
* A JSON syntax element, including nodes and tokens.
40+
*/
41+
export type JSONSyntaxElement = Token | AnyNode;
42+
43+
/**
44+
* Language options provided for JSON files.
45+
*/
46+
export type JSONLanguageOptions = Record<string, unknown>;
47+
3248
/**
3349
* The visitor format returned from rules in this package.
3450
*/
@@ -59,3 +75,21 @@ export interface JSONRuleVisitor extends RuleVisitor {
5975
"Infinity:exit"?(node: InfinityNode, parent?: ValueNodeParent): void;
6076
"Identifier:exit"?(node: IdentifierNode, parent?: ValueNodeParent): void;
6177
}
78+
79+
/**
80+
* The `SourceCode` implementation for JSON files.
81+
*/
82+
export type IJSONSourceCode = TextSourceCode<{
83+
LangOptions: JSONLanguageOptions;
84+
RootNode: DocumentNode;
85+
Node: JSONSyntaxElement;
86+
SyntaxElementWithLoc: JSONSyntaxElement;
87+
ConfigNode: null;
88+
}>;
89+
90+
export type IJSONLanguage = Language<{
91+
LangOptions: JSONLanguageOptions;
92+
Code: IJSONSourceCode;
93+
RootNode: DocumentNode;
94+
Node: AnyNode;
95+
}>;

0 commit comments

Comments
 (0)