5
5
*/
6
6
'use strict' ;
7
7
8
- var has = require ( 'has' ) ;
9
- var find = require ( 'array.prototype.find' ) ;
10
- var Components = require ( '../util/Components' ) ;
11
- var variableUtil = require ( '../util/variable' ) ;
12
- var annotations = require ( '../util/annotations' ) ;
8
+ const has = require ( 'has' ) ;
9
+ const Components = require ( '../util/Components' ) ;
10
+ const variableUtil = require ( '../util/variable' ) ;
11
+ const annotations = require ( '../util/annotations' ) ;
13
12
14
13
// ------------------------------------------------------------------------------
15
14
// Rule Definition
@@ -36,8 +35,8 @@ module.exports = {
36
35
37
36
create : Components . detect ( function ( context , components , utils ) {
38
37
39
- var configuration = context . options [ 0 ] || { } ;
40
- var allowRequiredDefaults = configuration . allowRequiredDefaults || false ;
38
+ const configuration = context . options [ 0 ] || { } ;
39
+ const allowRequiredDefaults = configuration . allowRequiredDefaults || false ;
41
40
42
41
/**
43
42
* Get properties name
@@ -52,7 +51,7 @@ module.exports = {
52
51
// Special case for class properties
53
52
// (babel-eslint@5 does not expose property name so we have to rely on tokens)
54
53
} else if ( node . type === 'ClassProperty' ) {
55
- var tokens = context . getFirstTokens ( node , 2 ) ;
54
+ const tokens = context . getFirstTokens ( node , 2 ) ;
56
55
return tokens [ 1 ] && tokens [ 1 ] . type === 'Identifier' ? tokens [ 1 ] . value : tokens [ 0 ] . value ;
57
56
}
58
57
return '' ;
@@ -73,7 +72,7 @@ module.exports = {
73
72
* @returns {Boolean } `true` if the node is a defaultProps declaration, `false` if not
74
73
*/
75
74
function isDefaultPropsDeclaration ( node ) {
76
- var propName = getPropertyName ( node ) ;
75
+ const propName = getPropertyName ( node ) ;
77
76
return ( propName === 'defaultProps' || propName === 'getDefaultProps' ) ;
78
77
}
79
78
@@ -92,9 +91,7 @@ module.exports = {
92
91
* @returns {ASTNode|null } Return null if the variable could not be found, ASTNode otherwise.
93
92
*/
94
93
function findVariableByName ( name ) {
95
- var variable = find ( variableUtil . variablesInScope ( context ) , function ( item ) {
96
- return item . name === name ;
97
- } ) ;
94
+ const variable = variableUtil . variablesInScope ( context ) . find ( ( item ) => item . name === name ) ;
98
95
99
96
if ( ! variable || ! variable . defs [ 0 ] || ! variable . defs [ 0 ] . node ) {
100
97
return null ;
@@ -151,7 +148,7 @@ module.exports = {
151
148
* @returns {Object[] } Array of PropType object representations, to be consumed by `addPropTypesToComponent`.
152
149
*/
153
150
function getPropTypesFromObjectExpression ( objectExpression ) {
154
- var props = objectExpression . properties . filter ( function ( property ) {
151
+ const props = objectExpression . properties . filter ( function ( property ) {
155
152
return property . type !== 'ExperimentalSpreadProperty' ;
156
153
} ) ;
157
154
@@ -170,11 +167,11 @@ module.exports = {
170
167
* @returns {Object[] } Array of PropType object representations, to be consumed by `addPropTypesToComponent`.
171
168
*/
172
169
function getPropTypesFromTypeAnnotation ( node ) {
173
- var properties ;
170
+ let properties ;
174
171
175
172
switch ( node . typeAnnotation . type ) {
176
173
case 'GenericTypeAnnotation' :
177
- var annotation = resolveGenericTypeAnnotation ( node . typeAnnotation ) ;
174
+ let annotation = resolveGenericTypeAnnotation ( node . typeAnnotation ) ;
178
175
179
176
if ( annotation && annotation . id ) {
180
177
annotation = findVariableByName ( annotation . id . name ) ;
@@ -184,7 +181,7 @@ module.exports = {
184
181
break ;
185
182
186
183
case 'UnionTypeAnnotation' :
187
- var union = resolveUnionTypeAnnotation ( node . typeAnnotation ) ;
184
+ const union = resolveUnionTypeAnnotation ( node . typeAnnotation ) ;
188
185
properties = union . reduce ( function ( acc , curr ) {
189
186
if ( ! curr ) {
190
187
return acc ;
@@ -203,14 +200,14 @@ module.exports = {
203
200
break ;
204
201
}
205
202
206
- var props = properties . filter ( function ( property ) {
203
+ const props = properties . filter ( function ( property ) {
207
204
return property . type === 'ObjectTypeProperty' ;
208
205
} ) ;
209
206
210
207
return props . map ( function ( property ) {
211
208
// the `key` property is not present in ObjectTypeProperty nodes, so we need to get the key name manually.
212
- var tokens = context . getFirstTokens ( property , 1 ) ;
213
- var name = tokens [ 0 ] . value ;
209
+ const tokens = context . getFirstTokens ( property , 1 ) ;
210
+ const name = tokens [ 0 ] . value ;
214
211
215
212
return {
216
213
name : name ,
@@ -228,9 +225,7 @@ module.exports = {
228
225
* from this ObjectExpression can't be resolved.
229
226
*/
230
227
function getDefaultPropsFromObjectExpression ( objectExpression ) {
231
- var hasSpread = find ( objectExpression . properties , function ( property ) {
232
- return property . type === 'ExperimentalSpreadProperty' ;
233
- } ) ;
228
+ const hasSpread = objectExpression . properties . find ( property => property . type === 'ExperimentalSpreadProperty' ) ;
234
229
235
230
if ( hasSpread ) {
236
231
return 'unresolved' ;
@@ -264,7 +259,7 @@ module.exports = {
264
259
* @returns {void }
265
260
*/
266
261
function addPropTypesToComponent ( component , propTypes ) {
267
- var props = component . propTypes || [ ] ;
262
+ const props = component . propTypes || [ ] ;
268
263
269
264
components . set ( component . node , {
270
265
propTypes : props . concat ( propTypes )
@@ -289,7 +284,7 @@ module.exports = {
289
284
return ;
290
285
}
291
286
292
- var defaults = component . defaultProps || [ ] ;
287
+ const defaults = component . defaultProps || [ ] ;
293
288
294
289
components . set ( component . node , {
295
290
defaultProps : defaults . concat ( defaultProps )
@@ -307,7 +302,7 @@ module.exports = {
307
302
}
308
303
309
304
// find component this props annotation belongs to
310
- var component = components . get ( utils . getParentStatelessComponent ( ) ) ;
305
+ const component = components . get ( utils . getParentStatelessComponent ( ) ) ;
311
306
if ( ! component ) {
312
307
return ;
313
308
}
@@ -317,7 +312,7 @@ module.exports = {
317
312
318
313
function handlePropTypeAnnotationClassProperty ( node ) {
319
314
// find component this props annotation belongs to
320
- var component = components . get ( utils . getParentES6Component ( ) ) ;
315
+ const component = components . get ( utils . getParentES6Component ( ) ) ;
321
316
if ( ! component ) {
322
317
return ;
323
318
}
@@ -330,9 +325,7 @@ module.exports = {
330
325
}
331
326
332
327
function propFromName ( propTypes , name ) {
333
- return find ( propTypes , function ( prop ) {
334
- return prop . name === name ;
335
- } ) ;
328
+ return propTypes . find ( prop => prop . name === name ) ;
336
329
}
337
330
338
331
/**
@@ -350,7 +343,7 @@ module.exports = {
350
343
}
351
344
352
345
defaultProps . forEach ( function ( defaultProp ) {
353
- var prop = propFromName ( propTypes , defaultProp . name ) ;
346
+ const prop = propFromName ( propTypes , defaultProp . name ) ;
354
347
355
348
if ( prop && ( allowRequiredDefaults || ! prop . isRequired ) ) {
356
349
return ;
@@ -378,15 +371,15 @@ module.exports = {
378
371
379
372
return {
380
373
MemberExpression : function ( node ) {
381
- var isPropType = isPropTypesDeclaration ( node ) ;
382
- var isDefaultProp = isDefaultPropsDeclaration ( node ) ;
374
+ const isPropType = isPropTypesDeclaration ( node ) ;
375
+ const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
383
376
384
377
if ( ! isPropType && ! isDefaultProp ) {
385
378
return ;
386
379
}
387
380
388
381
// find component this propTypes/defaultProps belongs to
389
- var component = utils . getRelatedComponent ( node ) ;
382
+ const component = utils . getRelatedComponent ( node ) ;
390
383
if ( ! component ) {
391
384
return ;
392
385
}
@@ -402,7 +395,7 @@ module.exports = {
402
395
// MyComponent.propTypes = myPropTypes;
403
396
if ( node . parent . type === 'AssignmentExpression' ) {
404
397
405
- var expression = resolveNodeValue ( node . parent . right ) ;
398
+ const expression = resolveNodeValue ( node . parent . right ) ;
406
399
if ( ! expression || expression . type !== 'ObjectExpression' ) {
407
400
// If a value can't be found, we mark the defaultProps declaration as "unresolved", because
408
401
// we should ignore this component and not report any errors for it, to avoid false-positives
@@ -466,25 +459,25 @@ module.exports = {
466
459
return ;
467
460
}
468
461
469
- var isPropType = isPropTypesDeclaration ( node ) ;
470
- var isDefaultProp = isDefaultPropsDeclaration ( node ) ;
462
+ const isPropType = isPropTypesDeclaration ( node ) ;
463
+ const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
471
464
472
465
if ( ! isPropType && ! isDefaultProp ) {
473
466
return ;
474
467
}
475
468
476
469
// find component this propTypes/defaultProps belongs to
477
- var component = components . get ( utils . getParentES6Component ( ) ) ;
470
+ const component = components . get ( utils . getParentES6Component ( ) ) ;
478
471
if ( ! component ) {
479
472
return ;
480
473
}
481
474
482
- var returnStatement = utils . findReturnStatement ( node ) ;
475
+ const returnStatement = utils . findReturnStatement ( node ) ;
483
476
if ( ! returnStatement ) {
484
477
return ;
485
478
}
486
479
487
- var expression = resolveNodeValue ( returnStatement . argument ) ;
480
+ const expression = resolveNodeValue ( returnStatement . argument ) ;
488
481
if ( ! expression || expression . type !== 'ObjectExpression' ) {
489
482
return ;
490
483
}
@@ -522,21 +515,21 @@ module.exports = {
522
515
return ;
523
516
}
524
517
525
- var propName = getPropertyName ( node ) ;
526
- var isPropType = propName === 'propTypes' ;
527
- var isDefaultProp = propName === 'defaultProps' || propName === 'getDefaultProps' ;
518
+ const propName = getPropertyName ( node ) ;
519
+ const isPropType = propName === 'propTypes' ;
520
+ const isDefaultProp = propName === 'defaultProps' || propName === 'getDefaultProps' ;
528
521
529
522
if ( ! isPropType && ! isDefaultProp ) {
530
523
return ;
531
524
}
532
525
533
526
// find component this propTypes/defaultProps belongs to
534
- var component = components . get ( utils . getParentES6Component ( ) ) ;
527
+ const component = components . get ( utils . getParentES6Component ( ) ) ;
535
528
if ( ! component ) {
536
529
return ;
537
530
}
538
531
539
- var expression = resolveNodeValue ( node . value ) ;
532
+ const expression = resolveNodeValue ( node . value ) ;
540
533
if ( ! expression || expression . type !== 'ObjectExpression' ) {
541
534
return ;
542
535
}
@@ -564,7 +557,7 @@ module.exports = {
564
557
// });
565
558
ObjectExpression : function ( node ) {
566
559
// find component this propTypes/defaultProps belongs to
567
- var component = utils . isES5Component ( node ) && components . get ( node ) ;
560
+ const component = utils . isES5Component ( node ) && components . get ( node ) ;
568
561
if ( ! component ) {
569
562
return ;
570
563
}
@@ -575,8 +568,8 @@ module.exports = {
575
568
return ;
576
569
}
577
570
578
- var isPropType = isPropTypesDeclaration ( property ) ;
579
- var isDefaultProp = isDefaultPropsDeclaration ( property ) ;
571
+ const isPropType = isPropTypesDeclaration ( property ) ;
572
+ const isDefaultProp = isDefaultPropsDeclaration ( property ) ;
580
573
581
574
if ( ! isPropType && ! isDefaultProp ) {
582
575
return ;
@@ -588,7 +581,7 @@ module.exports = {
588
581
}
589
582
590
583
if ( isDefaultProp && property . value . type === 'FunctionExpression' ) {
591
- var returnStatement = utils . findReturnStatement ( property ) ;
584
+ const returnStatement = utils . findReturnStatement ( property ) ;
592
585
if ( ! returnStatement || returnStatement . argument . type !== 'ObjectExpression' ) {
593
586
return ;
594
587
}
@@ -604,9 +597,9 @@ module.exports = {
604
597
FunctionExpression : handleStatelessComponent ,
605
598
606
599
'Program:exit' : function ( ) {
607
- var list = components . list ( ) ;
600
+ const list = components . list ( ) ;
608
601
609
- for ( var component in list ) {
602
+ for ( let component in list ) {
610
603
if ( ! has ( list , component ) ) {
611
604
continue ;
612
605
}
0 commit comments