@@ -70,14 +70,20 @@ function assertTree(input: string, expected: any, expectedErrors: ParseError[] =
70
70
interface VisitorCallback {
71
71
id : keyof JSONVisitor ,
72
72
text : string ;
73
+ startLine : number ;
74
+ startCharacter : number ;
73
75
arg ?: any ;
74
76
} ;
77
+ interface VisitorError extends ParseError {
78
+ startLine : number ;
79
+ startCharacter : number ;
80
+ }
75
81
76
- function assertVisit ( input : string , expected : VisitorCallback [ ] , expectedErrors : ParseError [ ] = [ ] , disallowComments = false ) : void {
77
- let errors : ParseError [ ] = [ ] ;
82
+ function assertVisit ( input : string , expected : VisitorCallback [ ] , expectedErrors : VisitorError [ ] = [ ] , disallowComments = false ) : void {
83
+ let errors : VisitorError [ ] = [ ] ;
78
84
let actuals : VisitorCallback [ ] = [ ] ;
79
- let noArgHalder = ( id : keyof JSONVisitor ) => ( offset : number , length : number ) => actuals . push ( { id, text : input . substr ( offset , length ) } ) ;
80
- let oneArgHalder = ( id : keyof JSONVisitor ) => ( arg : any , offset : number , length : number ) => actuals . push ( { id, text : input . substr ( offset , length ) , arg } ) ;
85
+ let noArgHalder = ( id : keyof JSONVisitor ) => ( offset : number , length : number , startLine : number , startCharacter : number ) => actuals . push ( { id, text : input . substr ( offset , length ) , startLine , startCharacter } ) ;
86
+ let oneArgHalder = ( id : keyof JSONVisitor ) => ( arg : any , offset : number , length : number , startLine : number , startCharacter : number ) => actuals . push ( { id, text : input . substr ( offset , length ) , startLine , startCharacter , arg } ) ;
81
87
visit ( input , {
82
88
onObjectBegin : noArgHalder ( 'onObjectBegin' ) ,
83
89
onObjectProperty : oneArgHalder ( 'onObjectProperty' ) ,
@@ -87,8 +93,8 @@ function assertVisit(input: string, expected: VisitorCallback[], expectedErrors:
87
93
onLiteralValue : oneArgHalder ( 'onLiteralValue' ) ,
88
94
onSeparator : oneArgHalder ( 'onSeparator' ) ,
89
95
onComment : noArgHalder ( 'onComment' ) ,
90
- onError : ( error : ParseErrorCode , offset : number , length : number ) => {
91
- errors . push ( { error, offset, length } )
96
+ onError : ( error : ParseErrorCode , offset : number , length : number , startLine : number , startCharacter : number ) => {
97
+ errors . push ( { error, offset, length, startLine , startCharacter } )
92
98
}
93
99
} , {
94
100
disallowComments
@@ -282,7 +288,7 @@ suite('JSON', () => {
282
288
assertInvalidParse ( '[ ,1, 2, 3, ]' , [ 1 , 2 , 3 ] ) ;
283
289
} ) ;
284
290
285
- test ( 'parse: disallow commments ' , ( ) => {
291
+ test ( 'parse: disallow comments ' , ( ) => {
286
292
let options = { disallowComments : true } ;
287
293
288
294
assertValidParse ( '[ 1, 2, null, "foo" ]' , [ 1 , 2 , null , 'foo' ] , options ) ;
@@ -422,21 +428,80 @@ suite('JSON', () => {
422
428
} ) ;
423
429
424
430
test ( 'visit: object' , ( ) => {
425
- assertVisit ( '{ }' , [ { id : 'onObjectBegin' , text : '{' } , { id : 'onObjectEnd' , text : '}' } ] ) ;
426
- assertVisit ( '{ "foo": "bar" }' , [ { id : 'onObjectBegin' , text : '{' } , { id : 'onObjectProperty' , text : '"foo"' , arg : 'foo' } , { id : 'onSeparator' , text : ':' , arg : ':' } , { id : 'onLiteralValue' , text : '"bar"' , arg : 'bar' } , { id : 'onObjectEnd' , text : '}' } ] ) ;
427
- assertVisit ( '{ "foo": { "goo": 3 } }' , [ { id : 'onObjectBegin' , text : '{' } , { id : 'onObjectProperty' , text : '"foo"' , arg : 'foo' } , { id : 'onSeparator' , text : ':' , arg : ':' } , { id : 'onObjectBegin' , text : '{' } , { id : 'onObjectProperty' , text : '"goo"' , arg : 'goo' } , { id : 'onSeparator' , text : ':' , arg : ':' } , { id : 'onLiteralValue' , text : '3' , arg : 3 } , { id : 'onObjectEnd' , text : '}' } , { id : 'onObjectEnd' , text : '}' } ] ) ;
431
+ assertVisit ( '{ }' , [ { id : 'onObjectBegin' , text : '{' , startLine : 0 , startCharacter : 0 } , { id : 'onObjectEnd' , text : '}' , startLine : 0 , startCharacter : 2 } ] ) ;
432
+ assertVisit ( '{ "foo": "bar" }' , [
433
+ { id : 'onObjectBegin' , text : '{' , startLine : 0 , startCharacter : 0 } ,
434
+ { id : 'onObjectProperty' , text : '"foo"' , startLine : 0 , startCharacter : 2 , arg : 'foo' } ,
435
+ { id : 'onSeparator' , text : ':' , startLine : 0 , startCharacter : 7 , arg : ':' } ,
436
+ { id : 'onLiteralValue' , text : '"bar"' , startLine : 0 , startCharacter : 9 , arg : 'bar' } ,
437
+ { id : 'onObjectEnd' , text : '}' , startLine : 0 , startCharacter : 15 } ,
438
+ ] ) ;
439
+ assertVisit ( '{ "foo": { "goo": 3 } }' , [
440
+ { id : 'onObjectBegin' , text : '{' , startLine : 0 , startCharacter : 0 } ,
441
+ { id : 'onObjectProperty' , text : '"foo"' , startLine : 0 , startCharacter : 2 , arg : 'foo' } ,
442
+ { id : 'onSeparator' , text : ':' , startLine : 0 , startCharacter : 7 , arg : ':' } ,
443
+ { id : 'onObjectBegin' , text : '{' , startLine : 0 , startCharacter : 9 } ,
444
+ { id : 'onObjectProperty' , text : '"goo"' , startLine : 0 , startCharacter : 11 , arg : 'goo' } ,
445
+ { id : 'onSeparator' , text : ':' , startLine : 0 , startCharacter : 16 , arg : ':' } ,
446
+ { id : 'onLiteralValue' , text : '3' , startLine : 0 , startCharacter : 18 , arg : 3 } ,
447
+ { id : 'onObjectEnd' , text : '}' , startLine : 0 , startCharacter : 20 } ,
448
+ { id : 'onObjectEnd' , text : '}' , startLine : 0 , startCharacter : 22 } ,
449
+ ] ) ;
428
450
} ) ;
429
451
430
452
test ( 'visit: array' , ( ) => {
431
- assertVisit ( '[]' , [ { id : 'onArrayBegin' , text : '[' } , { id : 'onArrayEnd' , text : ']' } ] ) ;
432
- assertVisit ( '[ true, null, [] ]' , [ { id : 'onArrayBegin' , text : '[' } , { id : 'onLiteralValue' , text : 'true' , arg : true } , { id : 'onSeparator' , text : ',' , arg : ',' } , { id : 'onLiteralValue' , text : 'null' , arg : null } , { id : 'onSeparator' , text : ',' , arg : ',' } , { id : 'onArrayBegin' , text : '[' } , { id : 'onArrayEnd' , text : ']' } , { id : 'onArrayEnd' , text : ']' } ] ) ;
453
+ assertVisit ( '[]' , [ { id : 'onArrayBegin' , text : '[' , startLine : 0 , startCharacter : 0 } , { id : 'onArrayEnd' , text : ']' , startLine : 0 , startCharacter : 1 } ] ) ;
454
+ assertVisit ( '[ true, null, [] ]' , [
455
+ { id : 'onArrayBegin' , text : '[' , startLine : 0 , startCharacter : 0 } ,
456
+ { id : 'onLiteralValue' , text : 'true' , startLine : 0 , startCharacter : 2 , arg : true } ,
457
+ { id : 'onSeparator' , text : ',' , startLine : 0 , startCharacter : 6 , arg : ',' } ,
458
+ { id : 'onLiteralValue' , text : 'null' , startLine : 0 , startCharacter : 8 , arg : null } ,
459
+ { id : 'onSeparator' , text : ',' , startLine : 0 , startCharacter : 12 , arg : ',' } ,
460
+ { id : 'onArrayBegin' , text : '[' , startLine : 0 , startCharacter : 14 } ,
461
+ { id : 'onArrayEnd' , text : ']' , startLine : 0 , startCharacter : 15 } ,
462
+ { id : 'onArrayEnd' , text : ']' , startLine : 0 , startCharacter : 17 } ,
463
+ ] ) ;
464
+ assertVisit ( '[\r\n0,\r\n1,\r\n2\r\n]' , [
465
+ { id : 'onArrayBegin' , text : '[' , startLine : 0 , startCharacter : 0 } ,
466
+ { id : 'onLiteralValue' , text : '0' , startLine : 1 , startCharacter : 0 , arg : 0 } ,
467
+ { id : 'onSeparator' , text : ',' , startLine : 1 , startCharacter : 1 , arg : ',' } ,
468
+ { id : 'onLiteralValue' , text : '1' , startLine : 2 , startCharacter : 0 , arg : 1 } ,
469
+ { id : 'onSeparator' , text : ',' , startLine : 2 , startCharacter : 1 , arg : ',' } ,
470
+ { id : 'onLiteralValue' , text : '2' , startLine : 3 , startCharacter : 0 , arg : 2 } ,
471
+ { id : 'onArrayEnd' , text : ']' , startLine : 4 , startCharacter : 0 } ] ) ;
433
472
} ) ;
434
473
435
474
test ( 'visit: comment' , ( ) => {
436
- assertVisit ( '/* g */ { "foo": //f\n"bar" }' , [ { id : 'onComment' , text : '/* g */' } , { id : 'onObjectBegin' , text : '{' } , { id : 'onObjectProperty' , text : '"foo"' , arg : 'foo' } , { id : 'onSeparator' , text : ':' , arg : ':' } , { id : 'onComment' , text : '//f' } , { id : 'onLiteralValue' , text : '"bar"' , arg : 'bar' } , { id : 'onObjectEnd' , text : '}' } ] ) ;
437
- assertVisit ( '/* g */ { "foo": //f\n"bar" }' ,
438
- [ { id : 'onObjectBegin' , text : '{' } , { id : 'onObjectProperty' , text : '"foo"' , arg : 'foo' } , { id : 'onSeparator' , text : ':' , arg : ':' } , { id : 'onLiteralValue' , text : '"bar"' , arg : 'bar' } , { id : 'onObjectEnd' , text : '}' } ] ,
439
- [ { error : ParseErrorCode . InvalidCommentToken , offset : 0 , length : 7 } , { error : ParseErrorCode . InvalidCommentToken , offset : 17 , length : 3 } ] ,
475
+ assertVisit ( '/* g */ { "foo": //f\n"bar" }' , [
476
+ { id : 'onComment' , text : '/* g */' , startLine : 0 , startCharacter : 0 } ,
477
+ { id : 'onObjectBegin' , text : '{' , startLine : 0 , startCharacter : 8 } ,
478
+ { id : 'onObjectProperty' , text : '"foo"' , startLine : 0 , startCharacter : 10 , arg : 'foo' } ,
479
+ { id : 'onSeparator' , text : ':' , startLine : 0 , startCharacter : 15 , arg : ':' } ,
480
+ { id : 'onComment' , text : '//f' , startLine : 0 , startCharacter : 17 } ,
481
+ { id : 'onLiteralValue' , text : '"bar"' , startLine : 1 , startCharacter : 0 , arg : 'bar' } ,
482
+ { id : 'onObjectEnd' , text : '}' , startLine : 1 , startCharacter : 6 } ,
483
+ ] ) ;
484
+ assertVisit ( '/* g\r\n */ { "foo": //f\n"bar" }' , [
485
+ { id : 'onComment' , text : '/* g\r\n */' , startLine : 0 , startCharacter : 0 } ,
486
+ { id : 'onObjectBegin' , text : '{' , startLine : 1 , startCharacter : 4 } ,
487
+ { id : 'onObjectProperty' , text : '"foo"' , startLine : 1 , startCharacter : 6 , arg : 'foo' } ,
488
+ { id : 'onSeparator' , text : ':' , startLine : 1 , startCharacter : 11 , arg : ':' } ,
489
+ { id : 'onComment' , text : '//f' , startLine : 1 , startCharacter : 13 } ,
490
+ { id : 'onLiteralValue' , text : '"bar"' , startLine : 2 , startCharacter : 0 , arg : 'bar' } ,
491
+ { id : 'onObjectEnd' , text : '}' , startLine : 2 , startCharacter : 6 } ,
492
+ ] ) ;
493
+ assertVisit ( '/* g\n */ { "foo": //f\n"bar"\n}' ,
494
+ [
495
+ { id : 'onObjectBegin' , text : '{' , startLine : 1 , startCharacter : 4 } ,
496
+ { id : 'onObjectProperty' , text : '"foo"' , startLine : 1 , startCharacter : 6 , arg : 'foo' } ,
497
+ { id : 'onSeparator' , text : ':' , startLine : 1 , startCharacter : 11 , arg : ':' } ,
498
+ { id : 'onLiteralValue' , text : '"bar"' , startLine : 2 , startCharacter : 0 , arg : 'bar' } ,
499
+ { id : 'onObjectEnd' , text : '}' , startLine : 3 , startCharacter : 0 } ,
500
+ ] ,
501
+ [
502
+ { error : ParseErrorCode . InvalidCommentToken , offset : 0 , length : 8 , startLine : 0 , startCharacter : 0 } ,
503
+ { error : ParseErrorCode . InvalidCommentToken , offset : 18 , length : 3 , startLine : 1 , startCharacter : 13 } ,
504
+ ] ,
440
505
true ) ;
441
506
} ) ;
442
507
0 commit comments