1
1
const _ = require ( 'lodash' ) ;
2
- const utils = require ( '../utils' ) ;
2
+ const {
3
+ organizeDeprecations,
4
+ getLocalizedFix,
5
+ addToImports,
6
+ getComponentLocalName,
7
+ getComponentName
8
+ } = require ( '../utils' ) ;
3
9
4
10
const MAP_SCHEMA = {
5
11
type : 'object' ,
6
- additionalProperties : true ,
12
+ additionalProperties : true
7
13
} ;
8
14
9
15
module . exports = {
10
16
meta : {
11
17
docs : {
12
18
description : 'asset is deprecated' ,
13
19
category : 'Best Practices' ,
14
- recommended : true ,
20
+ recommended : true
15
21
} ,
16
22
messages : {
17
- uiLib : 'This asset is deprecated.' ,
23
+ uiLib : 'This asset is deprecated.'
18
24
} ,
19
25
fixable : 'code' ,
20
- schema : [ MAP_SCHEMA ] ,
26
+ schema : [ MAP_SCHEMA ]
21
27
} ,
22
28
create ( context ) {
23
29
function reportDeprecatedAssets ( node , options ) {
@@ -32,91 +38,43 @@ module.exports = {
32
38
if ( options . fix ) {
33
39
return fixer . replaceText ( node , options . fix ) ;
34
40
}
35
- } ,
41
+ }
36
42
} ) ;
37
43
} catch ( err ) {
38
44
console . log ( 'Found error in: ' , context . getFilename ( ) ) ;
39
45
}
40
46
}
41
47
42
- const defaultImportName = 'Assets' ;
43
48
const { deprecations, source} = context . options [ 0 ] ;
44
- let localImportSpecifier ;
49
+ const organizedDeprecations = organizeDeprecations ( deprecations , source ) ;
50
+ const imports = [ ] ;
45
51
46
- function setLocalImportSpecifierFromImport ( node ) {
47
- localImportSpecifier = utils . getLocalImportSpecifier ( node , source , defaultImportName ) ;
48
- }
52
+ function deprecationCheck ( node ) {
53
+ imports . forEach ( currentImport => {
54
+ const source = Object . keys ( currentImport ) [ 0 ] ;
55
+ const componentLocalName = getComponentLocalName ( node ) ;
56
+ if ( componentLocalName ) {
57
+ const deprecationSource = organizedDeprecations [ source ] ;
58
+ if ( deprecationSource ) {
59
+ // There are deprecations from this source
60
+ const componentName = getComponentName ( componentLocalName , imports ) ;
61
+ const foundDeprecations = deprecationSource . filter (
62
+ currentDeprecationSource => currentDeprecationSource . path === componentName
63
+ ) ;
49
64
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 ) ;
65
+ if ( foundDeprecations . length > 0 ) {
66
+ const localizedFix = getLocalizedFix ( foundDeprecations [ 0 ] . fix , currentImport ) ;
67
+ reportDeprecatedAssets ( node , { ...foundDeprecations [ 0 ] , fix : localizedFix } ) ;
82
68
}
83
69
}
84
- } else if ( node . name === localImportSpecifier ) {
85
- pathString = `${ node . name } .${ pathString } ` ;
86
- return pathString ;
87
70
}
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
- }
71
+ } ) ;
106
72
}
107
73
108
74
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),
75
+ ImportDeclaration : node => addToImports ( node , imports ) ,
76
+ VariableDeclarator : node => addToImports ( node , imports ) ,
77
+ MemberExpression : node => deprecationCheck ( node )
120
78
} ;
121
- } ,
79
+ }
122
80
} ;
0 commit comments