@@ -21,16 +21,22 @@ function Components() {
21
21
* Add a node to the components list, or update it if it's already in the list
22
22
*
23
23
* @param {ASTNode } node The AST node being added.
24
- * @param {Object } props Additional properties to add to the component.
24
+ * @param {Number } confidence Confidence in the component detection (0=banned, 1=maybe, 2=yes)
25
25
*/
26
- Components . prototype . add = function ( node , props ) {
26
+ Components . prototype . add = function ( node , confidence ) {
27
27
var id = this . _getId ( node ) ;
28
28
if ( this . _list [ id ] ) {
29
- this . _list [ id ] = util . _extend ( this . _list [ id ] , props ) ;
29
+ if ( confidence === 0 || this . _list [ id ] . confidence === 0 ) {
30
+ this . _list [ id ] . confidence = 0 ;
31
+ } else {
32
+ this . _list [ id ] . confidence = Math . max ( this . _list [ id ] . confidence , confidence ) ;
33
+ }
30
34
return ;
31
35
}
32
- props . node = node ;
33
- this . _list [ id ] = props ;
36
+ this . _list [ id ] = {
37
+ node : node ,
38
+ confidence : confidence
39
+ } ;
34
40
} ;
35
41
36
42
/**
@@ -70,7 +76,7 @@ Components.prototype.set = function(node, props) {
70
76
Components . prototype . list = function ( ) {
71
77
var list = { } ;
72
78
for ( var i in this . _list ) {
73
- if ( ! this . _list . hasOwnProperty ( i ) || ! this . _list [ i ] . confident ) {
79
+ if ( ! this . _list . hasOwnProperty ( i ) || this . _list [ i ] . confidence < 2 ) {
74
80
continue ;
75
81
}
76
82
list [ i ] = this . _list [ i ] ;
@@ -87,7 +93,7 @@ Components.prototype.list = function() {
87
93
Components . prototype . length = function ( ) {
88
94
var length = 0 ;
89
95
for ( var i in this . _list ) {
90
- if ( ! this . _list . hasOwnProperty ( i ) || ! this . _list [ i ] . confident ) {
96
+ if ( ! this . _list . hasOwnProperty ( i ) || this . _list [ i ] . confidence < 2 ) {
91
97
continue ;
92
98
}
93
99
length ++ ;
@@ -309,40 +315,38 @@ function componentRule(rule, context) {
309
315
if ( ! context . react . isES6Component ( node ) ) {
310
316
return ;
311
317
}
312
- components . add ( node , { confident : true } ) ;
318
+ components . add ( node , 2 ) ;
313
319
} ,
314
320
315
321
ClassProperty : function ( node ) {
316
322
node = context . react . getParentComponent ( ) ;
317
323
if ( ! node ) {
318
324
return ;
319
325
}
320
- components . add ( node , { confident : true } ) ;
326
+ components . add ( node , 2 ) ;
321
327
} ,
322
328
323
329
ObjectExpression : function ( node ) {
324
330
if ( ! context . react . isES5Component ( node ) ) {
325
331
return ;
326
332
}
327
- components . add ( node , { confident : true } ) ;
333
+ components . add ( node , 2 ) ;
328
334
} ,
329
335
330
336
FunctionExpression : function ( node ) {
331
337
node = context . react . getParentComponent ( ) ;
332
338
if ( ! node ) {
333
339
return ;
334
340
}
335
- var component = components . get ( node ) ;
336
- components . add ( node , { confident : component && component . confident || false } ) ;
341
+ components . add ( node , 1 ) ;
337
342
} ,
338
343
339
344
FunctionDeclaration : function ( node ) {
340
345
node = context . react . getParentComponent ( ) ;
341
346
if ( ! node ) {
342
347
return ;
343
348
}
344
- var component = components . get ( node ) ;
345
- components . add ( node , { confident : component && component . confident || false } ) ;
349
+ components . add ( node , 1 ) ;
346
350
} ,
347
351
348
352
ArrowFunctionExpression : function ( node ) {
@@ -351,11 +355,19 @@ function componentRule(rule, context) {
351
355
return ;
352
356
}
353
357
if ( node . expression && context . react . isReturningJSX ( node ) ) {
354
- components . add ( node , { confident : true } ) ;
358
+ components . add ( node , 2 ) ;
355
359
} else {
356
- var component = components . get ( node ) ;
357
- components . add ( node , { confident : component && component . confident || false } ) ;
360
+ components . add ( node , 1 ) ;
361
+ }
362
+ } ,
363
+
364
+ ThisExpression : function ( node ) {
365
+ node = context . react . getParentComponent ( ) ;
366
+ if ( ! node || ! / F u n c t i o n / . test ( node . type ) ) {
367
+ return ;
358
368
}
369
+ // Ban functions with a ThisExpression
370
+ components . add ( node , 0 ) ;
359
371
} ,
360
372
361
373
ReturnStatement : function ( node ) {
@@ -366,7 +378,7 @@ function componentRule(rule, context) {
366
378
if ( ! node ) {
367
379
return ;
368
380
}
369
- components . add ( node , { confident : true } ) ;
381
+ components . add ( node , 2 ) ;
370
382
}
371
383
} ;
372
384
0 commit comments