@eslint/json 0.3.0 → 0.4.1

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 CHANGED
@@ -75,6 +75,47 @@ export default [
75
75
  ];
76
76
  ```
77
77
 
78
+ In CommonJS format:
79
+
80
+ ```js
81
+ const json = require("@eslint/json").default;
82
+
83
+ module.exports = [
84
+ {
85
+ plugins: {
86
+ json,
87
+ },
88
+ },
89
+
90
+ // lint JSON files
91
+ {
92
+ files: ["**/*.json"],
93
+ language: "json/json",
94
+ rules: {
95
+ "json/no-duplicate-keys": "error",
96
+ },
97
+ },
98
+
99
+ // lint JSONC files
100
+ {
101
+ files: ["**/*.jsonc", ".vscode/*.json"],
102
+ language: "json/jsonc",
103
+ rules: {
104
+ "json/no-duplicate-keys": "error",
105
+ },
106
+ },
107
+
108
+ // lint JSON5 files
109
+ {
110
+ files: ["**/*.json5"],
111
+ language: "json/json5",
112
+ rules: {
113
+ "json/no-duplicate-keys": "error",
114
+ },
115
+ },
116
+ ];
117
+ ```
118
+
78
119
  ## Recommended Configuration
79
120
 
80
121
  To use the recommended configuration for this plugin, specify your matching `files` and then use the `json.configs.recommended` object, like this:
@@ -114,6 +155,34 @@ export default [
114
155
  - `no-duplicate-keys` - warns when there are two keys in an object with the same text.
115
156
  - `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)
116
157
 
158
+ ## Frequently Asked Questions
159
+
160
+ ### How does this relate to `eslint-plugin-json` and `eslint-plugin-jsonc`?
161
+
162
+ This plugin implements JSON parsing for ESLint using the language plugins API, which is the official way of supporting non-JavaScript languages in ESLint. This differs from the other plugins:
163
+
164
+ - `eslint-plugin-json` uses a processor to parse the JSON, meaning it doesn't create an AST and you can't write custom rules for it.
165
+ - `eslint-plugin-jsonc` uses a parser that still goes through the JavaScript linting functionality and requires several rules to disallow valid JavaScript syntax that is invalid in JSON.
166
+
167
+ As such, this plugin is more robust and faster than the others. You can write your own custom rules when using the languages in this plugin, too.
168
+
169
+ ### What about missing rules that are available in `eslint-plugin-json` and `eslint-plugin-jsonc`?
170
+
171
+ Most of the rules in `eslint-plugin-json` are actually syntax errors that are caught automatically by the parser used in this plugin.
172
+
173
+ Similarly, many of the rules in `eslint-plugin-jsonc` specifically disallow valid JavaScript syntax that is invalid in the context of JSON. These are also automatically caught by the parser in this plugin.
174
+
175
+ Any other rules that catch potential problems in JSON are welcome to be implemented. You can [open an issue](https://github.com/eslint/json/issues/new/choose) to propose a new rule.
176
+
117
177
  ## License
118
178
 
119
179
  Apache 2.0
180
+
181
+ ## Sponsors
182
+
183
+ <!-- NOTE: This section is autogenerated. Do not manually edit.-->
184
+ <!--sponsorsstart-->
185
+ <!--sponsorsend-->
186
+
187
+ <!--techsponsorsstart-->
188
+ <!--techsponsorsend-->
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var momoa = require('@humanwhocodes/momoa');
6
+ var pluginKit = require('@eslint/plugin-kit');
4
7
 
5
8
  /**
6
9
  * @fileoverview The JSONSourceCode class.
@@ -28,41 +31,13 @@ var momoa = require('@humanwhocodes/momoa');
28
31
 
29
32
  /**
30
33
  * A class to represent a step in the traversal process.
31
- * @implements {VisitTraversalStep}
32
34
  */
33
- class JSONTraversalStep {
34
- /**
35
- * The type of the step.
36
- * @type {"visit"}
37
- * @readonly
38
- */
39
- type = "visit";
40
-
41
- /**
42
- * The kind of the step. Represents the same data as the `type` property
43
- * but it's a number for performance.
44
- * @type {1}
45
- * @readonly
46
- */
47
- kind = 1;
48
-
35
+ class JSONTraversalStep extends pluginKit.VisitNodeStep {
49
36
  /**
50
37
  * The target of the step.
51
38
  * @type {JSONNode}
52
39
  */
53
- target;
54
-
55
- /**
56
- * The phase of the step.
57
- * @type {1|2}
58
- */
59
- phase;
60
-
61
- /**
62
- * The arguments of the step.
63
- * @type {Array<any>}
64
- */
65
- args;
40
+ target = undefined;
66
41
 
67
42
  /**
68
43
  * Creates a new instance.
@@ -72,9 +47,9 @@ class JSONTraversalStep {
72
47
  * @param {Array<any>} options.args The arguments of the step.
73
48
  */
74
49
  constructor({ target, phase, args }) {
50
+ super({ target, phase, args });
51
+
75
52
  this.target = target;
76
- this.phase = phase;
77
- this.args = args;
78
53
  }
79
54
  }
80
55
 
@@ -84,9 +59,8 @@ class JSONTraversalStep {
84
59
 
85
60
  /**
86
61
  * JSON Source Code Object
87
- * @implements {TextSourceCode}
88
62
  */
89
- class JSONSourceCode {
63
+ class JSONSourceCode extends pluginKit.TextSourceCodeBase {
90
64
  /**
91
65
  * Cached traversal steps.
92
66
  * @type {Array<JSONTraversalStep>|undefined}
@@ -99,23 +73,11 @@ class JSONSourceCode {
99
73
  */
100
74
  #parents = new WeakMap();
101
75
 
102
- /**
103
- * The lines of text in the source code.
104
- * @type {Array<string>}
105
- */
106
- #lines;
107
-
108
76
  /**
109
77
  * The AST of the source code.
110
78
  * @type {DocumentNode}
111
79
  */
112
- ast;
113
-
114
- /**
115
- * The text of the source code.
116
- * @type {string}
117
- */
118
- text;
80
+ ast = undefined;
119
81
 
120
82
  /**
121
83
  * The comment node in the source code.
@@ -130,35 +92,13 @@ class JSONSourceCode {
130
92
  * @param {DocumentNode} options.ast The root AST node.
131
93
  */
132
94
  constructor({ text, ast }) {
95
+ super({ text, ast });
133
96
  this.ast = ast;
134
- this.text = text;
135
- this.comments = ast.tokens.filter(token =>
136
- token.type.endsWith("Comment"),
137
- );
138
- }
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;
97
+ this.comments = ast.tokens
98
+ ? ast.tokens.filter(token => token.type.endsWith("Comment"))
99
+ : [];
149
100
  }
150
101
 
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
-
162
102
  /**
163
103
  * Returns the parent of the given node.
164
104
  * @param {JSONNode} node The node to get the parent of.
@@ -168,61 +108,6 @@ class JSONSourceCode {
168
108
  return this.#parents.get(node);
169
109
  }
170
110
 
171
- /**
172
- * Gets all the ancestors of a given node
173
- * @param {JSONNode} node The node
174
- * @returns {Array<JSONNode>} All the ancestor nodes in the AST, not including the provided node, starting
175
- * from the root node at index 0 and going inwards to the parent node.
176
- * @throws {TypeError} When `node` is missing.
177
- */
178
- getAncestors(node) {
179
- if (!node) {
180
- throw new TypeError("Missing required argument: node.");
181
- }
182
-
183
- const ancestorsStartingAtParent = [];
184
-
185
- for (
186
- let ancestor = this.#parents.get(node);
187
- ancestor;
188
- ancestor = this.#parents.get(ancestor)
189
- ) {
190
- ancestorsStartingAtParent.push(ancestor);
191
- }
192
-
193
- return ancestorsStartingAtParent.reverse();
194
- }
195
-
196
- /**
197
- * Gets the source code for the given node.
198
- * @param {JSONNode} [node] The AST node to get the text for.
199
- * @param {number} [beforeCount] The number of characters before the node to retrieve.
200
- * @param {number} [afterCount] The number of characters after the node to retrieve.
201
- * @returns {string} The text representing the AST node.
202
- * @public
203
- */
204
- getText(node, beforeCount, afterCount) {
205
- if (node) {
206
- return this.text.slice(
207
- Math.max(node.range[0] - (beforeCount || 0), 0),
208
- node.range[1] + (afterCount || 0),
209
- );
210
- }
211
- return this.text;
212
- }
213
-
214
- /**
215
- * Gets the entire source text split into an array of lines.
216
- * @returns {Array} The source text as an array of lines.
217
- * @public
218
- */
219
- get lines() {
220
- if (!this.#lines) {
221
- this.#lines = this.text.split(/\r?\n/gu);
222
- }
223
- return this.#lines;
224
- }
225
-
226
111
  /**
227
112
  * Traverse the source code and return the steps that were taken.
228
113
  * @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
@@ -237,7 +122,10 @@ class JSONSourceCode {
237
122
  const steps = (this.#steps = []);
238
123
 
239
124
  for (const { node, parent, phase } of momoa.iterator(this.ast)) {
240
- this.#parents.set(node, parent);
125
+ if (parent) {
126
+ this.#parents.set(node, parent);
127
+ }
128
+
241
129
  steps.push(
242
130
  new JSONTraversalStep({
243
131
  target: node,
@@ -498,7 +386,7 @@ var noEmptyKeys = {
498
386
  const plugin = {
499
387
  meta: {
500
388
  name: "@eslint/json",
501
- version: "0.3.0", // x-release-please-version
389
+ version: "0.4.1", // x-release-please-version
502
390
  },
503
391
  languages: {
504
392
  json: new JSONLanguage({ mode: "json" }),
@@ -522,4 +410,6 @@ Object.assign(plugin.configs, {
522
410
  },
523
411
  });
524
412
 
525
- module.exports = plugin;
413
+ exports.JSONLanguage = JSONLanguage;
414
+ exports.JSONSourceCode = JSONSourceCode;
415
+ exports.default = plugin;
@@ -1,4 +1,3 @@
1
- export { plugin as default };
2
1
  export type DocumentNode = import("@humanwhocodes/momoa").DocumentNode;
3
2
  export type JSONNode = import("@humanwhocodes/momoa").Node;
4
3
  export type JSONToken = import("@humanwhocodes/momoa").Token;
@@ -11,50 +10,6 @@ export type VisitTraversalStep = import("@eslint/core").VisitTraversalStep;
11
10
  export type Language = import("@eslint/core").Language;
12
11
  export type OkParseResult = import("@eslint/core").OkParseResult<DocumentNode>;
13
12
  export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
14
- declare namespace plugin {
15
- namespace meta {
16
- let name: string;
17
- let version: string;
18
- }
19
- namespace languages {
20
- let json: JSONLanguage;
21
- let jsonc: JSONLanguage;
22
- let json5: JSONLanguage;
23
- }
24
- let rules: {
25
- "no-duplicate-keys": {
26
- meta: {
27
- type: string;
28
- docs: {
29
- description: string;
30
- };
31
- messages: {
32
- duplicateKey: string;
33
- };
34
- };
35
- create(context: any): {
36
- Object(): void;
37
- Member(node: any): void;
38
- "Object:exit"(): void;
39
- };
40
- };
41
- "no-empty-keys": {
42
- meta: {
43
- type: string;
44
- docs: {
45
- description: string;
46
- };
47
- messages: {
48
- emptyKey: string;
49
- };
50
- };
51
- create(context: any): {
52
- Member(node: any): void;
53
- };
54
- };
55
- };
56
- let configs: {};
57
- }
58
13
  /**
59
14
  * @filedescription Functions to fix up rules to provide missing methods on the `context` object.
60
15
  * @author Nicholas C. Zakas
@@ -66,7 +21,7 @@ declare namespace plugin {
66
21
  * JSON Language Object
67
22
  * @implements {Language}
68
23
  */
69
- declare class JSONLanguage implements Language {
24
+ export class JSONLanguage implements Language {
70
25
  /**
71
26
  * Creates a new instance.
72
27
  * @param {Object} options The options to use for this instance.
@@ -124,9 +79,8 @@ declare class JSONLanguage implements Language {
124
79
  }
125
80
  /**
126
81
  * JSON Source Code Object
127
- * @implements {TextSourceCode}
128
82
  */
129
- declare class JSONSourceCode implements TextSourceCode {
83
+ export class JSONSourceCode extends TextSourceCodeBase {
130
84
  /**
131
85
  * Creates a new instance.
132
86
  * @param {Object} options The options for the instance.
@@ -142,57 +96,17 @@ declare class JSONSourceCode implements TextSourceCode {
142
96
  * @type {DocumentNode}
143
97
  */
144
98
  ast: DocumentNode;
145
- /**
146
- * The text of the source code.
147
- * @type {string}
148
- */
149
- text: string;
150
99
  /**
151
100
  * The comment node in the source code.
152
101
  * @type {Array<JSONToken>|undefined}
153
102
  */
154
103
  comments: Array<JSONToken> | undefined;
155
- /**
156
- * Returns the loc information for the given node or token.
157
- * @param {JSONNode|JSONToken} nodeOrToken The node or token to get the loc information for.
158
- * @returns {SourceLocation} The loc information for the node or token.
159
- */
160
- getLoc(nodeOrToken: JSONNode | JSONToken): SourceLocation;
161
- /**
162
- * Returns the range information for the given node or token.
163
- * @param {JSONNode|JSONToken} nodeOrToken The node or token to get the range information for.
164
- * @returns {SourceRange} The range information for the node or token.
165
- */
166
- getRange(nodeOrToken: JSONNode | JSONToken): SourceRange;
167
104
  /**
168
105
  * Returns the parent of the given node.
169
106
  * @param {JSONNode} node The node to get the parent of.
170
107
  * @returns {JSONNode|undefined} The parent of the node.
171
108
  */
172
109
  getParent(node: JSONNode): JSONNode | undefined;
173
- /**
174
- * Gets all the ancestors of a given node
175
- * @param {JSONNode} node The node
176
- * @returns {Array<JSONNode>} All the ancestor nodes in the AST, not including the provided node, starting
177
- * from the root node at index 0 and going inwards to the parent node.
178
- * @throws {TypeError} When `node` is missing.
179
- */
180
- getAncestors(node: JSONNode): Array<JSONNode>;
181
- /**
182
- * Gets the source code for the given node.
183
- * @param {JSONNode} [node] The AST node to get the text for.
184
- * @param {number} [beforeCount] The number of characters before the node to retrieve.
185
- * @param {number} [afterCount] The number of characters after the node to retrieve.
186
- * @returns {string} The text representing the AST node.
187
- * @public
188
- */
189
- public getText(node?: JSONNode, beforeCount?: number, afterCount?: number): string;
190
- /**
191
- * Gets the entire source text split into an array of lines.
192
- * @returns {Array} The source text as an array of lines.
193
- * @public
194
- */
195
- public get lines(): any[];
196
110
  /**
197
111
  * Traverse the source code and return the steps that were taken.
198
112
  * @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
@@ -200,6 +114,51 @@ declare class JSONSourceCode implements TextSourceCode {
200
114
  traverse(): Iterable<JSONTraversalStep>;
201
115
  #private;
202
116
  }
117
+ declare namespace plugin {
118
+ namespace meta {
119
+ let name: string;
120
+ let version: string;
121
+ }
122
+ namespace languages {
123
+ let json: JSONLanguage;
124
+ let jsonc: JSONLanguage;
125
+ let json5: JSONLanguage;
126
+ }
127
+ let rules: {
128
+ "no-duplicate-keys": {
129
+ meta: {
130
+ type: string;
131
+ docs: {
132
+ description: string;
133
+ };
134
+ messages: {
135
+ duplicateKey: string;
136
+ };
137
+ };
138
+ create(context: any): {
139
+ Object(): void;
140
+ Member(node: any): void;
141
+ "Object:exit"(): void;
142
+ };
143
+ };
144
+ "no-empty-keys": {
145
+ meta: {
146
+ type: string;
147
+ docs: {
148
+ description: string;
149
+ };
150
+ messages: {
151
+ emptyKey: string;
152
+ };
153
+ };
154
+ create(context: any): {
155
+ Member(node: any): void;
156
+ };
157
+ };
158
+ };
159
+ let configs: {};
160
+ }
161
+ import { TextSourceCodeBase } from '@eslint/plugin-kit';
203
162
  /**
204
163
  * @fileoverview The JSONSourceCode class.
205
164
  * @author Nicholas C. Zakas
@@ -215,9 +174,8 @@ declare class JSONSourceCode implements TextSourceCode {
215
174
  /** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
216
175
  /**
217
176
  * A class to represent a step in the traversal process.
218
- * @implements {VisitTraversalStep}
219
177
  */
220
- declare class JSONTraversalStep implements VisitTraversalStep {
178
+ declare class JSONTraversalStep extends VisitNodeStep {
221
179
  /**
222
180
  * Creates a new instance.
223
181
  * @param {Object} options The options for the step.
@@ -230,32 +188,11 @@ declare class JSONTraversalStep implements VisitTraversalStep {
230
188
  phase: 1 | 2;
231
189
  args: Array<any>;
232
190
  });
233
- /**
234
- * The type of the step.
235
- * @type {"visit"}
236
- * @readonly
237
- */
238
- readonly type: "visit";
239
- /**
240
- * The kind of the step. Represents the same data as the `type` property
241
- * but it's a number for performance.
242
- * @type {1}
243
- * @readonly
244
- */
245
- readonly kind: 1;
246
191
  /**
247
192
  * The target of the step.
248
193
  * @type {JSONNode}
249
194
  */
250
195
  target: JSONNode;
251
- /**
252
- * The phase of the step.
253
- * @type {1|2}
254
- */
255
- phase: 1 | 2;
256
- /**
257
- * The arguments of the step.
258
- * @type {Array<any>}
259
- */
260
- args: Array<any>;
261
196
  }
197
+ import { VisitNodeStep } from '@eslint/plugin-kit';
198
+ export { plugin as default };
@@ -1,4 +1,3 @@
1
- export { plugin as default };
2
1
  export type DocumentNode = import("@humanwhocodes/momoa").DocumentNode;
3
2
  export type JSONNode = import("@humanwhocodes/momoa").Node;
4
3
  export type JSONToken = import("@humanwhocodes/momoa").Token;
@@ -11,50 +10,6 @@ export type VisitTraversalStep = import("@eslint/core").VisitTraversalStep;
11
10
  export type Language = import("@eslint/core").Language;
12
11
  export type OkParseResult = import("@eslint/core").OkParseResult<DocumentNode>;
13
12
  export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
14
- declare namespace plugin {
15
- namespace meta {
16
- let name: string;
17
- let version: string;
18
- }
19
- namespace languages {
20
- let json: JSONLanguage;
21
- let jsonc: JSONLanguage;
22
- let json5: JSONLanguage;
23
- }
24
- let rules: {
25
- "no-duplicate-keys": {
26
- meta: {
27
- type: string;
28
- docs: {
29
- description: string;
30
- };
31
- messages: {
32
- duplicateKey: string;
33
- };
34
- };
35
- create(context: any): {
36
- Object(): void;
37
- Member(node: any): void;
38
- "Object:exit"(): void;
39
- };
40
- };
41
- "no-empty-keys": {
42
- meta: {
43
- type: string;
44
- docs: {
45
- description: string;
46
- };
47
- messages: {
48
- emptyKey: string;
49
- };
50
- };
51
- create(context: any): {
52
- Member(node: any): void;
53
- };
54
- };
55
- };
56
- let configs: {};
57
- }
58
13
  /**
59
14
  * @filedescription Functions to fix up rules to provide missing methods on the `context` object.
60
15
  * @author Nicholas C. Zakas
@@ -66,7 +21,7 @@ declare namespace plugin {
66
21
  * JSON Language Object
67
22
  * @implements {Language}
68
23
  */
69
- declare class JSONLanguage implements Language {
24
+ export class JSONLanguage implements Language {
70
25
  /**
71
26
  * Creates a new instance.
72
27
  * @param {Object} options The options to use for this instance.
@@ -124,9 +79,8 @@ declare class JSONLanguage implements Language {
124
79
  }
125
80
  /**
126
81
  * JSON Source Code Object
127
- * @implements {TextSourceCode}
128
82
  */
129
- declare class JSONSourceCode implements TextSourceCode {
83
+ export class JSONSourceCode extends TextSourceCodeBase {
130
84
  /**
131
85
  * Creates a new instance.
132
86
  * @param {Object} options The options for the instance.
@@ -142,57 +96,17 @@ declare class JSONSourceCode implements TextSourceCode {
142
96
  * @type {DocumentNode}
143
97
  */
144
98
  ast: DocumentNode;
145
- /**
146
- * The text of the source code.
147
- * @type {string}
148
- */
149
- text: string;
150
99
  /**
151
100
  * The comment node in the source code.
152
101
  * @type {Array<JSONToken>|undefined}
153
102
  */
154
103
  comments: Array<JSONToken> | undefined;
155
- /**
156
- * Returns the loc information for the given node or token.
157
- * @param {JSONNode|JSONToken} nodeOrToken The node or token to get the loc information for.
158
- * @returns {SourceLocation} The loc information for the node or token.
159
- */
160
- getLoc(nodeOrToken: JSONNode | JSONToken): SourceLocation;
161
- /**
162
- * Returns the range information for the given node or token.
163
- * @param {JSONNode|JSONToken} nodeOrToken The node or token to get the range information for.
164
- * @returns {SourceRange} The range information for the node or token.
165
- */
166
- getRange(nodeOrToken: JSONNode | JSONToken): SourceRange;
167
104
  /**
168
105
  * Returns the parent of the given node.
169
106
  * @param {JSONNode} node The node to get the parent of.
170
107
  * @returns {JSONNode|undefined} The parent of the node.
171
108
  */
172
109
  getParent(node: JSONNode): JSONNode | undefined;
173
- /**
174
- * Gets all the ancestors of a given node
175
- * @param {JSONNode} node The node
176
- * @returns {Array<JSONNode>} All the ancestor nodes in the AST, not including the provided node, starting
177
- * from the root node at index 0 and going inwards to the parent node.
178
- * @throws {TypeError} When `node` is missing.
179
- */
180
- getAncestors(node: JSONNode): Array<JSONNode>;
181
- /**
182
- * Gets the source code for the given node.
183
- * @param {JSONNode} [node] The AST node to get the text for.
184
- * @param {number} [beforeCount] The number of characters before the node to retrieve.
185
- * @param {number} [afterCount] The number of characters after the node to retrieve.
186
- * @returns {string} The text representing the AST node.
187
- * @public
188
- */
189
- public getText(node?: JSONNode, beforeCount?: number, afterCount?: number): string;
190
- /**
191
- * Gets the entire source text split into an array of lines.
192
- * @returns {Array} The source text as an array of lines.
193
- * @public
194
- */
195
- public get lines(): any[];
196
110
  /**
197
111
  * Traverse the source code and return the steps that were taken.
198
112
  * @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
@@ -200,6 +114,51 @@ declare class JSONSourceCode implements TextSourceCode {
200
114
  traverse(): Iterable<JSONTraversalStep>;
201
115
  #private;
202
116
  }
117
+ declare namespace plugin {
118
+ namespace meta {
119
+ let name: string;
120
+ let version: string;
121
+ }
122
+ namespace languages {
123
+ let json: JSONLanguage;
124
+ let jsonc: JSONLanguage;
125
+ let json5: JSONLanguage;
126
+ }
127
+ let rules: {
128
+ "no-duplicate-keys": {
129
+ meta: {
130
+ type: string;
131
+ docs: {
132
+ description: string;
133
+ };
134
+ messages: {
135
+ duplicateKey: string;
136
+ };
137
+ };
138
+ create(context: any): {
139
+ Object(): void;
140
+ Member(node: any): void;
141
+ "Object:exit"(): void;
142
+ };
143
+ };
144
+ "no-empty-keys": {
145
+ meta: {
146
+ type: string;
147
+ docs: {
148
+ description: string;
149
+ };
150
+ messages: {
151
+ emptyKey: string;
152
+ };
153
+ };
154
+ create(context: any): {
155
+ Member(node: any): void;
156
+ };
157
+ };
158
+ };
159
+ let configs: {};
160
+ }
161
+ import { TextSourceCodeBase } from '@eslint/plugin-kit';
203
162
  /**
204
163
  * @fileoverview The JSONSourceCode class.
205
164
  * @author Nicholas C. Zakas
@@ -215,9 +174,8 @@ declare class JSONSourceCode implements TextSourceCode {
215
174
  /** @typedef {import("@eslint/core").VisitTraversalStep} VisitTraversalStep */
216
175
  /**
217
176
  * A class to represent a step in the traversal process.
218
- * @implements {VisitTraversalStep}
219
177
  */
220
- declare class JSONTraversalStep implements VisitTraversalStep {
178
+ declare class JSONTraversalStep extends VisitNodeStep {
221
179
  /**
222
180
  * Creates a new instance.
223
181
  * @param {Object} options The options for the step.
@@ -230,32 +188,11 @@ declare class JSONTraversalStep implements VisitTraversalStep {
230
188
  phase: 1 | 2;
231
189
  args: Array<any>;
232
190
  });
233
- /**
234
- * The type of the step.
235
- * @type {"visit"}
236
- * @readonly
237
- */
238
- readonly type: "visit";
239
- /**
240
- * The kind of the step. Represents the same data as the `type` property
241
- * but it's a number for performance.
242
- * @type {1}
243
- * @readonly
244
- */
245
- readonly kind: 1;
246
191
  /**
247
192
  * The target of the step.
248
193
  * @type {JSONNode}
249
194
  */
250
195
  target: JSONNode;
251
- /**
252
- * The phase of the step.
253
- * @type {1|2}
254
- */
255
- phase: 1 | 2;
256
- /**
257
- * The arguments of the step.
258
- * @type {Array<any>}
259
- */
260
- args: Array<any>;
261
196
  }
197
+ import { VisitNodeStep } from '@eslint/plugin-kit';
198
+ export { plugin as default };
package/dist/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // @ts-self-types="./index.d.ts"
2
2
  import { iterator, visitorKeys, parse } from '@humanwhocodes/momoa';
3
+ import { TextSourceCodeBase, VisitNodeStep } from '@eslint/plugin-kit';
3
4
 
4
5
  /**
5
6
  * @fileoverview The JSONSourceCode class.
@@ -27,41 +28,13 @@ import { iterator, visitorKeys, parse } from '@humanwhocodes/momoa';
27
28
 
28
29
  /**
29
30
  * A class to represent a step in the traversal process.
30
- * @implements {VisitTraversalStep}
31
31
  */
32
- class JSONTraversalStep {
33
- /**
34
- * The type of the step.
35
- * @type {"visit"}
36
- * @readonly
37
- */
38
- type = "visit";
39
-
40
- /**
41
- * The kind of the step. Represents the same data as the `type` property
42
- * but it's a number for performance.
43
- * @type {1}
44
- * @readonly
45
- */
46
- kind = 1;
47
-
32
+ class JSONTraversalStep extends VisitNodeStep {
48
33
  /**
49
34
  * The target of the step.
50
35
  * @type {JSONNode}
51
36
  */
52
- target;
53
-
54
- /**
55
- * The phase of the step.
56
- * @type {1|2}
57
- */
58
- phase;
59
-
60
- /**
61
- * The arguments of the step.
62
- * @type {Array<any>}
63
- */
64
- args;
37
+ target = undefined;
65
38
 
66
39
  /**
67
40
  * Creates a new instance.
@@ -71,9 +44,9 @@ class JSONTraversalStep {
71
44
  * @param {Array<any>} options.args The arguments of the step.
72
45
  */
73
46
  constructor({ target, phase, args }) {
47
+ super({ target, phase, args });
48
+
74
49
  this.target = target;
75
- this.phase = phase;
76
- this.args = args;
77
50
  }
78
51
  }
79
52
 
@@ -83,9 +56,8 @@ class JSONTraversalStep {
83
56
 
84
57
  /**
85
58
  * JSON Source Code Object
86
- * @implements {TextSourceCode}
87
59
  */
88
- class JSONSourceCode {
60
+ class JSONSourceCode extends TextSourceCodeBase {
89
61
  /**
90
62
  * Cached traversal steps.
91
63
  * @type {Array<JSONTraversalStep>|undefined}
@@ -98,23 +70,11 @@ class JSONSourceCode {
98
70
  */
99
71
  #parents = new WeakMap();
100
72
 
101
- /**
102
- * The lines of text in the source code.
103
- * @type {Array<string>}
104
- */
105
- #lines;
106
-
107
73
  /**
108
74
  * The AST of the source code.
109
75
  * @type {DocumentNode}
110
76
  */
111
- ast;
112
-
113
- /**
114
- * The text of the source code.
115
- * @type {string}
116
- */
117
- text;
77
+ ast = undefined;
118
78
 
119
79
  /**
120
80
  * The comment node in the source code.
@@ -129,35 +89,13 @@ class JSONSourceCode {
129
89
  * @param {DocumentNode} options.ast The root AST node.
130
90
  */
131
91
  constructor({ text, ast }) {
92
+ super({ text, ast });
132
93
  this.ast = ast;
133
- this.text = text;
134
- this.comments = ast.tokens.filter(token =>
135
- token.type.endsWith("Comment"),
136
- );
137
- }
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;
94
+ this.comments = ast.tokens
95
+ ? ast.tokens.filter(token => token.type.endsWith("Comment"))
96
+ : [];
148
97
  }
149
98
 
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
-
161
99
  /**
162
100
  * Returns the parent of the given node.
163
101
  * @param {JSONNode} node The node to get the parent of.
@@ -167,61 +105,6 @@ class JSONSourceCode {
167
105
  return this.#parents.get(node);
168
106
  }
169
107
 
170
- /**
171
- * Gets all the ancestors of a given node
172
- * @param {JSONNode} node The node
173
- * @returns {Array<JSONNode>} All the ancestor nodes in the AST, not including the provided node, starting
174
- * from the root node at index 0 and going inwards to the parent node.
175
- * @throws {TypeError} When `node` is missing.
176
- */
177
- getAncestors(node) {
178
- if (!node) {
179
- throw new TypeError("Missing required argument: node.");
180
- }
181
-
182
- const ancestorsStartingAtParent = [];
183
-
184
- for (
185
- let ancestor = this.#parents.get(node);
186
- ancestor;
187
- ancestor = this.#parents.get(ancestor)
188
- ) {
189
- ancestorsStartingAtParent.push(ancestor);
190
- }
191
-
192
- return ancestorsStartingAtParent.reverse();
193
- }
194
-
195
- /**
196
- * Gets the source code for the given node.
197
- * @param {JSONNode} [node] The AST node to get the text for.
198
- * @param {number} [beforeCount] The number of characters before the node to retrieve.
199
- * @param {number} [afterCount] The number of characters after the node to retrieve.
200
- * @returns {string} The text representing the AST node.
201
- * @public
202
- */
203
- getText(node, beforeCount, afterCount) {
204
- if (node) {
205
- return this.text.slice(
206
- Math.max(node.range[0] - (beforeCount || 0), 0),
207
- node.range[1] + (afterCount || 0),
208
- );
209
- }
210
- return this.text;
211
- }
212
-
213
- /**
214
- * Gets the entire source text split into an array of lines.
215
- * @returns {Array} The source text as an array of lines.
216
- * @public
217
- */
218
- get lines() {
219
- if (!this.#lines) {
220
- this.#lines = this.text.split(/\r?\n/gu);
221
- }
222
- return this.#lines;
223
- }
224
-
225
108
  /**
226
109
  * Traverse the source code and return the steps that were taken.
227
110
  * @returns {Iterable<JSONTraversalStep>} The steps that were taken while traversing the source code.
@@ -236,7 +119,10 @@ class JSONSourceCode {
236
119
  const steps = (this.#steps = []);
237
120
 
238
121
  for (const { node, parent, phase } of iterator(this.ast)) {
239
- this.#parents.set(node, parent);
122
+ if (parent) {
123
+ this.#parents.set(node, parent);
124
+ }
125
+
240
126
  steps.push(
241
127
  new JSONTraversalStep({
242
128
  target: node,
@@ -497,7 +383,7 @@ var noEmptyKeys = {
497
383
  const plugin = {
498
384
  meta: {
499
385
  name: "@eslint/json",
500
- version: "0.3.0", // x-release-please-version
386
+ version: "0.4.1", // x-release-please-version
501
387
  },
502
388
  languages: {
503
389
  json: new JSONLanguage({ mode: "json" }),
@@ -521,4 +407,4 @@ Object.assign(plugin.configs, {
521
407
  },
522
408
  });
523
409
 
524
- export { plugin as default };
410
+ export { JSONLanguage, JSONSourceCode, plugin as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint/json",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "JSON linting plugin for ESLint",
5
5
  "author": "Nicholas C. Zakas",
6
6
  "type": "module",
@@ -61,6 +61,7 @@
61
61
  ],
62
62
  "license": "Apache-2.0",
63
63
  "dependencies": {
64
+ "@eslint/plugin-kit": "^0.1.0",
64
65
  "@humanwhocodes/momoa": "^3.2.0"
65
66
  },
66
67
  "devDependencies": {
@@ -79,8 +80,5 @@
79
80
  },
80
81
  "engines": {
81
82
  "node": "^18.18.0 || ^20.9.0 || >=21.1.0"
82
- },
83
- "peerDependencies": {
84
- "eslint": "^9.6.0"
85
83
  }
86
84
  }