1
1
/**
2
- * Prevent missing props validation in a Enact kind component definition
2
+ * Prevent missing props validation in an Enact kind component definition
3
3
*
4
4
* Based on the React prop-type rule created by Yannick Croissant
5
5
* https://github.com/yannickcr/eslint-plugin-react
@@ -69,8 +69,8 @@ module.exports = {
69
69
70
70
/**
71
71
* Helper for accessing the current scope in the stack.
72
- * @param {string } key The name of the identifier to access. If omitted, returns the full scope.
73
- * @param {ASTNode } value If provided sets the new value for the identifier.
72
+ * @param {string } [ key] The name of the identifier to access. If omitted, returns the full scope.
73
+ * @param {ASTNode } [ value] If provided sets the new value for the identifier.
74
74
* @returns {Object|ASTNode } Either the whole scope or the ASTNode associated with the given identifier.
75
75
*/
76
76
function typeScope ( key , value ) {
@@ -90,7 +90,7 @@ module.exports = {
90
90
*/
91
91
function isPropTypesUsage ( node ) {
92
92
var isClassUsage = (
93
- ( utils . getParentES6Component ( ) || utils . getParentES5Component ( ) ) &&
93
+ ( utils . getParentES6Component ( node ) || utils . getParentES5Component ( node ) ) &&
94
94
node . object . type === 'ThisExpression' && node . property . name === 'props'
95
95
) ;
96
96
var isStatelessFunctionUsage = node . object . name === 'props' ;
@@ -104,7 +104,7 @@ module.exports = {
104
104
*/
105
105
function isAnnotatedClassPropsDeclaration ( node ) {
106
106
if ( node && ( node . type === 'ClassProperty' || node . type === 'PropertyDefinition' ) ) {
107
- var tokens = context . getFirstTokens ( node , 2 ) ;
107
+ let tokens = sourceCode . getFirstTokens ? sourceCode . getFirstTokens ( node , 2 ) : context . getFirstTokens ( node , 2 ) ;
108
108
if (
109
109
node . typeAnnotation && (
110
110
tokens [ 0 ] . value === 'props' ||
@@ -124,7 +124,7 @@ module.exports = {
124
124
*/
125
125
function isAnnotatedFunctionPropsDeclaration ( node ) {
126
126
if ( node && node . params && node . params . length ) {
127
- var tokens = context . getFirstTokens ( node . params [ 0 ] , 2 ) ;
127
+ let tokens = sourceCode . getFirstTokens ? sourceCode . getFirstTokens ( node . params [ 0 ] , 2 ) : context . getFirstTokens ( node . params [ 0 ] , 2 ) ;
128
128
var isAnnotated = node . params [ 0 ] . typeAnnotation ;
129
129
var isDestructuredProps = node . params [ 0 ] . type === 'ObjectPattern' ;
130
130
var isProps = tokens [ 0 ] . value === 'props' || ( tokens [ 1 ] && tokens [ 1 ] . value === 'props' ) ;
@@ -170,7 +170,7 @@ module.exports = {
170
170
// Special case for class properties
171
171
// (babel-eslint does not expose property name so we have to rely on tokens)
172
172
if ( node && ( node . type === 'ClassProperty' || node . type === 'PropertyDefinition' ) ) {
173
- var tokens = context . getFirstTokens ( node , 2 ) ;
173
+ let tokens = sourceCode . getFirstTokens ? sourceCode . getFirstTokens ( node , 2 ) : context . getFirstTokens ( node , 2 ) ;
174
174
if (
175
175
tokens [ 0 ] . value === 'propTypes' ||
176
176
( tokens [ 1 ] && tokens [ 1 ] . value === 'propTypes' )
@@ -257,7 +257,7 @@ module.exports = {
257
257
// this is the last key, accept everything
258
258
return true ;
259
259
}
260
- // non trivial, check all of them
260
+ // non- trivial, check all of them
261
261
var unionTypes = propType . children ;
262
262
var unionPropType = { } ;
263
263
for ( var k = 0 , z = unionTypes . length ; k < z ; k ++ ) {
@@ -312,12 +312,12 @@ module.exports = {
312
312
313
313
/**
314
314
* Retrieve the name of a key node
315
- * @param {ASTNode } node The AST node with the key.
315
+ * @param {Object| ASTNode } node The AST node with the key.
316
316
* @return {string } the name of the key
317
317
*/
318
318
function getKeyValue ( node ) {
319
319
if ( node . type === 'ObjectTypeProperty' ) {
320
- var tokens = context . getFirstTokens ( node , 1 ) ;
320
+ let tokens = sourceCode . getFirstTokens ? sourceCode . getFirstTokens ( node , 1 ) : context . getFirstTokens ( node , 1 ) ;
321
321
return tokens [ 0 ] . value ;
322
322
}
323
323
var key = node . key || node . argument || node . property ;
@@ -451,7 +451,7 @@ module.exports = {
451
451
/**
452
452
* Creates the representation of the React props type annotation for the component.
453
453
* The representation is used to verify nested used properties.
454
- * @param {ASTNode } annotation Type annotation for the props class property.
454
+ * @param {Object| ASTNode } annotation Type annotation for the props class property.
455
455
* @return {Object|Boolean } The representation of the declaration, true means
456
456
* the property is declared without the need for further analysis.
457
457
*/
@@ -511,8 +511,8 @@ module.exports = {
511
511
* Check if we are in a class constructor
512
512
* @return {boolean } true if we are in a class constructor, false if not
513
513
*/
514
- function inConstructor ( ) {
515
- var scope = context . getScope ( ) ;
514
+ function inConstructor ( node ) {
515
+ let scope = sourceCode . getScope ? sourceCode . getScope ( node ) : context . getScope ( ) ;
516
516
while ( scope ) {
517
517
if ( scope . block && scope . block . parent && scope . block . parent . kind === 'constructor' ) {
518
518
return true ;
@@ -529,7 +529,7 @@ module.exports = {
529
529
*/
530
530
function getPropertyName ( node ) {
531
531
var isDirectProp = DIRECT_PROPS_REGEX . test ( sourceCode . getText ( node ) ) ;
532
- var isInClassComponent = utils . getParentES6Component ( ) || utils . getParentES5Component ( ) ;
532
+ var isInClassComponent = utils . getParentES6Component ( node ) || utils . getParentES5Component ( node ) ;
533
533
var isNotInConstructor = ! inConstructor ( node ) ;
534
534
535
535
if ( isInClassComponent && utils . isKindComponent ( isInClassComponent ) ) {
@@ -576,7 +576,7 @@ module.exports = {
576
576
* @param {ASTNode } node The AST node being marked.
577
577
*/
578
578
function markComputedPropTypes ( node ) {
579
- var component = components . get ( utils . getParentComponent ( ) ) ;
579
+ var component = components . get ( utils . getParentComponent ( node ) ) ;
580
580
var computed = component && component . computedProps || [ ] ;
581
581
for ( var i = 0 ; i < node . properties . length ; i ++ ) {
582
582
if ( node . properties [ i ] && node . properties [ i ] . key && node . properties [ i ] . key . name ) {
@@ -593,7 +593,7 @@ module.exports = {
593
593
* @param {ASTNode } node The AST node being marked.
594
594
*/
595
595
function markHandlersPropTypes ( node ) {
596
- var component = components . get ( utils . getParentComponent ( ) ) ;
596
+ var component = components . get ( utils . getParentComponent ( node ) ) ;
597
597
var handlers = component && component . handlersProps || [ ] ;
598
598
for ( var i = 0 ; i < node . properties . length ; i ++ ) {
599
599
if ( node . properties [ i ] && node . properties [ i ] . key && node . properties [ i ] . key . name ) {
@@ -608,6 +608,7 @@ module.exports = {
608
608
/**
609
609
* Mark a prop type as used
610
610
* @param {ASTNode } node The AST node being marked.
611
+ * @param {Array } [parentNames] The list of parent names.
611
612
*/
612
613
function markPropTypesAsUsed ( node , parentNames ) {
613
614
parentNames = parentNames || [ ] ;
@@ -692,7 +693,7 @@ module.exports = {
692
693
// let {firstname} = props
693
694
var directDestructuring =
694
695
node . init . name === 'props' &&
695
- ( utils . getParentStatelessComponent ( ) || inConstructor ( ) )
696
+ ( utils . getParentStatelessComponent ( node ) || inConstructor ( node ) )
696
697
;
697
698
698
699
if ( thisDestructuring ) {
@@ -710,7 +711,7 @@ module.exports = {
710
711
throw new Error ( node . type + ' ASTNodes are not handled by markPropTypesAsUsed' ) ;
711
712
}
712
713
713
- var component = components . get ( utils . getParentComponent ( ) ) ;
714
+ var component = components . get ( utils . getParentComponent ( node ) ) ;
714
715
var usedPropTypes = component && component . usedPropTypes || [ ] ;
715
716
716
717
switch ( type ) {
@@ -764,7 +765,7 @@ module.exports = {
764
765
/**
765
766
* Mark a prop type as declared
766
767
* @param {ASTNode } node The AST node being checked.
767
- * @param {propTypes } node The AST node containing the proptypes
768
+ * @param {Object|ASTNode } propTypes The AST node containing the proptypes.
768
769
*/
769
770
function markPropTypesAsDeclared ( node , propTypes ) {
770
771
var componentNode = node ;
@@ -819,7 +820,7 @@ module.exports = {
819
820
}
820
821
break ;
821
822
case 'Identifier' :
822
- var variablesInScope = variable . variablesInScope ( context ) ;
823
+ var variablesInScope = variable . variablesInScope ( sourceCode , context , componentNode )
823
824
for ( var i = 0 , j = variablesInScope . length ; i < j ; i ++ ) {
824
825
if ( variablesInScope [ i ] . name !== propTypes . name ) {
825
826
continue ;
@@ -845,7 +846,7 @@ module.exports = {
845
846
846
847
/**
847
848
* Reports undeclared proptypes for a given component
848
- * handlers can be used in computed, but not vice- versa.
849
+ * handlers can be used in computed, but not vice versa.
849
850
* handlers and computed props can't reference others declared
850
851
* only in the same place.
851
852
* @param {Object } component The component to process
@@ -889,7 +890,7 @@ module.exports = {
889
890
* This method also resolves type aliases where possible.
890
891
*
891
892
* @param {ASTNode } node The annotation or a node containing the type annotation.
892
- * @returns {ASTNode } The resolved type annotation for the node.
893
+ * @returns {Object| ASTNode } The resolved type annotation for the node.
893
894
*/
894
895
function resolveTypeAnnotation ( node ) {
895
896
var annotation = node . typeAnnotation || node ;
@@ -999,7 +1000,7 @@ module.exports = {
999
1000
var directDestructuring =
1000
1001
destructuring &&
1001
1002
node . init . name === 'props' &&
1002
- ( utils . getParentStatelessComponent ( ) || inConstructor ( ) )
1003
+ ( utils . getParentStatelessComponent ( node ) || inConstructor ( node ) )
1003
1004
;
1004
1005
1005
1006
if ( ! thisDestructuring && ! directDestructuring ) {
0 commit comments