@@ -20,30 +20,38 @@ const packages = getPackages(process.cwd()).map(
2020 } ) ,
2121) ;
2222
23+ const isImportExpression = expression =>
24+ expression . type === 'CallExpression' &&
25+ expression . arguments . length > 0 &&
26+ expression . arguments [ 0 ] . type === 'Literal' &&
27+ ( expression . callee . type === 'Import' ||
28+ ( expression . callee . type === 'Identifier' &&
29+ expression . callee . name === 'require' ) ) ;
30+
31+ const resolveImport = ( context , node , { value, range } ) => {
32+ const path = resolve ( dirname ( context . getFilename ( ) ) , value ) ;
33+ const [ start , end ] = range ;
34+ return { node, value, path, start, end } ;
35+ } ;
36+
2337const getImport = ( context , callback ) => ( {
2438 ImportDeclaration : node => {
2539 if ( node . source . type === 'Literal' ) {
26- const { value, range } = node . source ;
27- const path = resolve ( dirname ( context . getFilename ( ) ) , value ) ;
28- const [ start , end ] = range ;
29- callback ( { node, value, path, start, end } ) ;
40+ callback ( resolveImport ( context , node , node . source ) ) ;
3041 }
3142 } ,
3243 ExpressionStatement : node => {
33- if (
34- node . expression . type === 'CallExpression' &&
35- node . expression . arguments . length > 0 &&
36- node . expression . arguments [ 0 ] . type === 'Literal' &&
37- ( node . expression . callee . type === 'Import' ||
38- ( node . expression . callee . type === 'Identifier' &&
39- node . expression . callee . name === 'require' ) )
40- ) {
41- const { value, range } = node . expression . arguments [ 0 ] ;
42- const path = resolve ( dirname ( context . getFilename ( ) ) , value ) ;
43- const [ start , end ] = range ;
44- callback ( { node, value, path, start, end } ) ;
44+ if ( isImportExpression ( node . expression ) ) {
45+ callback ( resolveImport ( context , node , node . expression . arguments [ 0 ] ) ) ;
4546 }
4647 } ,
48+ VariableDeclaration : node => {
49+ node . declarations . forEach ( ( { init } ) => {
50+ if ( isImportExpression ( init ) ) {
51+ callback ( resolveImport ( context , node , init . arguments [ 0 ] ) ) ;
52+ }
53+ } ) ;
54+ } ,
4755} ) ;
4856
4957module . exports = { isSubPath, packages, getImport } ;
0 commit comments