@@ -126,11 +126,19 @@ function getModuleRealName(resolved) {
126
126
return getFilePackageName ( resolved ) ;
127
127
}
128
128
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
+
130
138
// in case of sub package.json inside a module
131
139
// check the dependencies on all hierarchy
132
140
const packageHierarchy = [ ] ;
133
- const packageNameParts = packageName . split ( '/' ) ;
141
+ const packageNameParts = packageName ? packageName . split ( '/' ) : [ ] ;
134
142
packageNameParts . forEach ( ( namePart , index ) => {
135
143
if ( ! namePart . startsWith ( '@' ) ) {
136
144
const ancestor = packageNameParts . slice ( 0 , index + 1 ) . join ( '/' ) ;
@@ -147,18 +155,16 @@ function checkDependencyDeclaration(deps, packageName) {
147
155
isInBundledDeps :
148
156
result . isInBundledDeps || deps . bundledDependencies . indexOf ( ancestorName ) !== - 1 ,
149
157
} ;
150
- } , {
151
- isInDeps : false ,
152
- isInDevDeps : false ,
153
- isInOptDeps : false ,
154
- isInPeerDeps : false ,
155
- isInBundledDeps : false ,
156
- } ) ;
158
+ } , newDeclarationStatus ) ;
157
159
}
158
160
159
161
function reportIfMissing ( context , deps , depsOptions , node , name ) {
160
162
// 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
+ ) {
162
168
return ;
163
169
}
164
170
@@ -170,48 +176,46 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
170
176
if ( ! resolved ) { return ; }
171
177
172
178
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 )
180
187
) {
181
188
return ;
182
189
}
183
190
184
191
// 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
186
193
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
+ }
196
206
}
197
207
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 ) ) ;
203
210
return ;
204
211
}
205
212
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 ) ) ;
211
215
return ;
212
216
}
213
217
214
- context . report ( node , missingErrorMessage ( realPackageName ) ) ;
218
+ context . report ( node , missingErrorMessage ( realPackageName || importPackageName ) ) ;
215
219
}
216
220
217
221
function testConfig ( config , filename ) {
0 commit comments