@@ -332,6 +332,8 @@ function componentRule(rule, context) {
332
332
var j ;
333
333
var k ;
334
334
var l ;
335
+ var componentName ;
336
+ var componentNode ;
335
337
// Get the component path
336
338
var componentPath = [ ] ;
337
339
while ( node ) {
@@ -344,6 +346,7 @@ function componentRule(rule, context) {
344
346
node = node . object ;
345
347
}
346
348
componentPath . reverse ( ) ;
349
+ componentName = componentPath . slice ( 0 , componentPath . length - 1 ) . join ( '.' ) ;
347
350
348
351
// Find the variable in the current scope
349
352
var variableName = componentPath . shift ( ) ;
@@ -362,7 +365,31 @@ function componentRule(rule, context) {
362
365
return null ;
363
366
}
364
367
365
- // Find the variable declaration
368
+ // Try to find the component using variable references
369
+ var refs = variableInScope . references ;
370
+ var refId ;
371
+ for ( i = 0 , j = refs . length ; i < j ; i ++ ) {
372
+ refId = refs [ i ] . identifier ;
373
+ if ( refId . parent && refId . parent . type === 'MemberExpression' ) {
374
+ refId = refId . parent ;
375
+ }
376
+ if ( sourceCode . getText ( refId ) !== componentName ) {
377
+ continue ;
378
+ }
379
+ if ( refId . type === 'MemberExpression' ) {
380
+ componentNode = refId . parent . right ;
381
+ } else if ( refId . parent && refId . parent . type === 'VariableDeclarator' ) {
382
+ componentNode = refId . parent . init ;
383
+ }
384
+ break ;
385
+ }
386
+
387
+ if ( componentNode ) {
388
+ // Return the component
389
+ return components . add ( componentNode , 1 ) ;
390
+ }
391
+
392
+ // Try to find the component using variable declarations
366
393
var defInScope ;
367
394
var defs = variableInScope . defs ;
368
395
for ( i = 0 , j = defs . length ; i < j ; i ++ ) {
@@ -374,27 +401,27 @@ function componentRule(rule, context) {
374
401
if ( ! defInScope || ! defInScope . node ) {
375
402
return null ;
376
403
}
377
- node = defInScope . node . init || defInScope . node ;
404
+ componentNode = defInScope . node . init || defInScope . node ;
378
405
379
406
// Traverse the node properties to the component declaration
380
407
for ( i = 0 , j = componentPath . length ; i < j ; i ++ ) {
381
- if ( ! node . properties ) {
408
+ if ( ! componentNode . properties ) {
382
409
continue ;
383
410
}
384
- for ( k = 0 , l = node . properties . length ; k < l ; k ++ ) {
385
- if ( node . properties [ k ] . key . name === componentPath [ i ] ) {
386
- node = node . properties [ k ] ;
411
+ for ( k = 0 , l = componentNode . properties . length ; k < l ; k ++ ) {
412
+ if ( componentNode . properties [ k ] . key . name === componentPath [ i ] ) {
413
+ componentNode = componentNode . properties [ k ] ;
387
414
break ;
388
415
}
389
416
}
390
- if ( ! node || ! node . value ) {
417
+ if ( ! componentNode || ! componentNode . value ) {
391
418
return null ;
392
419
}
393
- node = node . value ;
420
+ componentNode = componentNode . value ;
394
421
}
395
422
396
423
// Return the component
397
- return components . add ( node , 1 ) ;
424
+ return components . add ( componentNode , 1 ) ;
398
425
}
399
426
} ;
400
427
0 commit comments