@@ -1274,4 +1274,81 @@ describe('utils', () => {
1274
1274
} ) ;
1275
1275
} ) ;
1276
1276
} ) ;
1277
+
1278
+ describe ( 'evaluateObjectProperties' , function ( ) {
1279
+ it ( 'behaves correctly with simple object expression' , function ( ) {
1280
+ const ast = espree . parse ( 'const obj = { a: 123, b: foo() };' , {
1281
+ ecmaVersion : 9 ,
1282
+ range : true ,
1283
+ } ) ;
1284
+ const scopeManager = eslintScope . analyze ( ast ) ;
1285
+ const result = utils . evaluateObjectProperties (
1286
+ ast . body [ 0 ] . declarations [ 0 ] . init ,
1287
+ scopeManager
1288
+ ) ;
1289
+ assert . deepEqual ( result , ast . body [ 0 ] . declarations [ 0 ] . init . properties ) ;
1290
+ } ) ;
1291
+
1292
+ it ( 'behaves correctly with spreads of objects' , function ( ) {
1293
+ const ast = espree . parse (
1294
+ `
1295
+ const extra1 = { a: 123 };
1296
+ const extra2 = { b: 456 };
1297
+ const obj = { ...extra1, c: 789, ...extra2 };
1298
+ ` ,
1299
+ {
1300
+ ecmaVersion : 9 ,
1301
+ range : true ,
1302
+ }
1303
+ ) ;
1304
+ const scopeManager = eslintScope . analyze ( ast ) ;
1305
+ const result = utils . evaluateObjectProperties (
1306
+ ast . body [ 2 ] . declarations [ 0 ] . init ,
1307
+ scopeManager
1308
+ ) ;
1309
+ assert . deepEqual ( result , [
1310
+ ...ast . body [ 0 ] . declarations [ 0 ] . init . properties , // First spread properties
1311
+ ...ast . body [ 2 ] . declarations [ 0 ] . init . properties . filter (
1312
+ ( property ) => property . type !== 'SpreadElement'
1313
+ ) , // Non-spread properties
1314
+ ...ast . body [ 1 ] . declarations [ 0 ] . init . properties , // Second spread properties
1315
+ ] ) ;
1316
+ } ) ;
1317
+
1318
+ it ( 'behaves correctly with non-variable spreads' , function ( ) {
1319
+ const ast = espree . parse ( `function foo() {} const obj = { ...foo() };` , {
1320
+ ecmaVersion : 9 ,
1321
+ range : true ,
1322
+ } ) ;
1323
+ const scopeManager = eslintScope . analyze ( ast ) ;
1324
+ const result = utils . evaluateObjectProperties (
1325
+ ast . body [ 1 ] . declarations [ 0 ] . init ,
1326
+ scopeManager
1327
+ ) ;
1328
+ assert . deepEqual ( result , [ ] ) ;
1329
+ } ) ;
1330
+
1331
+ it ( 'behaves correctly with spread with variable that cannot be found' , function ( ) {
1332
+ const ast = espree . parse ( `const obj = { ...foo };` , {
1333
+ ecmaVersion : 9 ,
1334
+ range : true ,
1335
+ } ) ;
1336
+ const scopeManager = eslintScope . analyze ( ast ) ;
1337
+ const result = utils . evaluateObjectProperties (
1338
+ ast . body [ 0 ] . declarations [ 0 ] . init ,
1339
+ scopeManager
1340
+ ) ;
1341
+ assert . deepEqual ( result , [ ] ) ;
1342
+ } ) ;
1343
+
1344
+ it ( 'behaves correctly when passed wrong node type' , function ( ) {
1345
+ const ast = espree . parse ( `foo();` , {
1346
+ ecmaVersion : 9 ,
1347
+ range : true ,
1348
+ } ) ;
1349
+ const scopeManager = eslintScope . analyze ( ast ) ;
1350
+ const result = utils . evaluateObjectProperties ( ast . body [ 0 ] , scopeManager ) ;
1351
+ assert . deepEqual ( result , [ ] ) ;
1352
+ } ) ;
1353
+ } ) ;
1277
1354
} ) ;
0 commit comments