1
1
const _ = require ( 'lodash' ) ;
2
- const utils = require ( '../utils_old' ) ;
2
+ const { findValueNodeOfIdentifier , getComponentName , getPathPrefix , getPathSuffix } = require ( '../utils_old' ) ;
3
3
4
4
const MAP_SCHEMA = {
5
5
type : 'object' ,
@@ -52,7 +52,7 @@ module.exports = {
52
52
} ,
53
53
create ( context ) {
54
54
function reportPropValueShapeDeprecation ( propKey , prop , deprecation , node ) {
55
- const componentName = utils . getComponentName ( node ) ;
55
+ const componentName = getComponentName ( node ) ;
56
56
const newProp = _ . get ( deprecation , 'fix.propName' ) ;
57
57
const fixMessage = _ . get ( deprecation , 'message' ) ? ' ' + _ . get ( deprecation , 'message' ) : '' ;
58
58
const message = `The shape of '${ prop } ' prop of '${ componentName } ' doesn't contain '${ deprecation . prop } ' anymore.${ fixMessage } ` ;
@@ -70,24 +70,13 @@ module.exports = {
70
70
function testJSXAttributes ( node ) {
71
71
try {
72
72
const { deprecations} = _ . get ( context , 'options[0]' ) ;
73
- const componentName = utils . getComponentName ( node ) ;
73
+ const componentName = getComponentName ( node ) ;
74
74
_ . forEach ( deprecations , deprecation => {
75
75
if ( _ . includes ( deprecation . components , componentName ) ) {
76
76
_ . forEach ( node . attributes , attribute => {
77
- const attributeName = _ . get ( attribute , 'name.name' ) ;
78
- if ( attribute . type === 'JSXSpreadAttribute' ) {
79
- const spreadSource = utils . findValueNodeOfIdentifier ( attribute . argument . name , context . getScope ( ) ) ;
80
- const spreadSourceName = _ . get ( spreadSource , 'properties[0].key.name' ) ;
81
- checkAttributeProperties (
82
- spreadSource . properties [ 0 ] . value . properties ,
83
- spreadSourceName ,
84
- deprecation ,
85
- node ,
86
- context
87
- ) ;
88
- } else if ( _ . includes ( deprecation . propNames , attributeName ) ) {
89
- checkAttribute ( attribute , deprecation , node ) ;
90
- }
77
+ _ . forEach ( deprecation . propNames , deprecationProp => {
78
+ recursiveDeprecation ( attribute , deprecationProp , deprecation , deprecationProp , node ) ;
79
+ } ) ;
91
80
} ) ;
92
81
}
93
82
} ) ;
@@ -96,34 +85,58 @@ module.exports = {
96
85
}
97
86
}
98
87
99
- function checkAttribute ( attribute , deprecation , node ) {
100
- const attributeName = _ . get ( attribute , 'name.name' ) ;
101
- const attributeType = _ . get ( attribute , 'value.expression.type' ) ;
102
- if ( attributeType === 'Identifier' ) {
103
- const passedProp = utils . findValueNodeOfIdentifier ( attribute . value . expression . name , context . getScope ( ) ) ;
104
- if ( passedProp && passedProp . properties ) {
105
- checkAttributeProperties ( passedProp . properties , attributeName , deprecation , node , context ) ;
88
+ function recursiveDeprecation ( attribute , deprecationProp , deprecation , deprecationPath , node ) {
89
+ const deprecationPrefix = getPathPrefix ( deprecationProp ) ;
90
+ const deprecationSuffix = getPathSuffix ( deprecationProp ) ;
91
+ let passedProps ;
92
+ let attributeName = _ . get ( attribute , 'name.name' ) || _ . get ( attribute , 'key.name' ) ;
93
+ if ( attribute . type === 'JSXSpreadAttribute' || attribute . type === 'ExperimentalSpreadProperty' ) {
94
+ const spreadSource = findValueNodeOfIdentifier ( attribute . argument . name , context . getScope ( ) ) ;
95
+ const spreadSourceName = _ . get ( spreadSource , 'properties[0].key.name' ) ;
96
+ if ( deprecationPrefix === spreadSourceName ) {
97
+ checkAttributeProperties ( spreadSource . properties [ 0 ] . value . properties , deprecationPath , deprecation , node ) ;
106
98
}
107
- }
108
- const attributeProps = _ . get ( attribute , 'value.expression.properties' ) ;
109
- for ( let index = 0 ; index < attributeProps . length ; index ++ ) {
110
- const spreadElementType = _ . get ( attribute , `value.expression.properties[${ index } ].type` ) ;
111
- if ( attributeType === 'ObjectExpression' && spreadElementType === 'ExperimentalSpreadProperty' ) {
112
- const spreadSource = utils . findValueNodeOfIdentifier (
113
- attribute . value . expression . properties [ index ] . argument . name ,
114
- context . getScope ( )
115
- ) ;
116
- if ( spreadSource && spreadSource . properties ) {
117
- checkAttributeProperties ( spreadSource . properties , attributeName , deprecation , node ) ;
118
- }
99
+ } else if ( ! deprecationSuffix && deprecationPrefix === attributeName ) {
100
+ const attributeType = _ . get ( attribute , 'value.expression.type' ) || _ . get ( attribute , 'type' ) ;
101
+ if ( attributeType === 'Identifier' ) {
102
+ const passedPropsName = _ . get ( attribute , 'value.expression.name' ) ;
103
+ passedProps = findValueNodeOfIdentifier ( passedPropsName , context . getScope ( ) ) ;
104
+ }
105
+ let attributeProperties = passedProps
106
+ ? _ . get ( passedProps , 'properties' )
107
+ : attributeType === 'Property'
108
+ ? _ . get ( attribute , 'value.properties' )
109
+ : _ . get ( attribute , 'value.expression.properties' ) ;
110
+ if ( attributeType === 'Property' && ! attributeProperties ) {
111
+ const passedPropsName = _ . get ( attribute , 'value.name' ) ;
112
+ passedProps = findValueNodeOfIdentifier ( passedPropsName , context . getScope ( ) ) ;
113
+ attributeProperties = passedProps . properties ;
119
114
}
115
+ checkAttributeProperties ( attributeProperties , deprecationPath , deprecation , node ) ;
116
+ } else if ( deprecationSuffix ) {
117
+ const attributeType = _ . get ( attribute , 'value.expression.type' ) ;
118
+ if ( attributeType === 'Identifier' ) {
119
+ const passedPropsName = _ . get ( attribute , 'value.expression.name' ) ;
120
+ passedProps = findValueNodeOfIdentifier ( passedPropsName , context . getScope ( ) ) ;
121
+ }
122
+ const attributeProperties = passedProps
123
+ ? _ . get ( passedProps , 'properties' )
124
+ : _ . get ( attribute , 'value.expression.properties' ) ||
125
+ _ . get ( attribute , 'value.properties' ) ||
126
+ _ . get ( attribute , 'value.expression.elements[0].properties' ) ;
127
+ _ . forEach ( attributeProperties , attributeProperty => {
128
+ recursiveDeprecation ( attributeProperty , deprecationSuffix , deprecation , deprecationPath , node ) ;
129
+ } ) ;
120
130
}
121
- const attributeProperties = _ . get ( attribute , 'value.expression.properties' ) ;
122
- checkAttributeProperties ( attributeProperties , attributeName , deprecation , node ) ;
123
131
}
124
132
125
133
function checkAttributeProperties ( attributeProperties , attributeName , deprecation , node ) {
126
- for ( let i = 0 ; i <= attributeProperties . length ; i ++ ) {
134
+ for ( let i = 0 ; i < attributeProperties . length ; i ++ ) {
135
+ const propertyType = _ . get ( attributeProperties [ i ] , 'type' ) ;
136
+ if ( propertyType === 'ExperimentalSpreadProperty' ) {
137
+ const spreadProps = findValueNodeOfIdentifier ( attributeProperties [ i ] . argument . name , context . getScope ( ) ) ;
138
+ checkAttributeProperties ( spreadProps . properties , attributeName , deprecation , node ) ;
139
+ }
127
140
const propertyName = _ . get ( attributeProperties [ i ] , 'key.name' ) ;
128
141
const origin = propertyName && _ . find ( deprecation . shape , [ 'prop' , propertyName ] ) ;
129
142
if ( origin && origin . prop && propertyName === origin . prop ) {
0 commit comments