Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 9ff2482

Browse files
thorn0Brocco
authored andcommitted
feat(@angular-devkit/core): allow trailing commas in loose JSON
1 parent 68da660 commit 9ff2482

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/angular_devkit/core/src/json/parser.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,11 @@ function _readArray(context: JsonParserContext, comments = _readBlanks(context))
378378
while (_peek(context) != ']') {
379379
_token(context, ',');
380380

381-
const node = _readValue(context);
381+
const valueComments = _readBlanks(context);
382+
if ((context.mode & JsonParseMode.TrailingCommasAllowed) !== 0 && _peek(context) === ']') {
383+
break;
384+
}
385+
const node = _readValue(context, valueComments);
382386
elements.push(node);
383387
value.push(node.value);
384388
}
@@ -505,7 +509,11 @@ function _readObject(context: JsonParserContext,
505509
while (_peek(context) != '}') {
506510
_token(context, ',');
507511

508-
const property = _readProperty(context);
512+
const propertyComments = _readBlanks(context);
513+
if ((context.mode & JsonParseMode.TrailingCommasAllowed) !== 0 && _peek(context) === '}') {
514+
break;
515+
}
516+
const property = _readProperty(context, propertyComments);
509517
value[property.key.value] = property.value.value;
510518
properties.push(property);
511519
}
@@ -607,11 +615,10 @@ function _readBlanks(context: JsonParserContext): (JsonAstComment | JsonAstMulti
607615
* Read a JSON value from the context, which can be any form of JSON value.
608616
* @private
609617
*/
610-
function _readValue(context: JsonParserContext): JsonAstNode {
618+
function _readValue(context: JsonParserContext, comments = _readBlanks(context)): JsonAstNode {
611619
let result: JsonAstNode;
612620

613621
// Clean up before.
614-
const comments = _readBlanks(context);
615622
const char = _peek(context);
616623
switch (char) {
617624
case undefined:
@@ -673,9 +680,11 @@ export enum JsonParseMode {
673680
CommentsAllowed = 1 << 0, // Allows comments, both single or multi lines.
674681
SingleQuotesAllowed = 1 << 1, // Allow single quoted strings.
675682
IdentifierKeyNamesAllowed = 1 << 2, // Allow identifiers as objectp properties.
683+
TrailingCommasAllowed = 1 << 3,
676684

677685
Default = Strict,
678-
Loose = CommentsAllowed | SingleQuotesAllowed | IdentifierKeyNamesAllowed,
686+
Loose = CommentsAllowed | SingleQuotesAllowed |
687+
IdentifierKeyNamesAllowed | TrailingCommasAllowed,
679688
}
680689

681690

packages/angular_devkit/core/src/json/parser_spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ describe('parseJson and parseJsonAst', () => {
240240
'{\n/* hello\n*/ }': [[0, 0, 0], [15, 2, 4], {}],
241241
'{}// ': [[0, 0, 0], [2, 0, 2], {}, '{}'],
242242
'{}//': [[0, 0, 0], [2, 0, 2], {}, '{}'],
243+
'{hello:0,}': [[0, 0, 0], [10, 0, 10], {hello: 0}],
244+
'{hello:0/**/,}': [[0, 0, 0], [14, 0, 14], {hello: 0}],
245+
'{hello:0,/**/}': [[0, 0, 0], [14, 0, 14], {hello: 0}],
246+
'{hi:["hello",]}': [[0, 0, 0], [15, 0, 15], {hi: ['hello']}],
247+
'{hi:["hello",/* */]}': [[0, 0, 0], [20, 0, 20], {hi: ['hello']}],
248+
'{hi:["hello"/* */,]}': [[0, 0, 0], [20, 0, 20], {hi: ['hello']}],
249+
'{hi:["hello" , ] , }': [[0, 0, 0], [20, 0, 20], {hi: ['hello']}],
243250
};
244251
const errors = [
245252
'{1b: 0}',

0 commit comments

Comments
 (0)