@@ -13,6 +13,11 @@ function baseModule(name) {
13
13
return pkg ;
14
14
}
15
15
16
+ function isInternalRegexMatch ( name , settings ) {
17
+ const internalScope = ( settings && settings [ 'import/internal-regex' ] ) ;
18
+ return internalScope && new RegExp ( internalScope ) . test ( name ) ;
19
+ }
20
+
16
21
export function isAbsolute ( name ) {
17
22
return typeof name === 'string' && nodeIsAbsolute ( name ) ;
18
23
}
@@ -25,33 +30,18 @@ export function isBuiltIn(name, settings, path) {
25
30
return isCoreModule ( base ) || extras . indexOf ( base ) > - 1 ;
26
31
}
27
32
28
- export function isExternalModule ( name , settings , path , context ) {
29
- if ( arguments . length < 4 ) {
30
- throw new TypeError ( 'isExternalModule: name, settings, path, and context are all required' ) ;
33
+ export function isExternalModule ( name , path , context ) {
34
+ if ( arguments . length < 3 ) {
35
+ throw new TypeError ( 'isExternalModule: name, path, and context are all required' ) ;
31
36
}
32
- return ( isModule ( name ) || isScoped ( name ) ) && isExternalPath ( name , settings , path , getContextPackagePath ( context ) ) ;
33
- }
34
-
35
- export function isExternalModuleMain ( name , settings , path , context ) {
36
- return isModuleMain ( name ) && isExternalPath ( name , settings , path , getContextPackagePath ( context ) ) ;
37
+ return ( isModule ( name ) || isScoped ( name ) ) && typeTest ( name , context , path ) === 'external' ;
37
38
}
38
39
39
- function isExternalPath ( name , settings , path , packagePath ) {
40
- const internalScope = ( settings && settings [ 'import/internal-regex' ] ) ;
41
- if ( internalScope && new RegExp ( internalScope ) . test ( name ) ) {
42
- return false ;
43
- }
44
-
45
- if ( ! path || relative ( packagePath , path ) . startsWith ( '..' ) ) {
46
- return true ;
40
+ export function isExternalModuleMain ( name , path , context ) {
41
+ if ( arguments . length < 3 ) {
42
+ throw new TypeError ( 'isExternalModule: name, path, and context are all required' ) ;
47
43
}
48
-
49
- const folders = ( settings && settings [ 'import/external-module-folders' ] ) || [ 'node_modules' ] ;
50
- return folders . some ( ( folder ) => {
51
- const folderPath = nodeResolve ( packagePath , folder ) ;
52
- const relativePath = relative ( folderPath , path ) ;
53
- return ! relativePath . startsWith ( '..' ) ;
54
- } ) ;
44
+ return isModuleMain ( name ) && typeTest ( name , context , path ) === 'external' ;
55
45
}
56
46
57
47
const moduleRegExp = / ^ \w / ;
@@ -87,17 +77,49 @@ function isRelativeToSibling(name) {
87
77
return / ^ \. [ \\ / ] / . test ( name ) ;
88
78
}
89
79
90
- function typeTest ( name , context , path ) {
80
+ function isExternalPath ( path , context ) {
81
+ if ( ! path ) {
82
+ return false ;
83
+ }
84
+
85
+ const { settings } = context ;
86
+ const packagePath = getContextPackagePath ( context ) ;
87
+
88
+ if ( relative ( packagePath , path ) . startsWith ( '..' ) ) {
89
+ return true ;
90
+ }
91
+
92
+ const folders = ( settings && settings [ 'import/external-module-folders' ] ) || [ 'node_modules' ] ;
93
+ return folders . some ( ( folder ) => {
94
+ const folderPath = nodeResolve ( packagePath , folder ) ;
95
+ const relativePath = relative ( folderPath , path ) ;
96
+ return ! relativePath . startsWith ( '..' ) ;
97
+ } ) ;
98
+ }
99
+
100
+ function isInternalPath ( path , context ) {
101
+ if ( ! path ) {
102
+ return false ;
103
+ }
104
+ const packagePath = getContextPackagePath ( context ) ;
105
+ return ! relative ( packagePath , path ) . startsWith ( '../' ) ;
106
+ }
107
+
108
+ function isExternalLookingName ( name ) {
109
+ return isModule ( name ) || isScoped ( name ) ;
110
+ }
111
+
112
+ function typeTest ( name , context , path ) {
91
113
const { settings } = context ;
114
+ if ( isInternalRegexMatch ( name , settings ) ) { return 'internal' ; }
92
115
if ( isAbsolute ( name , settings , path ) ) { return 'absolute' ; }
93
116
if ( isBuiltIn ( name , settings , path ) ) { return 'builtin' ; }
94
- if ( isModule ( name , settings , path ) || isScoped ( name , settings , path ) ) {
95
- const packagePath = getContextPackagePath ( context ) ;
96
- return isExternalPath ( name , settings , path , packagePath ) ? 'external' : 'internal' ;
97
- }
98
117
if ( isRelativeToParent ( name , settings , path ) ) { return 'parent' ; }
99
118
if ( isIndex ( name , settings , path ) ) { return 'index' ; }
100
119
if ( isRelativeToSibling ( name , settings , path ) ) { return 'sibling' ; }
120
+ if ( isExternalPath ( path , context ) ) { return 'external' ; }
121
+ if ( isInternalPath ( path , context ) ) { return 'internal' ; }
122
+ if ( isExternalLookingName ( name ) ) { return 'external' ; }
101
123
return 'unknown' ;
102
124
}
103
125
0 commit comments