1
1
const _ = require ( 'lodash' ) ;
2
2
const utils = require ( '../utils' ) ;
3
+ const {
4
+ organizeDeprecations,
5
+ OrganizationType,
6
+ getLocalizedFix,
7
+ addToImports,
8
+ getComponentLocalName,
9
+ getComponentName
10
+ } = utils ;
3
11
4
12
const MAP_SCHEMA = {
5
13
type : 'object' ,
6
- additionalProperties : true ,
14
+ additionalProperties : true
7
15
} ;
8
16
9
17
module . exports = {
10
18
meta : {
11
19
docs : {
12
20
description : 'asset is deprecated' ,
13
21
category : 'Best Practices' ,
14
- recommended : true ,
22
+ recommended : true
15
23
} ,
16
24
messages : {
17
- uiLib : 'This asset is deprecated.' ,
25
+ uiLib : 'This asset is deprecated.'
18
26
} ,
19
27
fixable : 'code' ,
20
- schema : [ MAP_SCHEMA ] ,
28
+ schema : [ MAP_SCHEMA ]
21
29
} ,
22
30
create ( context ) {
23
31
function reportDeprecatedAssets ( node , options ) {
@@ -32,91 +40,43 @@ module.exports = {
32
40
if ( options . fix ) {
33
41
return fixer . replaceText ( node , options . fix ) ;
34
42
}
35
- } ,
43
+ }
36
44
} ) ;
37
45
} catch ( err ) {
38
46
console . log ( 'Found error in: ' , context . getFilename ( ) ) ;
39
47
}
40
48
}
41
49
42
- const defaultImportName = 'Assets' ;
43
50
const { deprecations, source} = context . options [ 0 ] ;
44
- let localImportSpecifier ;
51
+ const organizedDeprecations = organizeDeprecations ( deprecations , OrganizationType . SOURCE , source ) ;
52
+ const imports = [ ] ;
45
53
46
- function setLocalImportSpecifierFromImport ( node ) {
47
- localImportSpecifier = utils . getLocalImportSpecifier ( node , source , defaultImportName ) ;
48
- }
54
+ function deprecationCheck ( node ) {
55
+ imports . forEach ( currentImport => {
56
+ const source = Object . keys ( currentImport ) [ 0 ] ;
57
+ const componentLocalName = getComponentLocalName ( node ) ;
58
+ if ( componentLocalName ) {
59
+ const deprecationSource = organizedDeprecations [ source ] ;
60
+ if ( deprecationSource ) {
61
+ // There are deprecations from this source
62
+ const componentName = getComponentName ( componentLocalName , imports ) ;
63
+ const foundDeprecations = deprecationSource . filter (
64
+ currentDeprecationSource => currentDeprecationSource . path === componentName
65
+ ) ;
49
66
50
- function setLocalImportSpecifierFromRequire ( node ) {
51
- if ( node . init && node . init . callee && node . init . callee . name === 'require' ) {
52
- if ( node . id && node . id . properties ) {
53
- _ . map ( node . id . properties , property => {
54
- if ( property . key && property . key . name === defaultImportName ) {
55
- if ( property . value && property . value . name ) {
56
- localImportSpecifier = property . value . name ;
57
- } else {
58
- localImportSpecifier = property . key . name ;
59
- }
60
- }
61
- } ) ;
62
- }
63
- }
64
- }
65
-
66
- function getAssetString ( node , pathString = '' ) {
67
- if ( node ) {
68
- if ( node . object ) {
69
- if ( node . property ) {
70
- let name ;
71
- if ( node . property . type === 'Identifier' && node . property . name ) {
72
- name = node . property . name ;
73
- } else if ( node . property . type === 'Literal' && node . property . value ) {
74
- name = node . property . value ;
75
- } else if ( node . property . type === 'CallExpression' && node . property . callee && node . property . callee . name ) {
76
- // TODO: ¯\_(ツ)_/¯
77
- }
78
-
79
- if ( name ) {
80
- pathString = ( pathString === '' ) ? `${ name } ` : `${ name } .${ pathString } ` ;
81
- return getAssetString ( node . object , pathString ) ;
67
+ if ( foundDeprecations . length > 0 ) {
68
+ const localizedFix = getLocalizedFix ( foundDeprecations [ 0 ] . fix , currentImport ) ;
69
+ reportDeprecatedAssets ( node , { ...foundDeprecations [ 0 ] , fix : localizedFix } ) ;
82
70
}
83
71
}
84
- } else if ( node . name === localImportSpecifier ) {
85
- pathString = `${ node . name } .${ pathString } ` ;
86
- return pathString ;
87
72
}
88
- }
89
-
90
- return undefined ;
91
- }
92
-
93
- function findAndReportDeprecation ( node , possibleDeprecation ) {
94
- possibleDeprecation = possibleDeprecation . replace ( localImportSpecifier , defaultImportName ) ;
95
- const deprecatedObject = _ . find ( deprecations , { path : possibleDeprecation } ) ;
96
- if ( deprecatedObject ) {
97
- reportDeprecatedAssets ( node , deprecatedObject ) ;
98
- }
99
- }
100
-
101
- function testMemberDeprecation ( node ) {
102
- const assetString = getAssetString ( node ) ;
103
- if ( assetString ) {
104
- findAndReportDeprecation ( node , assetString ) ;
105
- }
73
+ } ) ;
106
74
}
107
75
108
76
return {
109
- ImportDeclaration : node => ! localImportSpecifier && setLocalImportSpecifierFromImport ( node ) ,
110
- VariableDeclarator : node => ! localImportSpecifier && setLocalImportSpecifierFromRequire ( node ) ,
111
- MemberExpression : node => localImportSpecifier && testMemberDeprecation ( node ) ,
112
-
113
- // ExpressionStatement: node => testExpressionStatement(node),
114
- // AssignmentExpression: node => testAssignmentExpression(node),
115
- // JSXAttribute: node => testJSXAttribute(node),
116
- // JSXOpeningElement: node => testJSXOpeningElement(node),
117
- // JSXSpreadAttribute: node => testJSXSpreadAttribute(node),
118
- // ObjectExpression: node => testObjectExpression(node),
119
- // Property: node => testProperty(node),
77
+ ImportDeclaration : node => addToImports ( node , imports ) ,
78
+ VariableDeclarator : node => addToImports ( node , imports ) ,
79
+ MemberExpression : node => deprecationCheck ( node )
120
80
} ;
121
- } ,
81
+ }
122
82
} ;
0 commit comments