@@ -1250,6 +1250,7 @@ describe('parser', function() {
1250
1250
type : 'Property' ,
1251
1251
kind : 'init' ,
1252
1252
key : { type : 'Identifier' , name : 'foo' } ,
1253
+ computed : false ,
1253
1254
value : { type : 'Identifier' , name : 'bar' }
1254
1255
}
1255
1256
]
@@ -1271,6 +1272,7 @@ describe('parser', function() {
1271
1272
type : 'Property' ,
1272
1273
kind : 'init' ,
1273
1274
key : { type : 'Identifier' , name : 'foo' } ,
1275
+ computed : false ,
1274
1276
value : { type : 'Identifier' , name : 'bar' }
1275
1277
}
1276
1278
]
@@ -1292,18 +1294,21 @@ describe('parser', function() {
1292
1294
type : 'Property' ,
1293
1295
kind : 'init' ,
1294
1296
key : { type : 'Identifier' , name : 'foo' } ,
1297
+ computed : false ,
1295
1298
value : { type : 'Identifier' , name : 'bar' }
1296
1299
} ,
1297
1300
{
1298
1301
type : 'Property' ,
1299
1302
kind : 'init' ,
1300
1303
key : { type : 'Literal' , value : 'man' } ,
1304
+ computed : false ,
1301
1305
value : { type : 'Literal' , value : 'shell' }
1302
1306
} ,
1303
1307
{
1304
1308
type : 'Property' ,
1305
1309
kind : 'init' ,
1306
1310
key : { type : 'Literal' , value : 42 } ,
1311
+ computed : false ,
1307
1312
value : { type : 'Literal' , value : 23 }
1308
1313
}
1309
1314
]
@@ -1325,18 +1330,21 @@ describe('parser', function() {
1325
1330
type : 'Property' ,
1326
1331
kind : 'init' ,
1327
1332
key : { type : 'Identifier' , name : 'foo' } ,
1333
+ computed : false ,
1328
1334
value : { type : 'Identifier' , name : 'bar' }
1329
1335
} ,
1330
1336
{
1331
1337
type : 'Property' ,
1332
1338
kind : 'init' ,
1333
1339
key : { type : 'Literal' , value : 'man' } ,
1340
+ computed : false ,
1334
1341
value : { type : 'Literal' , value : 'shell' }
1335
1342
} ,
1336
1343
{
1337
1344
type : 'Property' ,
1338
1345
kind : 'init' ,
1339
1346
key : { type : 'Literal' , value : 42 } ,
1347
+ computed : false ,
1340
1348
value : { type : 'Literal' , value : 23 }
1341
1349
}
1342
1350
]
@@ -1347,6 +1355,97 @@ describe('parser', function() {
1347
1355
) ;
1348
1356
} ) ;
1349
1357
1358
+ it ( 'should understand ES6 object initializer' , function ( ) {
1359
+ // Shorthand properties definitions.
1360
+ expect ( createAst ( '{x, y, z}' ) ) . toEqual (
1361
+ {
1362
+ type : 'Program' ,
1363
+ body : [
1364
+ {
1365
+ type : 'ExpressionStatement' ,
1366
+ expression : {
1367
+ type : 'ObjectExpression' ,
1368
+ properties : [
1369
+ {
1370
+ type : 'Property' ,
1371
+ kind : 'init' ,
1372
+ key : { type : 'Identifier' , name : 'x' } ,
1373
+ computed : false ,
1374
+ value : { type : 'Identifier' , name : 'x' }
1375
+ } ,
1376
+ {
1377
+ type : 'Property' ,
1378
+ kind : 'init' ,
1379
+ key : { type : 'Identifier' , name : 'y' } ,
1380
+ computed : false ,
1381
+ value : { type : 'Identifier' , name : 'y' }
1382
+ } ,
1383
+ {
1384
+ type : 'Property' ,
1385
+ kind : 'init' ,
1386
+ key : { type : 'Identifier' , name : 'z' } ,
1387
+ computed : false ,
1388
+ value : { type : 'Identifier' , name : 'z' }
1389
+ }
1390
+ ]
1391
+ }
1392
+ }
1393
+ ]
1394
+ }
1395
+ ) ;
1396
+ expect ( function ( ) { createAst ( '{"foo"}' ) ; } ) . toThrow ( ) ;
1397
+
1398
+ // Computed properties
1399
+ expect ( createAst ( '{[x]: x}' ) ) . toEqual (
1400
+ {
1401
+ type : 'Program' ,
1402
+ body : [
1403
+ {
1404
+ type : 'ExpressionStatement' ,
1405
+ expression : {
1406
+ type : 'ObjectExpression' ,
1407
+ properties : [
1408
+ {
1409
+ type : 'Property' ,
1410
+ kind : 'init' ,
1411
+ key : { type : 'Identifier' , name : 'x' } ,
1412
+ computed : true ,
1413
+ value : { type : 'Identifier' , name : 'x' }
1414
+ }
1415
+ ]
1416
+ }
1417
+ }
1418
+ ]
1419
+ }
1420
+ ) ;
1421
+ expect ( createAst ( '{[x + 1]: x}' ) ) . toEqual (
1422
+ {
1423
+ type : 'Program' ,
1424
+ body : [
1425
+ {
1426
+ type : 'ExpressionStatement' ,
1427
+ expression : {
1428
+ type : 'ObjectExpression' ,
1429
+ properties : [
1430
+ {
1431
+ type : 'Property' ,
1432
+ kind : 'init' ,
1433
+ key : {
1434
+ type : 'BinaryExpression' ,
1435
+ operator : '+' ,
1436
+ left : { type : 'Identifier' , name : 'x' } ,
1437
+ right : { type : 'Literal' , value : 1 }
1438
+ } ,
1439
+ computed : true ,
1440
+ value : { type : 'Identifier' , name : 'x' }
1441
+ }
1442
+ ]
1443
+ }
1444
+ }
1445
+ ]
1446
+ }
1447
+ ) ;
1448
+ } ) ;
1350
1449
1351
1450
it ( 'should understand multiple expressions' , function ( ) {
1352
1451
expect ( createAst ( 'foo = bar; man = shell' ) ) . toEqual (
@@ -1626,6 +1725,7 @@ describe('parser', function() {
1626
1725
type : 'Property' ,
1627
1726
kind : 'init' ,
1628
1727
key : { type : 'Identifier' , name : 'foo' } ,
1728
+ computed : false ,
1629
1729
value : {
1630
1730
type : 'AssignmentExpression' ,
1631
1731
left : { type : 'Identifier' , name : 'bar' } ,
@@ -2091,11 +2191,18 @@ describe('parser', function() {
2091
2191
expect ( scope . $eval ( "{false:1}" ) ) . toEqual ( { false :1 } ) ;
2092
2192
expect ( scope . $eval ( "{'false':1}" ) ) . toEqual ( { false :1 } ) ;
2093
2193
expect ( scope . $eval ( "{'':1,}" ) ) . toEqual ( { "" :1 } ) ;
2194
+
2195
+ // ES6 object initializers.
2196
+ expect ( scope . $eval ( '{x, y}' , { x : 'foo' , y : 'bar' } ) ) . toEqual ( { x : 'foo' , y : 'bar' } ) ;
2197
+ expect ( scope . $eval ( '{[x]: x}' , { x : 'foo' } ) ) . toEqual ( { foo : 'foo' } ) ;
2198
+ expect ( scope . $eval ( '{[x + "z"]: x}' , { x : 'foo' } ) ) . toEqual ( { fooz : 'foo' } ) ;
2199
+ expect ( scope . $eval ( '{x, 1: x, [x = x + 1]: x, 3: x + 1, [x = x + 2]: x, 5: x + 1}' , { x : 1 } ) )
2200
+ . toEqual ( { x : 1 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 } ) ;
2094
2201
} ) ;
2095
2202
2096
2203
it ( 'should throw syntax error exception for non constant/identifier JSON keys' , function ( ) {
2097
2204
expect ( function ( ) { scope . $eval ( "{[:0}" ) ; } ) . toThrowMinErr ( "$parse" , "syntax" ,
2098
- "Syntax Error: Token '[' invalid key at column 2 of the expression [{[:0}] starting at [ [:0}]" ) ;
2205
+ "Syntax Error: Token ':' not a primary expression at column 3 of the expression [{[:0}] starting at [:0}]" ) ;
2099
2206
expect ( function ( ) { scope . $eval ( "{{:0}" ) ; } ) . toThrowMinErr ( "$parse" , "syntax" ,
2100
2207
"Syntax Error: Token '{' invalid key at column 2 of the expression [{{:0}] starting at [{:0}]" ) ;
2101
2208
expect ( function ( ) { scope . $eval ( "{?:0}" ) ; } ) . toThrowMinErr ( "$parse" , "syntax" ,
@@ -3654,6 +3761,7 @@ describe('parser', function() {
3654
3761
expect ( $parse ( '"foo" + "bar"' ) . constant ) . toBe ( true ) ;
3655
3762
expect ( $parse ( '5 != null' ) . constant ) . toBe ( true ) ;
3656
3763
expect ( $parse ( '{standard: 4/3, wide: 16/9}' ) . constant ) . toBe ( true ) ;
3764
+ expect ( $parse ( '{[standard]: 4/3, wide: 16/9}' ) . constant ) . toBe ( false ) ;
3657
3765
} ) ) ;
3658
3766
3659
3767
it ( 'should not mark any expression involving variables or function calls as constant' , inject ( function ( $parse ) {
0 commit comments