@eslint/json 0.5.0 → 0.6.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 CHANGED
@@ -185,6 +185,35 @@ In JSONC and JSON5 files, you can also use [rule configurations comments](https:
185
185
 
186
186
  Both line and block comments can be used for all kinds of configuration comments.
187
187
 
188
+ ## Allowing trailing commas in JSONC
189
+
190
+ The Microsoft implementation of JSONC optionally allows for trailing commas in objects and arrays (files like `tsconfig.json` have this option enabled by default in Visual Studio Code). To enable trailing commas in JSONC files, use the `allowTrailingCommas` language option, as in this example:
191
+
192
+ ```js
193
+ import json from "@eslint/json";
194
+
195
+ export default [
196
+ // lint JSONC files
197
+ {
198
+ files: ["**/*.jsonc"],
199
+ language: "json/jsonc",
200
+ ...json.configs.recommended,
201
+ },
202
+
203
+ // lint JSONC files and allow trailing commas
204
+ {
205
+ files: ["**/tsconfig.json", ".vscode/*.json"],
206
+ language: "json/jsonc",
207
+ languageOptions: {
208
+ allowTrailingCommas: true,
209
+ },
210
+ ...json.configs.recommended,
211
+ },
212
+ ];
213
+ ```
214
+
215
+ **Note:** The `allowTrailingCommas` option is only valid for the `json/jsonc` language.
216
+
188
217
  ## Frequently Asked Questions
189
218
 
190
219
  ### How does this relate to `eslint-plugin-json` and `eslint-plugin-jsonc`?
@@ -208,11 +237,20 @@ Any other rules that catch potential problems in JSON are welcome to be implemen
208
237
 
209
238
  Apache 2.0
210
239
 
211
- ## Sponsors
212
-
213
240
  <!-- NOTE: This section is autogenerated. Do not manually edit.-->
214
241
  <!--sponsorsstart-->
215
- <!--sponsorsend-->
216
242
 
217
- <!--techsponsorsstart-->
218
- <!--techsponsorsend-->
243
+ ## Sponsors
244
+
245
+ The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. [Become a Sponsor](https://eslint.org/donate)
246
+ to get your logo on our READMEs and [website](https://eslint.org/sponsors).
247
+
248
+ <h3>Platinum Sponsors</h3>
249
+ <p><a href="https://automattic.com"><img src="https://images.opencollective.com/automattic/d0ef3e1/logo.png" alt="Automattic" height="128"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="128"></a></p><h3>Gold Sponsors</h3>
250
+ <p><a href="https://trunk.io/"><img src="https://images.opencollective.com/trunkio/fb92d60/avatar.png" alt="trunk.io" height="96"></a></p><h3>Silver Sponsors</h3>
251
+ <p><a href="https://www.serptriumph.com/"><img src="https://images.opencollective.com/serp-triumph5/fea3074/logo.png" alt="SERP Triumph" height="64"></a> <a href="https://www.jetbrains.com/"><img src="https://images.opencollective.com/jetbrains/fe76f99/logo.png" alt="JetBrains" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a> <a href="https://www.workleap.com"><img src="https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4" alt="Workleap" height="64"></a></p><h3>Bronze Sponsors</h3>
252
+ <p><a href="https://cybozu.co.jp/"><img src="https://images.opencollective.com/cybozu/933e46d/logo.png" alt="Cybozu" height="32"></a> <a href="https://www.wordhint.net/"><img src="https://images.opencollective.com/wordhint/be86813/avatar.png" alt="WordHint" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.gitbook.com"><img src="https://avatars.githubusercontent.com/u/7111340?v=4" alt="GitBook" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104?v=4" alt="Nx" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a></p>
253
+ <h3>Technology Sponsors</h3>
254
+ Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.
255
+ <p><a href="https://netlify.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/netlify-icon.svg" alt="Netlify" height="32"></a> <a href="https://algolia.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/algolia-icon.svg" alt="Algolia" height="32"></a> <a href="https://1password.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/1password-icon.svg" alt="1Password" height="32"></a></p>
256
+ <!--sponsorsend-->
@@ -296,6 +296,10 @@ class JSONSourceCode extends pluginKit.TextSourceCodeBase {
296
296
  /** @typedef {import("@eslint/core").Language} Language */
297
297
  /** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
298
298
  /** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
299
+ /**
300
+ * @typedef {Object} JSONLanguageOptions
301
+ * @property {boolean} [allowTrailingCommas] Whether to allow trailing commas.
302
+ */
299
303
 
300
304
  //-----------------------------------------------------------------------------
301
305
  // Exports
@@ -351,26 +355,42 @@ class JSONLanguage {
351
355
  this.#mode = mode;
352
356
  }
353
357
 
354
- /* eslint-disable class-methods-use-this, no-unused-vars -- Required to complete interface. */
355
358
  /**
356
359
  * Validates the language options.
357
- * @param {Object} languageOptions The language options to validate.
360
+ * @param {JSONLanguageOptions} languageOptions The language options to validate.
358
361
  * @returns {void}
359
362
  * @throws {Error} When the language options are invalid.
360
363
  */
361
364
  validateLanguageOptions(languageOptions) {
362
- // no-op
365
+ if (languageOptions.allowTrailingCommas !== undefined) {
366
+ if (typeof languageOptions.allowTrailingCommas !== "boolean") {
367
+ throw new Error(
368
+ "allowTrailingCommas must be a boolean if provided.",
369
+ );
370
+ }
371
+
372
+ // we know that allowTrailingCommas is a boolean here
373
+
374
+ // only allowed in JSONC mode
375
+ if (this.#mode !== "jsonc") {
376
+ throw new Error(
377
+ "allowTrailingCommas option is only available in JSONC.",
378
+ );
379
+ }
380
+ }
363
381
  }
364
- /* eslint-enable class-methods-use-this, no-unused-vars -- Required to complete interface. */
365
382
 
366
383
  /**
367
384
  * Parses the given file into an AST.
368
385
  * @param {File} file The virtual file to parse.
386
+ * @param {{languageOptions: JSONLanguageOptions}} context The options to use for parsing.
369
387
  * @returns {ParseResult} The result of parsing.
370
388
  */
371
- parse(file) {
389
+ parse(file, context) {
372
390
  // Note: BOM already removed
373
391
  const text = /** @type {string} */ (file.body);
392
+ const allowTrailingCommas =
393
+ context?.languageOptions?.allowTrailingCommas;
374
394
 
375
395
  /*
376
396
  * Check for parsing errors first. If there's a parsing error, nothing
@@ -383,6 +403,7 @@ class JSONLanguage {
383
403
  mode: this.#mode,
384
404
  ranges: true,
385
405
  tokens: true,
406
+ allowTrailingCommas,
386
407
  });
387
408
 
388
409
  return {
@@ -530,7 +551,7 @@ var noEmptyKeys = {
530
551
  const plugin = {
531
552
  meta: {
532
553
  name: "@eslint/json",
533
- version: "0.5.0", // x-release-please-version
554
+ version: "0.6.0", // x-release-please-version
534
555
  },
535
556
  languages: {
536
557
  json: new JSONLanguage({ mode: "json" }),
@@ -13,6 +13,12 @@ export type RulesConfig = import("@eslint/core").RulesConfig;
13
13
  export type Language = import("@eslint/core").Language;
14
14
  export type OkParseResult = import("@eslint/core").OkParseResult<DocumentNode>;
15
15
  export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
16
+ export type JSONLanguageOptions = {
17
+ /**
18
+ * Whether to allow trailing commas.
19
+ */
20
+ allowTrailingCommas?: boolean;
21
+ };
16
22
  /**
17
23
  * @filedescription Functions to fix up rules to provide missing methods on the `context` object.
18
24
  * @author Nicholas C. Zakas
@@ -20,6 +26,10 @@ export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
20
26
  /** @typedef {import("@eslint/core").Language} Language */
21
27
  /** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
22
28
  /** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
29
+ /**
30
+ * @typedef {Object} JSONLanguageOptions
31
+ * @property {boolean} [allowTrailingCommas] Whether to allow trailing commas.
32
+ */
23
33
  /**
24
34
  * JSON Language Object
25
35
  * @implements {Language}
@@ -60,17 +70,20 @@ export class JSONLanguage implements Language {
60
70
  visitorKeys: Record<string, string[]>;
61
71
  /**
62
72
  * Validates the language options.
63
- * @param {Object} languageOptions The language options to validate.
73
+ * @param {JSONLanguageOptions} languageOptions The language options to validate.
64
74
  * @returns {void}
65
75
  * @throws {Error} When the language options are invalid.
66
76
  */
67
- validateLanguageOptions(languageOptions: any): void;
77
+ validateLanguageOptions(languageOptions: JSONLanguageOptions): void;
68
78
  /**
69
79
  * Parses the given file into an AST.
70
80
  * @param {File} file The virtual file to parse.
81
+ * @param {{languageOptions: JSONLanguageOptions}} context The options to use for parsing.
71
82
  * @returns {ParseResult} The result of parsing.
72
83
  */
73
- parse(file: File): ParseResult;
84
+ parse(file: File, context: {
85
+ languageOptions: JSONLanguageOptions;
86
+ }): ParseResult;
74
87
  /**
75
88
  * Creates a new `JSONSourceCode` object from the given information.
76
89
  * @param {File} file The virtual file to create a `JSONSourceCode` object from.
@@ -13,6 +13,12 @@ export type RulesConfig = import("@eslint/core").RulesConfig;
13
13
  export type Language = import("@eslint/core").Language;
14
14
  export type OkParseResult = import("@eslint/core").OkParseResult<DocumentNode>;
15
15
  export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
16
+ export type JSONLanguageOptions = {
17
+ /**
18
+ * Whether to allow trailing commas.
19
+ */
20
+ allowTrailingCommas?: boolean;
21
+ };
16
22
  /**
17
23
  * @filedescription Functions to fix up rules to provide missing methods on the `context` object.
18
24
  * @author Nicholas C. Zakas
@@ -20,6 +26,10 @@ export type ParseResult = import("@eslint/core").ParseResult<DocumentNode>;
20
26
  /** @typedef {import("@eslint/core").Language} Language */
21
27
  /** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
22
28
  /** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
29
+ /**
30
+ * @typedef {Object} JSONLanguageOptions
31
+ * @property {boolean} [allowTrailingCommas] Whether to allow trailing commas.
32
+ */
23
33
  /**
24
34
  * JSON Language Object
25
35
  * @implements {Language}
@@ -60,17 +70,20 @@ export class JSONLanguage implements Language {
60
70
  visitorKeys: Record<string, string[]>;
61
71
  /**
62
72
  * Validates the language options.
63
- * @param {Object} languageOptions The language options to validate.
73
+ * @param {JSONLanguageOptions} languageOptions The language options to validate.
64
74
  * @returns {void}
65
75
  * @throws {Error} When the language options are invalid.
66
76
  */
67
- validateLanguageOptions(languageOptions: any): void;
77
+ validateLanguageOptions(languageOptions: JSONLanguageOptions): void;
68
78
  /**
69
79
  * Parses the given file into an AST.
70
80
  * @param {File} file The virtual file to parse.
81
+ * @param {{languageOptions: JSONLanguageOptions}} context The options to use for parsing.
71
82
  * @returns {ParseResult} The result of parsing.
72
83
  */
73
- parse(file: File): ParseResult;
84
+ parse(file: File, context: {
85
+ languageOptions: JSONLanguageOptions;
86
+ }): ParseResult;
74
87
  /**
75
88
  * Creates a new `JSONSourceCode` object from the given information.
76
89
  * @param {File} file The virtual file to create a `JSONSourceCode` object from.
package/dist/esm/index.js CHANGED
@@ -293,6 +293,10 @@ class JSONSourceCode extends TextSourceCodeBase {
293
293
  /** @typedef {import("@eslint/core").Language} Language */
294
294
  /** @typedef {import("@eslint/core").OkParseResult<DocumentNode>} OkParseResult */
295
295
  /** @typedef {import("@eslint/core").ParseResult<DocumentNode>} ParseResult */
296
+ /**
297
+ * @typedef {Object} JSONLanguageOptions
298
+ * @property {boolean} [allowTrailingCommas] Whether to allow trailing commas.
299
+ */
296
300
 
297
301
  //-----------------------------------------------------------------------------
298
302
  // Exports
@@ -348,26 +352,42 @@ class JSONLanguage {
348
352
  this.#mode = mode;
349
353
  }
350
354
 
351
- /* eslint-disable class-methods-use-this, no-unused-vars -- Required to complete interface. */
352
355
  /**
353
356
  * Validates the language options.
354
- * @param {Object} languageOptions The language options to validate.
357
+ * @param {JSONLanguageOptions} languageOptions The language options to validate.
355
358
  * @returns {void}
356
359
  * @throws {Error} When the language options are invalid.
357
360
  */
358
361
  validateLanguageOptions(languageOptions) {
359
- // no-op
362
+ if (languageOptions.allowTrailingCommas !== undefined) {
363
+ if (typeof languageOptions.allowTrailingCommas !== "boolean") {
364
+ throw new Error(
365
+ "allowTrailingCommas must be a boolean if provided.",
366
+ );
367
+ }
368
+
369
+ // we know that allowTrailingCommas is a boolean here
370
+
371
+ // only allowed in JSONC mode
372
+ if (this.#mode !== "jsonc") {
373
+ throw new Error(
374
+ "allowTrailingCommas option is only available in JSONC.",
375
+ );
376
+ }
377
+ }
360
378
  }
361
- /* eslint-enable class-methods-use-this, no-unused-vars -- Required to complete interface. */
362
379
 
363
380
  /**
364
381
  * Parses the given file into an AST.
365
382
  * @param {File} file The virtual file to parse.
383
+ * @param {{languageOptions: JSONLanguageOptions}} context The options to use for parsing.
366
384
  * @returns {ParseResult} The result of parsing.
367
385
  */
368
- parse(file) {
386
+ parse(file, context) {
369
387
  // Note: BOM already removed
370
388
  const text = /** @type {string} */ (file.body);
389
+ const allowTrailingCommas =
390
+ context?.languageOptions?.allowTrailingCommas;
371
391
 
372
392
  /*
373
393
  * Check for parsing errors first. If there's a parsing error, nothing
@@ -380,6 +400,7 @@ class JSONLanguage {
380
400
  mode: this.#mode,
381
401
  ranges: true,
382
402
  tokens: true,
403
+ allowTrailingCommas,
383
404
  });
384
405
 
385
406
  return {
@@ -527,7 +548,7 @@ var noEmptyKeys = {
527
548
  const plugin = {
528
549
  meta: {
529
550
  name: "@eslint/json",
530
- version: "0.5.0", // x-release-please-version
551
+ version: "0.6.0", // x-release-please-version
531
552
  },
532
553
  languages: {
533
554
  json: new JSONLanguage({ mode: "json" }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint/json",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "JSON linting plugin for ESLint",
5
5
  "author": "Nicholas C. Zakas",
6
6
  "type": "module",
@@ -44,6 +44,7 @@
44
44
  "build:dedupe-types": "node tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
45
45
  "build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"",
46
46
  "build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
47
+ "build:readme": "node tools/update-readme.js",
47
48
  "test:jsr": "npx jsr@latest publish --dry-run",
48
49
  "pretest": "npm run build",
49
50
  "lint": "eslint",
@@ -62,7 +63,7 @@
62
63
  "license": "Apache-2.0",
63
64
  "dependencies": {
64
65
  "@eslint/plugin-kit": "^0.2.0",
65
- "@humanwhocodes/momoa": "^3.2.1"
66
+ "@humanwhocodes/momoa": "^3.3.0"
66
67
  },
67
68
  "devDependencies": {
68
69
  "@eslint/core": "^0.6.0",
@@ -71,6 +72,7 @@
71
72
  "dedent": "^1.5.3",
72
73
  "eslint": "^9.11.1",
73
74
  "eslint-config-eslint": "^11.0.0",
75
+ "got": "^14.4.2",
74
76
  "lint-staged": "^15.2.7",
75
77
  "mocha": "^10.4.0",
76
78
  "prettier": "^3.3.2",