@eslint/json 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/dist/cjs/index.cjs +42 -15
- package/dist/cjs/index.d.cts +92 -12
- package/dist/esm/index.d.ts +92 -12
- package/dist/esm/index.js +42 -15
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ deno add @eslint/json
|
|
|
31
31
|
This package exports two different languages:
|
|
32
32
|
|
|
33
33
|
- `"json/json"` is for regular JSON files
|
|
34
|
-
- `"json/jsonc"` is for JSON files that support comments (JSON-C)
|
|
34
|
+
- `"json/jsonc"` is for JSON files that support comments (JSON-C) such as those used for Visual Studio Code configuration files
|
|
35
35
|
|
|
36
36
|
Depending on which types of JSON files you'd like to lint, you can set up your `eslint.config.js` file to include just the files you'd like. Here's an example that lints both JSON and JSON-C files:
|
|
37
37
|
|
|
@@ -56,7 +56,7 @@ export default [
|
|
|
56
56
|
|
|
57
57
|
// lint JSON-C files
|
|
58
58
|
{
|
|
59
|
-
files: ["**/*.jsonc"],
|
|
59
|
+
files: ["**/*.jsonc", ".vscode/*.json"],
|
|
60
60
|
language: "json/jsonc",
|
|
61
61
|
rules: {
|
|
62
62
|
"json/no-duplicate-keys": "error",
|
|
@@ -76,6 +76,7 @@ export default [
|
|
|
76
76
|
// lint JSON files
|
|
77
77
|
{
|
|
78
78
|
files: ["**/*.json"],
|
|
79
|
+
ignores: ["package-lock.json"],
|
|
79
80
|
language: "json/json",
|
|
80
81
|
...json.configs.recommended,
|
|
81
82
|
},
|
|
@@ -89,10 +90,12 @@ export default [
|
|
|
89
90
|
];
|
|
90
91
|
```
|
|
91
92
|
|
|
93
|
+
**Note:** You generally want to ignore `package-lock.json` because it is auto-generated and you typically will not want to manually make changes to it.
|
|
94
|
+
|
|
92
95
|
## Rules
|
|
93
96
|
|
|
94
97
|
- `no-duplicate-keys` - warns when there are two keys in an object with the same text.
|
|
95
|
-
- `no-empty-keys` - warns when there is a key in an object that is an empty string or contains only whitespace
|
|
98
|
+
- `no-empty-keys` - warns when there is a key in an object that is an empty string or contains only whitespace (note: `package-lock.json` uses empty keys intentionally)
|
|
96
99
|
|
|
97
100
|
## License
|
|
98
101
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -15,13 +15,12 @@ var momoa = require('@humanwhocodes/momoa');
|
|
|
15
15
|
/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
|
|
16
16
|
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
|
|
17
17
|
/** @typedef {import("@humanwhocodes/momoa").Token} JSONToken */
|
|
18
|
-
/** @typedef {import("@eslint/core").
|
|
19
|
-
/** @typedef {import("@eslint/core").
|
|
18
|
+
/** @typedef {import("@eslint/core").SourceRange} SourceRange */
|
|
19
|
+
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
|
|
20
20
|
/** @typedef {import("@eslint/core").File} File */
|
|
21
21
|
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
|
|
22
|
-
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
|
|
23
22
|
/** @typedef {import("@eslint/core").TextSourceCode} TextSourceCode */
|
|
24
|
-
/** @typedef {import("@eslint/core").
|
|
23
|
+
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
|
|
25
24
|
|
|
26
25
|
//-----------------------------------------------------------------------------
|
|
27
26
|
// Helpers
|
|
@@ -49,7 +48,7 @@ class JSONTraversalStep {
|
|
|
49
48
|
|
|
50
49
|
/**
|
|
51
50
|
* The target of the step.
|
|
52
|
-
* @type {JSONNode
|
|
51
|
+
* @type {JSONNode}
|
|
53
52
|
*/
|
|
54
53
|
target;
|
|
55
54
|
|
|
@@ -68,7 +67,7 @@ class JSONTraversalStep {
|
|
|
68
67
|
/**
|
|
69
68
|
* Creates a new instance.
|
|
70
69
|
* @param {Object} options The options for the step.
|
|
71
|
-
* @param {JSONNode
|
|
70
|
+
* @param {JSONNode} options.target The target of the step.
|
|
72
71
|
* @param {1|2} options.phase The phase of the step.
|
|
73
72
|
* @param {Array<any>} options.args The arguments of the step.
|
|
74
73
|
*/
|
|
@@ -108,7 +107,7 @@ class JSONSourceCode {
|
|
|
108
107
|
|
|
109
108
|
/**
|
|
110
109
|
* The AST of the source code.
|
|
111
|
-
* @type {DocumentNode
|
|
110
|
+
* @type {DocumentNode}
|
|
112
111
|
*/
|
|
113
112
|
ast;
|
|
114
113
|
|
|
@@ -128,7 +127,7 @@ class JSONSourceCode {
|
|
|
128
127
|
* Creates a new instance.
|
|
129
128
|
* @param {Object} options The options for the instance.
|
|
130
129
|
* @param {string} options.text The source code text.
|
|
131
|
-
* @param {DocumentNode
|
|
130
|
+
* @param {DocumentNode} options.ast The root AST node.
|
|
132
131
|
*/
|
|
133
132
|
constructor({ text, ast }) {
|
|
134
133
|
this.ast = ast;
|
|
@@ -138,6 +137,28 @@ class JSONSourceCode {
|
|
|
138
137
|
);
|
|
139
138
|
}
|
|
140
139
|
|
|
140
|
+
/* eslint-disable class-methods-use-this -- Required to complete interface. */
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Returns the loc information for the given node or token.
|
|
144
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the loc information for.
|
|
145
|
+
* @returns {SourceLocation} The loc information for the node or token.
|
|
146
|
+
*/
|
|
147
|
+
getLoc(nodeOrToken) {
|
|
148
|
+
return nodeOrToken.loc;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Returns the range information for the given node or token.
|
|
153
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the range information for.
|
|
154
|
+
* @returns {SourceRange} The range information for the node or token.
|
|
155
|
+
*/
|
|
156
|
+
getRange(nodeOrToken) {
|
|
157
|
+
return nodeOrToken.range;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/* eslint-enable class-methods-use-this -- Required to complete interface. */
|
|
161
|
+
|
|
141
162
|
/**
|
|
142
163
|
* Returns the parent of the given node.
|
|
143
164
|
* @param {JSONNode} node The node to get the parent of.
|
|
@@ -204,7 +225,7 @@ class JSONSourceCode {
|
|
|
204
225
|
|
|
205
226
|
/**
|
|
206
227
|
* Traverse the source code and return the steps that were taken.
|
|
207
|
-
* @returns {Iterable<
|
|
228
|
+
* @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
|
|
208
229
|
*/
|
|
209
230
|
traverse() {
|
|
210
231
|
// Because the AST doesn't mutate, we can cache the steps
|
|
@@ -212,15 +233,14 @@ class JSONSourceCode {
|
|
|
212
233
|
return this.#steps.values();
|
|
213
234
|
}
|
|
214
235
|
|
|
236
|
+
/** @type {Array<JSONTraversalStep>} */
|
|
215
237
|
const steps = (this.#steps = []);
|
|
216
238
|
|
|
217
|
-
for (const { node, parent, phase } of momoa.iterator(
|
|
218
|
-
/** @type {DocumentNode} */ (this.ast),
|
|
219
|
-
)) {
|
|
239
|
+
for (const { node, parent, phase } of momoa.iterator(this.ast)) {
|
|
220
240
|
this.#parents.set(node, parent);
|
|
221
241
|
steps.push(
|
|
222
242
|
new JSONTraversalStep({
|
|
223
|
-
target:
|
|
243
|
+
target: node,
|
|
224
244
|
phase: phase === "enter" ? 1 : 2,
|
|
225
245
|
args: [node, parent],
|
|
226
246
|
}),
|
|
@@ -241,6 +261,9 @@ class JSONSourceCode {
|
|
|
241
261
|
// Types
|
|
242
262
|
//-----------------------------------------------------------------------------
|
|
243
263
|
|
|
264
|
+
/** @typedef {import("@eslint/core").Language} Language */
|
|
265
|
+
/** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
|
|
266
|
+
/** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
|
|
244
267
|
|
|
245
268
|
//-----------------------------------------------------------------------------
|
|
246
269
|
// Exports
|
|
@@ -332,7 +355,7 @@ class JSONLanguage {
|
|
|
332
355
|
|
|
333
356
|
return {
|
|
334
357
|
ok: true,
|
|
335
|
-
ast:
|
|
358
|
+
ast: root,
|
|
336
359
|
};
|
|
337
360
|
} catch (ex) {
|
|
338
361
|
// error messages end with (line:column) so we strip that off for ESLint
|
|
@@ -356,7 +379,7 @@ class JSONLanguage {
|
|
|
356
379
|
/**
|
|
357
380
|
* Creates a new `JSONSourceCode` object from the given information.
|
|
358
381
|
* @param {File} file The virtual file to create a `JSONSourceCode` object from.
|
|
359
|
-
* @param {
|
|
382
|
+
* @param {OkParseResult} parseResult The result returned from `parse()`.
|
|
360
383
|
* @returns {JSONSourceCode} The new `JSONSourceCode` object.
|
|
361
384
|
*/
|
|
362
385
|
createSourceCode(file, parseResult) {
|
|
@@ -467,6 +490,10 @@ var noEmptyKeys = {
|
|
|
467
490
|
//-----------------------------------------------------------------------------
|
|
468
491
|
|
|
469
492
|
const plugin = {
|
|
493
|
+
meta: {
|
|
494
|
+
name: "@eslint/json",
|
|
495
|
+
version: "0.2.0", // x-release-please-version
|
|
496
|
+
},
|
|
470
497
|
languages: {
|
|
471
498
|
json: new JSONLanguage({ mode: "json" }),
|
|
472
499
|
jsonc: new JSONLanguage({ mode: "jsonc" }),
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -2,14 +2,20 @@ export { plugin as default };
|
|
|
2
2
|
export type DocumentNode = import("@humanwhocodes/momoa").DocumentNode;
|
|
3
3
|
export type JSONNode = import("@humanwhocodes/momoa").Node;
|
|
4
4
|
export type JSONToken = import("@humanwhocodes/momoa").Token;
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
5
|
+
export type SourceRange = import("@eslint/core").SourceRange;
|
|
6
|
+
export type SourceLocation = import("@eslint/core").SourceLocation;
|
|
7
7
|
export type File = import("@eslint/core").File;
|
|
8
8
|
export type TraversalStep = import("@eslint/core").TraversalStep;
|
|
9
|
-
export type VisitTraversalStep = import("@eslint/core").VisitTraversalStep;
|
|
10
9
|
export type TextSourceCode = import("@eslint/core").TextSourceCode;
|
|
11
|
-
export type
|
|
10
|
+
export type VisitTraversalStep = import("@eslint/core").VisitTraversalStep;
|
|
11
|
+
export type Language = import("@eslint/core").Language;
|
|
12
|
+
export type OkParseResult = import("@eslint/core").OkParseResult<DocumentNode>;
|
|
13
|
+
export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
|
|
12
14
|
declare namespace plugin {
|
|
15
|
+
namespace meta {
|
|
16
|
+
let name: string;
|
|
17
|
+
let version: string;
|
|
18
|
+
}
|
|
13
19
|
namespace languages {
|
|
14
20
|
let json: JSONLanguage;
|
|
15
21
|
let jsonc: JSONLanguage;
|
|
@@ -52,6 +58,9 @@ declare namespace plugin {
|
|
|
52
58
|
* @filedescription Functions to fix up rules to provide missing methods on the `context` object.
|
|
53
59
|
* @author Nicholas C. Zakas
|
|
54
60
|
*/
|
|
61
|
+
/** @typedef {import("@eslint/core").Language} Language */
|
|
62
|
+
/** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
|
|
63
|
+
/** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
|
|
55
64
|
/**
|
|
56
65
|
* JSON Language Object
|
|
57
66
|
* @implements {Language}
|
|
@@ -106,10 +115,10 @@ declare class JSONLanguage implements Language {
|
|
|
106
115
|
/**
|
|
107
116
|
* Creates a new `JSONSourceCode` object from the given information.
|
|
108
117
|
* @param {File} file The virtual file to create a `JSONSourceCode` object from.
|
|
109
|
-
* @param {
|
|
118
|
+
* @param {OkParseResult} parseResult The result returned from `parse()`.
|
|
110
119
|
* @returns {JSONSourceCode} The new `JSONSourceCode` object.
|
|
111
120
|
*/
|
|
112
|
-
createSourceCode(file: File, parseResult:
|
|
121
|
+
createSourceCode(file: File, parseResult: OkParseResult): JSONSourceCode;
|
|
113
122
|
#private;
|
|
114
123
|
}
|
|
115
124
|
/**
|
|
@@ -121,17 +130,17 @@ declare class JSONSourceCode implements TextSourceCode {
|
|
|
121
130
|
* Creates a new instance.
|
|
122
131
|
* @param {Object} options The options for the instance.
|
|
123
132
|
* @param {string} options.text The source code text.
|
|
124
|
-
* @param {DocumentNode
|
|
133
|
+
* @param {DocumentNode} options.ast The root AST node.
|
|
125
134
|
*/
|
|
126
135
|
constructor({ text, ast }: {
|
|
127
136
|
text: string;
|
|
128
|
-
ast: DocumentNode
|
|
137
|
+
ast: DocumentNode;
|
|
129
138
|
});
|
|
130
139
|
/**
|
|
131
140
|
* The AST of the source code.
|
|
132
|
-
* @type {DocumentNode
|
|
141
|
+
* @type {DocumentNode}
|
|
133
142
|
*/
|
|
134
|
-
ast: DocumentNode
|
|
143
|
+
ast: DocumentNode;
|
|
135
144
|
/**
|
|
136
145
|
* The text of the source code.
|
|
137
146
|
* @type {string}
|
|
@@ -142,6 +151,18 @@ declare class JSONSourceCode implements TextSourceCode {
|
|
|
142
151
|
* @type {Array<JSONToken>|undefined}
|
|
143
152
|
*/
|
|
144
153
|
comments: Array<JSONToken> | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Returns the loc information for the given node or token.
|
|
156
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the loc information for.
|
|
157
|
+
* @returns {SourceLocation} The loc information for the node or token.
|
|
158
|
+
*/
|
|
159
|
+
getLoc(nodeOrToken: JSONNode | JSONToken): SourceLocation;
|
|
160
|
+
/**
|
|
161
|
+
* Returns the range information for the given node or token.
|
|
162
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the range information for.
|
|
163
|
+
* @returns {SourceRange} The range information for the node or token.
|
|
164
|
+
*/
|
|
165
|
+
getRange(nodeOrToken: JSONNode | JSONToken): SourceRange;
|
|
145
166
|
/**
|
|
146
167
|
* Returns the parent of the given node.
|
|
147
168
|
* @param {JSONNode} node The node to get the parent of.
|
|
@@ -173,8 +194,67 @@ declare class JSONSourceCode implements TextSourceCode {
|
|
|
173
194
|
public get lines(): any[];
|
|
174
195
|
/**
|
|
175
196
|
* Traverse the source code and return the steps that were taken.
|
|
176
|
-
* @returns {Iterable<
|
|
197
|
+
* @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
|
|
177
198
|
*/
|
|
178
|
-
traverse(): Iterable<
|
|
199
|
+
traverse(): Iterable<JSONTraversalStep>;
|
|
179
200
|
#private;
|
|
180
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* @fileoverview The JSONSourceCode class.
|
|
204
|
+
* @author Nicholas C. Zakas
|
|
205
|
+
*/
|
|
206
|
+
/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
|
|
207
|
+
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
|
|
208
|
+
/** @typedef {import("@humanwhocodes/momoa").Token} JSONToken */
|
|
209
|
+
/** @typedef {import("@eslint/core").SourceRange} SourceRange */
|
|
210
|
+
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
|
|
211
|
+
/** @typedef {import("@eslint/core").File} File */
|
|
212
|
+
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
|
|
213
|
+
/** @typedef {import("@eslint/core").TextSourceCode} TextSourceCode */
|
|
214
|
+
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
|
|
215
|
+
/**
|
|
216
|
+
* A class to represent a step in the traversal process.
|
|
217
|
+
* @implements {VisitTraversalStep}
|
|
218
|
+
*/
|
|
219
|
+
declare class JSONTraversalStep implements VisitTraversalStep {
|
|
220
|
+
/**
|
|
221
|
+
* Creates a new instance.
|
|
222
|
+
* @param {Object} options The options for the step.
|
|
223
|
+
* @param {JSONNode} options.target The target of the step.
|
|
224
|
+
* @param {1|2} options.phase The phase of the step.
|
|
225
|
+
* @param {Array<any>} options.args The arguments of the step.
|
|
226
|
+
*/
|
|
227
|
+
constructor({ target, phase, args }: {
|
|
228
|
+
target: JSONNode;
|
|
229
|
+
phase: 1 | 2;
|
|
230
|
+
args: Array<any>;
|
|
231
|
+
});
|
|
232
|
+
/**
|
|
233
|
+
* The type of the step.
|
|
234
|
+
* @type {"visit"}
|
|
235
|
+
* @readonly
|
|
236
|
+
*/
|
|
237
|
+
readonly type: "visit";
|
|
238
|
+
/**
|
|
239
|
+
* The kind of the step. Represents the same data as the `type` property
|
|
240
|
+
* but it's a number for performance.
|
|
241
|
+
* @type {1}
|
|
242
|
+
* @readonly
|
|
243
|
+
*/
|
|
244
|
+
readonly kind: 1;
|
|
245
|
+
/**
|
|
246
|
+
* The target of the step.
|
|
247
|
+
* @type {JSONNode}
|
|
248
|
+
*/
|
|
249
|
+
target: JSONNode;
|
|
250
|
+
/**
|
|
251
|
+
* The phase of the step.
|
|
252
|
+
* @type {1|2}
|
|
253
|
+
*/
|
|
254
|
+
phase: 1 | 2;
|
|
255
|
+
/**
|
|
256
|
+
* The arguments of the step.
|
|
257
|
+
* @type {Array<any>}
|
|
258
|
+
*/
|
|
259
|
+
args: Array<any>;
|
|
260
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,14 +2,20 @@ export { plugin as default };
|
|
|
2
2
|
export type DocumentNode = import("@humanwhocodes/momoa").DocumentNode;
|
|
3
3
|
export type JSONNode = import("@humanwhocodes/momoa").Node;
|
|
4
4
|
export type JSONToken = import("@humanwhocodes/momoa").Token;
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
5
|
+
export type SourceRange = import("@eslint/core").SourceRange;
|
|
6
|
+
export type SourceLocation = import("@eslint/core").SourceLocation;
|
|
7
7
|
export type File = import("@eslint/core").File;
|
|
8
8
|
export type TraversalStep = import("@eslint/core").TraversalStep;
|
|
9
|
-
export type VisitTraversalStep = import("@eslint/core").VisitTraversalStep;
|
|
10
9
|
export type TextSourceCode = import("@eslint/core").TextSourceCode;
|
|
11
|
-
export type
|
|
10
|
+
export type VisitTraversalStep = import("@eslint/core").VisitTraversalStep;
|
|
11
|
+
export type Language = import("@eslint/core").Language;
|
|
12
|
+
export type OkParseResult = import("@eslint/core").OkParseResult<DocumentNode>;
|
|
13
|
+
export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
|
|
12
14
|
declare namespace plugin {
|
|
15
|
+
namespace meta {
|
|
16
|
+
let name: string;
|
|
17
|
+
let version: string;
|
|
18
|
+
}
|
|
13
19
|
namespace languages {
|
|
14
20
|
let json: JSONLanguage;
|
|
15
21
|
let jsonc: JSONLanguage;
|
|
@@ -52,6 +58,9 @@ declare namespace plugin {
|
|
|
52
58
|
* @filedescription Functions to fix up rules to provide missing methods on the `context` object.
|
|
53
59
|
* @author Nicholas C. Zakas
|
|
54
60
|
*/
|
|
61
|
+
/** @typedef {import("@eslint/core").Language} Language */
|
|
62
|
+
/** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
|
|
63
|
+
/** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
|
|
55
64
|
/**
|
|
56
65
|
* JSON Language Object
|
|
57
66
|
* @implements {Language}
|
|
@@ -106,10 +115,10 @@ declare class JSONLanguage implements Language {
|
|
|
106
115
|
/**
|
|
107
116
|
* Creates a new `JSONSourceCode` object from the given information.
|
|
108
117
|
* @param {File} file The virtual file to create a `JSONSourceCode` object from.
|
|
109
|
-
* @param {
|
|
118
|
+
* @param {OkParseResult} parseResult The result returned from `parse()`.
|
|
110
119
|
* @returns {JSONSourceCode} The new `JSONSourceCode` object.
|
|
111
120
|
*/
|
|
112
|
-
createSourceCode(file: File, parseResult:
|
|
121
|
+
createSourceCode(file: File, parseResult: OkParseResult): JSONSourceCode;
|
|
113
122
|
#private;
|
|
114
123
|
}
|
|
115
124
|
/**
|
|
@@ -121,17 +130,17 @@ declare class JSONSourceCode implements TextSourceCode {
|
|
|
121
130
|
* Creates a new instance.
|
|
122
131
|
* @param {Object} options The options for the instance.
|
|
123
132
|
* @param {string} options.text The source code text.
|
|
124
|
-
* @param {DocumentNode
|
|
133
|
+
* @param {DocumentNode} options.ast The root AST node.
|
|
125
134
|
*/
|
|
126
135
|
constructor({ text, ast }: {
|
|
127
136
|
text: string;
|
|
128
|
-
ast: DocumentNode
|
|
137
|
+
ast: DocumentNode;
|
|
129
138
|
});
|
|
130
139
|
/**
|
|
131
140
|
* The AST of the source code.
|
|
132
|
-
* @type {DocumentNode
|
|
141
|
+
* @type {DocumentNode}
|
|
133
142
|
*/
|
|
134
|
-
ast: DocumentNode
|
|
143
|
+
ast: DocumentNode;
|
|
135
144
|
/**
|
|
136
145
|
* The text of the source code.
|
|
137
146
|
* @type {string}
|
|
@@ -142,6 +151,18 @@ declare class JSONSourceCode implements TextSourceCode {
|
|
|
142
151
|
* @type {Array<JSONToken>|undefined}
|
|
143
152
|
*/
|
|
144
153
|
comments: Array<JSONToken> | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Returns the loc information for the given node or token.
|
|
156
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the loc information for.
|
|
157
|
+
* @returns {SourceLocation} The loc information for the node or token.
|
|
158
|
+
*/
|
|
159
|
+
getLoc(nodeOrToken: JSONNode | JSONToken): SourceLocation;
|
|
160
|
+
/**
|
|
161
|
+
* Returns the range information for the given node or token.
|
|
162
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the range information for.
|
|
163
|
+
* @returns {SourceRange} The range information for the node or token.
|
|
164
|
+
*/
|
|
165
|
+
getRange(nodeOrToken: JSONNode | JSONToken): SourceRange;
|
|
145
166
|
/**
|
|
146
167
|
* Returns the parent of the given node.
|
|
147
168
|
* @param {JSONNode} node The node to get the parent of.
|
|
@@ -173,8 +194,67 @@ declare class JSONSourceCode implements TextSourceCode {
|
|
|
173
194
|
public get lines(): any[];
|
|
174
195
|
/**
|
|
175
196
|
* Traverse the source code and return the steps that were taken.
|
|
176
|
-
* @returns {Iterable<
|
|
197
|
+
* @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
|
|
177
198
|
*/
|
|
178
|
-
traverse(): Iterable<
|
|
199
|
+
traverse(): Iterable<JSONTraversalStep>;
|
|
179
200
|
#private;
|
|
180
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* @fileoverview The JSONSourceCode class.
|
|
204
|
+
* @author Nicholas C. Zakas
|
|
205
|
+
*/
|
|
206
|
+
/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
|
|
207
|
+
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
|
|
208
|
+
/** @typedef {import("@humanwhocodes/momoa").Token} JSONToken */
|
|
209
|
+
/** @typedef {import("@eslint/core").SourceRange} SourceRange */
|
|
210
|
+
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
|
|
211
|
+
/** @typedef {import("@eslint/core").File} File */
|
|
212
|
+
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
|
|
213
|
+
/** @typedef {import("@eslint/core").TextSourceCode} TextSourceCode */
|
|
214
|
+
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
|
|
215
|
+
/**
|
|
216
|
+
* A class to represent a step in the traversal process.
|
|
217
|
+
* @implements {VisitTraversalStep}
|
|
218
|
+
*/
|
|
219
|
+
declare class JSONTraversalStep implements VisitTraversalStep {
|
|
220
|
+
/**
|
|
221
|
+
* Creates a new instance.
|
|
222
|
+
* @param {Object} options The options for the step.
|
|
223
|
+
* @param {JSONNode} options.target The target of the step.
|
|
224
|
+
* @param {1|2} options.phase The phase of the step.
|
|
225
|
+
* @param {Array<any>} options.args The arguments of the step.
|
|
226
|
+
*/
|
|
227
|
+
constructor({ target, phase, args }: {
|
|
228
|
+
target: JSONNode;
|
|
229
|
+
phase: 1 | 2;
|
|
230
|
+
args: Array<any>;
|
|
231
|
+
});
|
|
232
|
+
/**
|
|
233
|
+
* The type of the step.
|
|
234
|
+
* @type {"visit"}
|
|
235
|
+
* @readonly
|
|
236
|
+
*/
|
|
237
|
+
readonly type: "visit";
|
|
238
|
+
/**
|
|
239
|
+
* The kind of the step. Represents the same data as the `type` property
|
|
240
|
+
* but it's a number for performance.
|
|
241
|
+
* @type {1}
|
|
242
|
+
* @readonly
|
|
243
|
+
*/
|
|
244
|
+
readonly kind: 1;
|
|
245
|
+
/**
|
|
246
|
+
* The target of the step.
|
|
247
|
+
* @type {JSONNode}
|
|
248
|
+
*/
|
|
249
|
+
target: JSONNode;
|
|
250
|
+
/**
|
|
251
|
+
* The phase of the step.
|
|
252
|
+
* @type {1|2}
|
|
253
|
+
*/
|
|
254
|
+
phase: 1 | 2;
|
|
255
|
+
/**
|
|
256
|
+
* The arguments of the step.
|
|
257
|
+
* @type {Array<any>}
|
|
258
|
+
*/
|
|
259
|
+
args: Array<any>;
|
|
260
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -14,13 +14,12 @@ import { iterator, visitorKeys, parse } from '@humanwhocodes/momoa';
|
|
|
14
14
|
/** @typedef {import("@humanwhocodes/momoa").DocumentNode} DocumentNode */
|
|
15
15
|
/** @typedef {import("@humanwhocodes/momoa").Node} JSONNode */
|
|
16
16
|
/** @typedef {import("@humanwhocodes/momoa").Token} JSONToken */
|
|
17
|
-
/** @typedef {import("@eslint/core").
|
|
18
|
-
/** @typedef {import("@eslint/core").
|
|
17
|
+
/** @typedef {import("@eslint/core").SourceRange} SourceRange */
|
|
18
|
+
/** @typedef {import("@eslint/core").SourceLocation} SourceLocation */
|
|
19
19
|
/** @typedef {import("@eslint/core").File} File */
|
|
20
20
|
/** @typedef {import("@eslint/core").TraversalStep} TraversalStep */
|
|
21
|
-
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
|
|
22
21
|
/** @typedef {import("@eslint/core").TextSourceCode} TextSourceCode */
|
|
23
|
-
/** @typedef {import("@eslint/core").
|
|
22
|
+
/** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
|
|
24
23
|
|
|
25
24
|
//-----------------------------------------------------------------------------
|
|
26
25
|
// Helpers
|
|
@@ -48,7 +47,7 @@ class JSONTraversalStep {
|
|
|
48
47
|
|
|
49
48
|
/**
|
|
50
49
|
* The target of the step.
|
|
51
|
-
* @type {JSONNode
|
|
50
|
+
* @type {JSONNode}
|
|
52
51
|
*/
|
|
53
52
|
target;
|
|
54
53
|
|
|
@@ -67,7 +66,7 @@ class JSONTraversalStep {
|
|
|
67
66
|
/**
|
|
68
67
|
* Creates a new instance.
|
|
69
68
|
* @param {Object} options The options for the step.
|
|
70
|
-
* @param {JSONNode
|
|
69
|
+
* @param {JSONNode} options.target The target of the step.
|
|
71
70
|
* @param {1|2} options.phase The phase of the step.
|
|
72
71
|
* @param {Array<any>} options.args The arguments of the step.
|
|
73
72
|
*/
|
|
@@ -107,7 +106,7 @@ class JSONSourceCode {
|
|
|
107
106
|
|
|
108
107
|
/**
|
|
109
108
|
* The AST of the source code.
|
|
110
|
-
* @type {DocumentNode
|
|
109
|
+
* @type {DocumentNode}
|
|
111
110
|
*/
|
|
112
111
|
ast;
|
|
113
112
|
|
|
@@ -127,7 +126,7 @@ class JSONSourceCode {
|
|
|
127
126
|
* Creates a new instance.
|
|
128
127
|
* @param {Object} options The options for the instance.
|
|
129
128
|
* @param {string} options.text The source code text.
|
|
130
|
-
* @param {DocumentNode
|
|
129
|
+
* @param {DocumentNode} options.ast The root AST node.
|
|
131
130
|
*/
|
|
132
131
|
constructor({ text, ast }) {
|
|
133
132
|
this.ast = ast;
|
|
@@ -137,6 +136,28 @@ class JSONSourceCode {
|
|
|
137
136
|
);
|
|
138
137
|
}
|
|
139
138
|
|
|
139
|
+
/* eslint-disable class-methods-use-this -- Required to complete interface. */
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Returns the loc information for the given node or token.
|
|
143
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the loc information for.
|
|
144
|
+
* @returns {SourceLocation} The loc information for the node or token.
|
|
145
|
+
*/
|
|
146
|
+
getLoc(nodeOrToken) {
|
|
147
|
+
return nodeOrToken.loc;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Returns the range information for the given node or token.
|
|
152
|
+
* @param {JSONNode|JSONToken} nodeOrToken The node or token to get the range information for.
|
|
153
|
+
* @returns {SourceRange} The range information for the node or token.
|
|
154
|
+
*/
|
|
155
|
+
getRange(nodeOrToken) {
|
|
156
|
+
return nodeOrToken.range;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* eslint-enable class-methods-use-this -- Required to complete interface. */
|
|
160
|
+
|
|
140
161
|
/**
|
|
141
162
|
* Returns the parent of the given node.
|
|
142
163
|
* @param {JSONNode} node The node to get the parent of.
|
|
@@ -203,7 +224,7 @@ class JSONSourceCode {
|
|
|
203
224
|
|
|
204
225
|
/**
|
|
205
226
|
* Traverse the source code and return the steps that were taken.
|
|
206
|
-
* @returns {Iterable<
|
|
227
|
+
* @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
|
|
207
228
|
*/
|
|
208
229
|
traverse() {
|
|
209
230
|
// Because the AST doesn't mutate, we can cache the steps
|
|
@@ -211,15 +232,14 @@ class JSONSourceCode {
|
|
|
211
232
|
return this.#steps.values();
|
|
212
233
|
}
|
|
213
234
|
|
|
235
|
+
/** @type {Array<JSONTraversalStep>} */
|
|
214
236
|
const steps = (this.#steps = []);
|
|
215
237
|
|
|
216
|
-
for (const { node, parent, phase } of iterator(
|
|
217
|
-
/** @type {DocumentNode} */ (this.ast),
|
|
218
|
-
)) {
|
|
238
|
+
for (const { node, parent, phase } of iterator(this.ast)) {
|
|
219
239
|
this.#parents.set(node, parent);
|
|
220
240
|
steps.push(
|
|
221
241
|
new JSONTraversalStep({
|
|
222
|
-
target:
|
|
242
|
+
target: node,
|
|
223
243
|
phase: phase === "enter" ? 1 : 2,
|
|
224
244
|
args: [node, parent],
|
|
225
245
|
}),
|
|
@@ -240,6 +260,9 @@ class JSONSourceCode {
|
|
|
240
260
|
// Types
|
|
241
261
|
//-----------------------------------------------------------------------------
|
|
242
262
|
|
|
263
|
+
/** @typedef {import("@eslint/core").Language} Language */
|
|
264
|
+
/** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
|
|
265
|
+
/** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
|
|
243
266
|
|
|
244
267
|
//-----------------------------------------------------------------------------
|
|
245
268
|
// Exports
|
|
@@ -331,7 +354,7 @@ class JSONLanguage {
|
|
|
331
354
|
|
|
332
355
|
return {
|
|
333
356
|
ok: true,
|
|
334
|
-
ast:
|
|
357
|
+
ast: root,
|
|
335
358
|
};
|
|
336
359
|
} catch (ex) {
|
|
337
360
|
// error messages end with (line:column) so we strip that off for ESLint
|
|
@@ -355,7 +378,7 @@ class JSONLanguage {
|
|
|
355
378
|
/**
|
|
356
379
|
* Creates a new `JSONSourceCode` object from the given information.
|
|
357
380
|
* @param {File} file The virtual file to create a `JSONSourceCode` object from.
|
|
358
|
-
* @param {
|
|
381
|
+
* @param {OkParseResult} parseResult The result returned from `parse()`.
|
|
359
382
|
* @returns {JSONSourceCode} The new `JSONSourceCode` object.
|
|
360
383
|
*/
|
|
361
384
|
createSourceCode(file, parseResult) {
|
|
@@ -466,6 +489,10 @@ var noEmptyKeys = {
|
|
|
466
489
|
//-----------------------------------------------------------------------------
|
|
467
490
|
|
|
468
491
|
const plugin = {
|
|
492
|
+
meta: {
|
|
493
|
+
name: "@eslint/json",
|
|
494
|
+
version: "0.2.0", // x-release-please-version
|
|
495
|
+
},
|
|
469
496
|
languages: {
|
|
470
497
|
json: new JSONLanguage({ mode: "json" }),
|
|
471
498
|
jsonc: new JSONLanguage({ mode: "jsonc" }),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint/json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "JSON linting plugin for ESLint",
|
|
5
5
|
"author": "Nicholas C. Zakas",
|
|
6
6
|
"type": "module",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"@humanwhocodes/momoa": "^3.1.1"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@eslint/core": "^0.
|
|
67
|
+
"@eslint/core": "^0.3.0",
|
|
68
68
|
"@types/eslint": "^8.56.10",
|
|
69
69
|
"c8": "^9.1.0",
|
|
70
70
|
"eslint": "^9.6.0",
|