Skip to content

Commit

Permalink
feat: Add option to allow trailing commas in JSON and JSONC (#136)
Browse files Browse the repository at this point in the history
fixes #135
  • Loading branch information
nzakas authored Oct 15, 2024
1 parent 30440e7 commit 76e23f4
Show file tree
Hide file tree
Showing 14 changed files with 1,880 additions and 22 deletions.
19 changes: 19 additions & 0 deletions decisions/002-json-jsonc-trailing-commas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# JSON and JSONC Will Allow Trailing Commas via an Option

Date: 2024-10-15

Status: accepted

## Context

JSONC parsing was meant to be compatible with Microsoft's [JSONC parser](https://github.com/microsoft/node-jsonc-parser) and initially the JSONC parsing was simply JSON with JavaScript-style comments. It was [pointed out](https://github.com/humanwhocodes/momoa/issues/135) that files like `tsconfig.json` and `settings.json` actually allow dangling commas in addition to comments. Upon further investigation, the Microsoft JSONC parser defaults to [disallowing dangling commas](https://github.com/microsoft/node-jsonc-parser/blob/3c9b4203d663061d87d4d34dd0004690aef94db5/src/impl/parser.ts#L22) and is enabled as an exception of VS Code- and TypeScript-related files.

## Decision

Both JSON and JSONC parsing modes will have an `allowTrailingCommas` option that will default to `false`. This will allow users to determine whether they want to allow comments, trailing commas, or both.

## Consequences

For JSON and JSON5 users, there will be no immediate action necessary.

For JSONC users, they will need to update their options to allowing trailing commas when parsing VS Code- and TypeScript-related files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
[1, /*true,*/ null, "hi",]
---
{
"type": "Document",
"body": {
"type": "Array",
"elements": [
{
"type": "Element",
"value": {
"type": "Number",
"value": 1,
"loc": {
"start": {
"line": 1,
"column": 2,
"offset": 1
},
"end": {
"line": 1,
"column": 3,
"offset": 2
}
},
"range": [
1,
2
]
},
"loc": {
"start": {
"line": 1,
"column": 2,
"offset": 1
},
"end": {
"line": 1,
"column": 3,
"offset": 2
}
}
},
{
"type": "Element",
"value": {
"type": "Null",
"loc": {
"start": {
"line": 1,
"column": 15,
"offset": 14
},
"end": {
"line": 1,
"column": 19,
"offset": 18
}
},
"range": [
14,
18
]
},
"loc": {
"start": {
"line": 1,
"column": 15,
"offset": 14
},
"end": {
"line": 1,
"column": 19,
"offset": 18
}
}
},
{
"type": "Element",
"value": {
"type": "String",
"value": "hi",
"loc": {
"start": {
"line": 1,
"column": 21,
"offset": 20
},
"end": {
"line": 1,
"column": 25,
"offset": 24
}
},
"range": [
20,
24
]
},
"loc": {
"start": {
"line": 1,
"column": 21,
"offset": 20
},
"end": {
"line": 1,
"column": 25,
"offset": 24
}
}
}
],
"loc": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 27,
"offset": 26
}
},
"range": [
0,
26
]
},
"loc": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 27,
"offset": 26
}
},
"tokens": [
{
"type": "LBracket",
"loc": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 2,
"offset": 1
}
},
"range": [
0,
1
]
},
{
"type": "Number",
"loc": {
"start": {
"line": 1,
"column": 2,
"offset": 1
},
"end": {
"line": 1,
"column": 3,
"offset": 2
}
},
"range": [
1,
2
]
},
{
"type": "Comma",
"loc": {
"start": {
"line": 1,
"column": 3,
"offset": 2
},
"end": {
"line": 1,
"column": 4,
"offset": 3
}
},
"range": [
2,
3
]
},
{
"type": "BlockComment",
"loc": {
"start": {
"line": 1,
"column": 5,
"offset": 4
},
"end": {
"line": 1,
"column": 14,
"offset": 13
}
},
"range": [
4,
13
]
},
{
"type": "Null",
"loc": {
"start": {
"line": 1,
"column": 15,
"offset": 14
},
"end": {
"line": 1,
"column": 19,
"offset": 18
}
},
"range": [
14,
18
]
},
{
"type": "Comma",
"loc": {
"start": {
"line": 1,
"column": 19,
"offset": 18
},
"end": {
"line": 1,
"column": 20,
"offset": 19
}
},
"range": [
18,
19
]
},
{
"type": "String",
"loc": {
"start": {
"line": 1,
"column": 21,
"offset": 20
},
"end": {
"line": 1,
"column": 25,
"offset": 24
}
},
"range": [
20,
24
]
},
{
"type": "Comma",
"loc": {
"start": {
"line": 1,
"column": 25,
"offset": 24
},
"end": {
"line": 1,
"column": 26,
"offset": 25
}
},
"range": [
24,
25
]
},
{
"type": "RBracket",
"loc": {
"start": {
"line": 1,
"column": 26,
"offset": 25
},
"end": {
"line": 1,
"column": 27,
"offset": 26
}
},
"range": [
25,
26
]
}
],
"range": [
0,
26
]
}
Loading

0 comments on commit 76e23f4

Please sign in to comment.