@@ -126,11 +126,19 @@ function getModuleRealName(resolved) {
126126 return getFilePackageName ( resolved ) ;
127127}
128128
129- function checkDependencyDeclaration ( deps , packageName ) {
129+ function checkDependencyDeclaration ( deps , packageName , declarationStatus ) {
130+ const newDeclarationStatus = declarationStatus || {
131+ isInDeps : false ,
132+ isInDevDeps : false ,
133+ isInOptDeps : false ,
134+ isInPeerDeps : false ,
135+ isInBundledDeps : false ,
136+ } ;
137+
130138 // in case of sub package.json inside a module
131139 // check the dependencies on all hierarchy
132140 const packageHierarchy = [ ] ;
133- const packageNameParts = packageName . split ( '/' ) ;
141+ const packageNameParts = packageName ? packageName . split ( '/' ) : [ ] ;
134142 packageNameParts . forEach ( ( namePart , index ) => {
135143 if ( ! namePart . startsWith ( '@' ) ) {
136144 const ancestor = packageNameParts . slice ( 0 , index + 1 ) . join ( '/' ) ;
@@ -147,18 +155,16 @@ function checkDependencyDeclaration(deps, packageName) {
147155 isInBundledDeps :
148156 result . isInBundledDeps || deps . bundledDependencies . indexOf ( ancestorName ) !== - 1 ,
149157 } ;
150- } , {
151- isInDeps : false ,
152- isInDevDeps : false ,
153- isInOptDeps : false ,
154- isInPeerDeps : false ,
155- isInBundledDeps : false ,
156- } ) ;
158+ } , newDeclarationStatus ) ;
157159}
158160
159161function reportIfMissing ( context , deps , depsOptions , node , name ) {
160162 // Do not report when importing types
161- if ( node . importKind === 'type' || ( node . parent && node . parent . importKind === 'type' ) || node . importKind === 'typeof' ) {
163+ if (
164+ node . importKind === 'type' ||
165+ ( node . parent && node . parent . importKind === 'type' ) ||
166+ node . importKind === 'typeof'
167+ ) {
162168 return ;
163169 }
164170
@@ -170,48 +176,46 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
170176 if ( ! resolved ) { return ; }
171177
172178 const importPackageName = getModuleOriginalName ( name ) ;
173- const importPackageNameDeclaration = checkDependencyDeclaration ( deps , importPackageName ) ;
174-
175- if ( importPackageNameDeclaration . isInDeps ||
176- ( depsOptions . allowDevDeps && importPackageNameDeclaration . isInDevDeps ) ||
177- ( depsOptions . allowPeerDeps && importPackageNameDeclaration . isInPeerDeps ) ||
178- ( depsOptions . allowOptDeps && importPackageNameDeclaration . isInOptDeps ) ||
179- ( depsOptions . allowBundledDeps && importPackageNameDeclaration . isInBundledDeps )
179+ let declarationStatus = checkDependencyDeclaration ( deps , importPackageName ) ;
180+
181+ if (
182+ declarationStatus . isInDeps ||
183+ ( depsOptions . allowDevDeps && declarationStatus . isInDevDeps ) ||
184+ ( depsOptions . allowPeerDeps && declarationStatus . isInPeerDeps ) ||
185+ ( depsOptions . allowOptDeps && declarationStatus . isInOptDeps ) ||
186+ ( depsOptions . allowBundledDeps && declarationStatus . isInBundledDeps )
180187 ) {
181188 return ;
182189 }
183190
184191 // test the real name from the resolved package.json
185- // if not aliased imports (alias/react for example), importPackageName can be misinterpreted
192+ // if not aliased imports (alias/react for example), importPackageName can be misinterpreted
186193 const realPackageName = getModuleRealName ( resolved ) ;
187- const realPackageNameDeclaration = checkDependencyDeclaration ( deps , realPackageName ) ;
188-
189- if ( realPackageNameDeclaration . isInDeps ||
190- ( depsOptions . allowDevDeps && realPackageNameDeclaration . isInDevDeps ) ||
191- ( depsOptions . allowPeerDeps && realPackageNameDeclaration . isInPeerDeps ) ||
192- ( depsOptions . allowOptDeps && realPackageNameDeclaration . isInOptDeps ) ||
193- ( depsOptions . allowBundledDeps && realPackageNameDeclaration . isInBundledDeps )
194- ) {
195- return ;
194+ if ( realPackageName && realPackageName !== importPackageName ) {
195+ declarationStatus = checkDependencyDeclaration ( deps , realPackageName , declarationStatus ) ;
196+
197+ if (
198+ declarationStatus . isInDeps ||
199+ ( depsOptions . allowDevDeps && declarationStatus . isInDevDeps ) ||
200+ ( depsOptions . allowPeerDeps && declarationStatus . isInPeerDeps ) ||
201+ ( depsOptions . allowOptDeps && declarationStatus . isInOptDeps ) ||
202+ ( depsOptions . allowBundledDeps && declarationStatus . isInBundledDeps )
203+ ) {
204+ return ;
205+ }
196206 }
197207
198- if ( (
199- importPackageNameDeclaration . isInDevDeps ||
200- realPackageNameDeclaration . isInDevDeps
201- ) && ! depsOptions . allowDevDeps ) {
202- context . report ( node , devDepErrorMessage ( realPackageName ) ) ;
208+ if ( declarationStatus . isInDevDeps && ! depsOptions . allowDevDeps ) {
209+ context . report ( node , devDepErrorMessage ( realPackageName || importPackageName ) ) ;
203210 return ;
204211 }
205212
206- if ( (
207- importPackageNameDeclaration . isInOptDeps ||
208- realPackageNameDeclaration . isInOptDeps
209- ) && ! depsOptions . allowOptDeps ) {
210- context . report ( node , optDepErrorMessage ( realPackageName ) ) ;
213+ if ( declarationStatus . isInOptDeps && ! depsOptions . allowOptDeps ) {
214+ context . report ( node , optDepErrorMessage ( realPackageName || importPackageName ) ) ;
211215 return ;
212216 }
213217
214- context . report ( node , missingErrorMessage ( realPackageName ) ) ;
218+ context . report ( node , missingErrorMessage ( realPackageName || importPackageName ) ) ;
215219}
216220
217221function testConfig ( config , filename ) {
0 commit comments