@@ -3,7 +3,7 @@ import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-import
33
44function transCamel ( _str , symbol ) {
55 const str = _str [ 0 ] . toLowerCase ( ) + _str . substr ( 1 ) ;
6- return str . replace ( / ( [ A - Z ] ) / g, ( $1 ) => `${ symbol } ${ $1 . toLowerCase ( ) } ` ) ;
6+ return str . replace ( / ( [ A - Z ] ) / g, $1 => `${ symbol } ${ $1 . toLowerCase ( ) } ` ) ;
77}
88
99function winPath ( path ) {
@@ -13,9 +13,9 @@ function winPath(path) {
1313function normalizeCustomName ( originCustomName ) {
1414 // If set to a string, treat it as a JavaScript source file path.
1515 if ( typeof originCustomName === 'string' ) {
16+ // eslint-disable-next-line import/no-dynamic-require
1617 const customNameExports = require ( originCustomName ) ;
17- return typeof customNameExports === 'function'
18- ? customNameExports : customNameExports . default ;
18+ return typeof customNameExports === 'function' ? customNameExports : customNameExports . default ;
1919 }
2020
2121 return originCustomName ;
@@ -34,56 +34,53 @@ export default class Plugin {
3434 customName ,
3535 transformToDefaultImport ,
3636 types ,
37- index = 0
37+ index = 0 ,
3838 ) {
3939 this . libraryName = libraryName ;
40- this . libraryDirectory = typeof libraryDirectory === 'undefined'
41- ? 'lib'
42- : libraryDirectory ;
43- this . camel2DashComponentName = typeof camel2DashComponentName === 'undefined'
44- ? true
45- : camel2DashComponentName ;
40+ this . libraryDirectory = typeof libraryDirectory === 'undefined' ? 'lib' : libraryDirectory ;
41+ this . camel2DashComponentName =
42+ typeof camel2DashComponentName === 'undefined' ? true : camel2DashComponentName ;
4643 this . camel2UnderlineComponentName = camel2UnderlineComponentName ;
4744 this . style = style || false ;
4845 this . styleLibraryDirectory = styleLibraryDirectory ;
4946 this . customStyleName = normalizeCustomName ( customStyleName ) ;
5047 this . fileName = fileName || '' ;
5148 this . customName = normalizeCustomName ( customName ) ;
52- this . transformToDefaultImport = typeof transformToDefaultImport === 'undefined'
53- ? true
54- : transformToDefaultImport ;
49+ this . transformToDefaultImport =
50+ typeof transformToDefaultImport === 'undefined' ? true : transformToDefaultImport ;
5551 this . types = types ;
5652 this . pluginStateKey = `importPluginState${ index } ` ;
5753 }
5854
5955 getPluginState ( state ) {
6056 if ( ! state [ this . pluginStateKey ] ) {
61- state [ this . pluginStateKey ] = { } ; // eslint-disable-line
57+ state [ this . pluginStateKey ] = { } ; // eslint-disable-line
6258 }
6359 return state [ this . pluginStateKey ] ;
6460 }
6561
6662 importMethod ( methodName , file , pluginState ) {
6763 if ( ! pluginState . selectedMethods [ methodName ] ) {
68- const libraryDirectory = this . libraryDirectory ;
69- const style = this . style ;
70- const transformedMethodName = this . camel2UnderlineComponentName // eslint-disable-line
64+ const { style, libraryDirectory } = this ;
65+ const transformedMethodName = this . camel2UnderlineComponentName // eslint-disable-line
7166 ? transCamel ( methodName , '_' )
7267 : this . camel2DashComponentName
73- ? transCamel ( methodName , '-' )
74- : methodName ;
68+ ? transCamel ( methodName , '-' )
69+ : methodName ;
7570 const path = winPath (
76- this . customName ? this . customName ( transformedMethodName , file ) : join ( this . libraryName , libraryDirectory , transformedMethodName , this . fileName ) // eslint-disable-line
71+ this . customName
72+ ? this . customName ( transformedMethodName , file )
73+ : join ( this . libraryName , libraryDirectory , transformedMethodName , this . fileName ) , // eslint-disable-line
7774 ) ;
78- pluginState . selectedMethods [ methodName ] = this . transformToDefaultImport // eslint-disable-line
75+ pluginState . selectedMethods [ methodName ] = this . transformToDefaultImport // eslint-disable-line
7976 ? addDefault ( file . path , path , { nameHint : methodName } )
8077 : addNamed ( file . path , methodName , path ) ;
8178 if ( this . customStyleName ) {
8279 const stylePath = winPath ( this . customStyleName ( transformedMethodName ) ) ;
8380 addSideEffect ( file . path , `${ stylePath } ` ) ;
8481 } else if ( this . styleLibraryDirectory ) {
8582 const stylePath = winPath (
86- join ( this . libraryName , this . styleLibraryDirectory , transformedMethodName , this . fileName )
83+ join ( this . libraryName , this . styleLibraryDirectory , transformedMethodName , this . fileName ) ,
8784 ) ;
8885 addSideEffect ( file . path , `${ stylePath } ` ) ;
8986 } else if ( style === true ) {
@@ -97,33 +94,35 @@ export default class Plugin {
9794 }
9895 }
9996 }
100- return Object . assign ( { } , pluginState . selectedMethods [ methodName ] ) ;
97+ return { ... pluginState . selectedMethods [ methodName ] } ;
10198 }
10299
103100 buildExpressionHandler ( node , props , path , state ) {
104101 const file = ( path && path . hub && path . hub . file ) || ( state && state . file ) ;
105- const types = this . types ;
102+ const { types } = this ;
106103 const pluginState = this . getPluginState ( state ) ;
107104 props . forEach ( prop => {
108105 if ( ! types . isIdentifier ( node [ prop ] ) ) return ;
109106 if (
110107 pluginState . specified [ node [ prop ] . name ] &&
111108 types . isImportSpecifier ( path . scope . getBinding ( node [ prop ] . name ) . path )
112109 ) {
113- node [ prop ] = this . importMethod ( pluginState . specified [ node [ prop ] . name ] , file , pluginState ) ; // eslint-disable-line
110+ node [ prop ] = this . importMethod ( pluginState . specified [ node [ prop ] . name ] , file , pluginState ) ; // eslint-disable-line
114111 }
115112 } ) ;
116113 }
117114
118115 buildDeclaratorHandler ( node , prop , path , state ) {
119116 const file = ( path && path . hub && path . hub . file ) || ( state && state . file ) ;
120- const types = this . types ;
117+ const { types } = this ;
121118 const pluginState = this . getPluginState ( state ) ;
122119 if ( ! types . isIdentifier ( node [ prop ] ) ) return ;
123- if ( pluginState . specified [ node [ prop ] . name ] &&
120+ if (
121+ pluginState . specified [ node [ prop ] . name ] &&
124122 path . scope . hasBinding ( node [ prop ] . name ) &&
125- path . scope . getBinding ( node [ prop ] . name ) . path . type === 'ImportSpecifier' ) {
126- node [ prop ] = this . importMethod ( pluginState . specified [ node [ prop ] . name ] , file , pluginState ) ; // eslint-disable-line
123+ path . scope . getBinding ( node [ prop ] . name ) . path . type === 'ImportSpecifier'
124+ ) {
125+ node [ prop ] = this . importMethod ( pluginState . specified [ node [ prop ] . name ] , file , pluginState ) ; // eslint-disable-line
127126 }
128127 }
129128
@@ -146,8 +145,8 @@ export default class Plugin {
146145 if ( ! node ) return ;
147146
148147 const { value } = node . source ;
149- const libraryName = this . libraryName ;
150- const types = this . types ;
148+ const { libraryName } = this ;
149+ const { types } = this ;
151150 const pluginState = this . getPluginState ( state ) ;
152151 if ( value === libraryName ) {
153152 node . specifiers . forEach ( spec => {
@@ -165,7 +164,7 @@ export default class Plugin {
165164 const { node } = path ;
166165 const file = ( path && path . hub && path . hub . file ) || ( state && state . file ) ;
167166 const { name } = node . callee ;
168- const types = this . types ;
167+ const { types } = this ;
169168 const pluginState = this . getPluginState ( state ) ;
170169
171170 if ( types . isIdentifier ( node . callee ) ) {
@@ -176,9 +175,11 @@ export default class Plugin {
176175
177176 node . arguments = node . arguments . map ( arg => {
178177 const { name : argName } = arg ;
179- if ( pluginState . specified [ argName ] &&
178+ if (
179+ pluginState . specified [ argName ] &&
180180 path . scope . hasBinding ( argName ) &&
181- path . scope . getBinding ( argName ) . path . type === 'ImportSpecifier' ) {
181+ path . scope . getBinding ( argName ) . path . type === 'ImportSpecifier'
182+ ) {
182183 return this . importMethod ( pluginState . specified [ argName ] , file , pluginState ) ;
183184 }
184185 return arg ;
@@ -197,14 +198,10 @@ export default class Plugin {
197198 // antd.Button -> _Button
198199 path . replaceWith ( this . importMethod ( node . property . name , file , pluginState ) ) ;
199200 } else if ( pluginState . specified [ node . object . name ] && path . scope . hasBinding ( node . object . name ) ) {
200- const scope = path . scope . getBinding ( node . object . name ) . scope ;
201+ const { scope } = path . scope . getBinding ( node . object . name ) ;
201202 // global variable in file scope
202203 if ( scope . path . parent . type === 'File' ) {
203- node . object = this . importMethod (
204- pluginState . specified [ node . object . name ] ,
205- file ,
206- pluginState
207- ) ;
204+ node . object = this . importMethod ( pluginState . specified [ node . object . name ] , file , pluginState ) ;
208205 }
209206 }
210207 }
0 commit comments