-
-
Notifications
You must be signed in to change notification settings - Fork 10
docs: create dedicated documentation for JSON #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
06a8a48
docs: create dedicated documentation for JSON
lumirlumir b094570
wip: fix a typo
lumirlumir b08c275
wip: update `README.md`
lumirlumir 119e8f7
wip: update template
lumirlumir a85d9ec
wip: update URL and ESLint config file
lumirlumir 2262118
wip
lumirlumir 3773815
wip: recommended
lumirlumir aebaa41
wip
lumirlumir 02c8d60
wip
lumirlumir f3c507e
wip: complete `no-duplicate-keys.md`
lumirlumir 64e034c
wip: complete `no-empty-keys.md`
lumirlumir 7c94b3e
wip: update `no-empty-keys`
lumirlumir f0f0e4b
wip: complete `no-unnormalized-keys.md`
lumirlumir b46e489
wip: fix link
lumirlumir 99bc347
wip: complete `no-unsafe-values.md`
lumirlumir 246837c
wip: prettier ignore
lumirlumir 649fe06
Update no-unnormalized-keys.md
lumirlumir 85a5f41
Update no-unsafe-values.md
lumirlumir 4b03dc6
wip
lumirlumir 4bc724e
Merge branch 'docs-create-dedicated-docs-for-json' of https://github.…
lumirlumir 25e2a0c
wip
lumirlumir 176c71a
wip: complete `sort-keys.md`
lumirlumir 8246317
wip: complete `top-level-interop.md`
lumirlumir a04b4c7
Merge branch 'main' into docs-create-dedicated-docs-for-json
lumirlumir b1d520b
Update docs/rules/no-duplicate-keys.md
lumirlumir d37c348
Update docs/rules/no-duplicate-keys.md
lumirlumir ba3bde9
Update docs/rules/no-duplicate-keys.md
lumirlumir 702e7ec
Update docs/rules/no-duplicate-keys.md
lumirlumir 6daff33
Update no-duplicate-keys.md
lumirlumir e86669e
Update docs/rules/no-empty-keys.md
lumirlumir c9c5dcf
Update docs/rules/no-unnormalized-keys.md
lumirlumir e1d4efb
Update docs/rules/no-unnormalized-keys.md
lumirlumir 4dedb7b
Update docs/rules/no-unsafe-values.md
lumirlumir 949bc16
Update docs/rules/sort-keys.md
lumirlumir 81d1ea8
Update docs/rules/sort-keys.md
lumirlumir 7f7d1c4
Update docs/rules/top-level-interop.md
lumirlumir 120eca4
Update docs/rules/top-level-interop.md
lumirlumir ea09952
wip: formatting
lumirlumir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# no-duplicate-keys | ||
|
||
Disallow duplicate keys in JSON objects. | ||
|
||
## Background | ||
|
||
The JSON specification doesn't explicitly forbid duplicate keys, but using them is considered a bad practice as it can lead to confusion and errors. Most parsers will only use the last occurrence of a duplicate key while others will throw an error. As such, it's considered a best practice to only have unique keys in JSON objects. | ||
|
||
## Rule Details | ||
|
||
This rule warns when there are two keys in an object with the same text, including when Unicode escape sequences resolve to the same character. For JSON5, this rule also considers identifier keys with the same text as string keys. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```json5 | ||
/* eslint json/no-duplicate-keys: "error" */ | ||
|
||
{ | ||
"foo": 1, | ||
"foo": 2 | ||
} | ||
|
||
{ | ||
"foo": 1, | ||
foo: 2 // Working with JSON5 | ||
} | ||
|
||
{ | ||
"foo": { | ||
"bar": 5 | ||
}, | ||
"foo": 6 | ||
} | ||
|
||
// With Unicode escapes that resolve to the same key | ||
{ | ||
"f\u006fot": 1, | ||
"fo\u006ft": 2 | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsonc | ||
/* eslint json/no-duplicate-keys: "error" */ | ||
|
||
{ | ||
"foo": 1, | ||
"bar": 2 | ||
} | ||
|
||
{ | ||
"foo": 1, | ||
"bar": 2, | ||
"baz": 3 | ||
} | ||
|
||
// Empty objects are valid | ||
{} | ||
|
||
// Nested objects with the same keys at different levels are valid | ||
{ | ||
"foo": 1, | ||
"bar": { | ||
"bar": 2 | ||
} | ||
} | ||
|
||
{ | ||
"foo": { | ||
"bar": 5 | ||
}, | ||
"bar": 6 | ||
} | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
You might want to disable this rule in the rare case where you intentionally need to include duplicate keys in a JSON document for interoperability with systems that rely on this pattern. However, this is not recommended and usually indicates a design issue in the consuming application. | ||
|
||
If you're working with non-standard JSON processors that have specific semantics for duplicate keys (such as using them for array-like constructs), you might need to disable this rule. However, consider using a more appropriate data structure or format for such cases. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# no-empty-keys | ||
|
||
Disallow empty keys in JSON objects. | ||
|
||
## Background | ||
|
||
In JSON, using empty keys (keys that are empty strings or contain only whitespace) can lead to accessibility and maintenance issues. While technically valid in JSON, empty keys make objects harder to read, can cause confusion when debugging, and may create problems with some JSON parsers or processors. Additionally, empty keys often indicate mistakes or oversights in the processes. | ||
|
||
> [!NOTE] | ||
> | ||
> [`package-lock.json`](https://docs.npmjs.com/cli/configuring-npm/package-lock-json) uses empty keys intentionally. | ||
|
||
## Rule Details | ||
|
||
This rule warns when there is a key in an object that is an empty string or contains only whitespace. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsonc | ||
/* eslint json/no-empty-keys: "error" */ | ||
|
||
{ | ||
"": "value" | ||
} | ||
|
||
{ | ||
"validKey": "value", | ||
"": "another value" | ||
} | ||
|
||
{ | ||
" ": "space as key" | ||
} | ||
|
||
{ | ||
"\t": "tab as key" | ||
} | ||
|
||
{ | ||
"\n": "newline as key" | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsonc | ||
/* eslint json/no-empty-keys: "error" */ | ||
|
||
{ | ||
"key": "value" | ||
} | ||
|
||
{ | ||
"id": 1, | ||
"name": "product" | ||
} | ||
|
||
// All keys must contain at least one non-whitespace character | ||
{ | ||
"key1": "value1", | ||
"key2": { | ||
"nestedKey": "nested value" | ||
} | ||
} | ||
|
||
// Empty objects are valid | ||
{} | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
You might want to disable this rule when working with specific JSON files that intentionally use empty keys as part of their schema or format. For example, as noted in the background, `package-lock.json` files sometimes use empty keys by design. | ||
|
||
If you're working with JSON data from external systems that you cannot modify, and these systems use empty keys in their response format, you might need to disable this rule for those specific files. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# no-unnormalized-keys | ||
|
||
Disallow JSON keys that are not normalized. | ||
|
||
## Background | ||
|
||
Unicode characters can sometimes have multiple representations that look identical but are technically different character sequences. For example, the character "é" can be represented as a single code point (U+00E9) or as an "e" followed by a combining accent (U+0065 + U+0301). This can lead to confusion, comparison issues, and unexpected behavior when working with JSON data. | ||
|
||
Using normalized Unicode ensures consistent representation of text, which is important for key comparison, sorting, and searching operations. When keys are properly normalized, operations like key lookups and equality checks will work as expected across different systems and platforms. | ||
|
||
## Rule Details | ||
|
||
This rule checks that all object keys in your JSON are properly normalized according to Unicode normalization forms. By default, it uses the NFC normalization form, but you can optionally specify the normalization form via `{ form: "form_name" }`, where `form_name` can be any of `"NFC"`, `"NFD"`, `"NFKC"`, or `"NFKD"`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsonc | ||
/* eslint json/no-unnormalized-keys: "error" */ | ||
|
||
// Key using decomposed form (NFD) instead of composed form (NFC) | ||
{ | ||
"cafe\u0301": "value" // é as 'e' + combining accent | ||
} | ||
|
||
// Different representations of the same visual character | ||
{ | ||
"cafè": "espresso", | ||
"cafe\u0300": "latte" // same visual character in a different form | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsonc | ||
/* eslint json/no-unnormalized-keys: "error" */ | ||
|
||
// Using NFC (default) normalized form | ||
{ | ||
"café": "value" // é as a single code point | ||
} | ||
|
||
// All keys properly normalized | ||
{ | ||
"résumé": "document", | ||
"naïve": "approach", | ||
"piñata": "party" | ||
} | ||
|
||
// ASCII-only keys are always normalized | ||
{ | ||
"simple": "value", | ||
"no_special_chars": true | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
The following options are available on this rule: | ||
|
||
- `form: "NFC" | "NFD" | "NFKC" | "NFKD"` - specifies which Unicode normalization form to use when checking keys. Must be one of: `"NFC"` (default), `"NFD"`, `"NFKC"`, or `"NFKD"`. | ||
|
||
Each normalization form has specific characteristics: | ||
|
||
- **NFC**: Canonical Decomposition followed by Canonical Composition (default) | ||
- **NFD**: Canonical Decomposition | ||
- **NFKC**: Compatibility Decomposition followed by Canonical Composition | ||
- **NFKD**: Compatibility Decomposition | ||
|
||
Examples of **incorrect** code when configured as `"no-unnormalized-keys": ["error", { form: "NFD" }]`: | ||
|
||
```jsonc | ||
/* eslint json/no-unnormalized-keys: ["error", { form: "NFD" }] */ | ||
|
||
{ | ||
"café": "value", // é as a single code point is invalid in NFD | ||
} | ||
``` | ||
|
||
Examples of **correct** code when configured as `"no-unnormalized-keys": ["error", { form: "NFD" }]`: | ||
|
||
```jsonc | ||
/* eslint json/no-unnormalized-keys: ["error", { form: "NFD" }] */ | ||
|
||
{ | ||
"cafe\u0301": "value", // é represented as 'e' + combining accent is valid in NFD | ||
} | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
You might want to disable this rule if: | ||
|
||
1. You're working with JSON data from external systems that you cannot modify, and normalizing the keys would break compatibility with those systems. | ||
1. Your project specifically needs to maintain different Unicode representations for technical reasons. | ||
1. Your JSON processing tools or environment have specific requirements regarding Unicode normalization that differ from the standard forms. | ||
|
||
In most cases, however, following a consistent normalization form improves interoperability and prevents subtle bugs. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.