7
7
import Exports from '../ExportMap'
8
8
import resolve from 'eslint-module-utils/resolve'
9
9
import docsUrl from '../docsUrl'
10
+ import { dirname , join } from 'path'
11
+ import readPkgUp from 'read-pkg-up'
12
+ import values from 'object.values'
10
13
11
14
// eslint/lib/util/glob-util has been moved to eslint/lib/util/glob-utils with version 5.3
12
15
// and has been moved to eslint/lib/cli-engine/file-enumerator in version 6
@@ -80,7 +83,7 @@ const prepareImportsAndExports = (srcFiles, context) => {
80
83
if ( currentExports ) {
81
84
const { dependencies, reexports, imports : localImportList , namespace } = currentExports
82
85
83
- // dependencies === export * from
86
+ // dependencies === export * from
84
87
const currentExportAll = new Set ( )
85
88
dependencies . forEach ( value => {
86
89
currentExportAll . add ( value ( ) . path )
@@ -146,7 +149,7 @@ const prepareImportsAndExports = (srcFiles, context) => {
146
149
}
147
150
148
151
/**
149
- * traverse through all imports and add the respective path to the whereUsed-list
152
+ * traverse through all imports and add the respective path to the whereUsed-list
150
153
* of the corresponding export
151
154
*/
152
155
const determineUsage = ( ) => {
@@ -201,6 +204,58 @@ const newNamespaceImportExists = specifiers =>
201
204
const newDefaultImportExists = specifiers =>
202
205
specifiers . some ( ( { type } ) => type === IMPORT_DEFAULT_SPECIFIER )
203
206
207
+ const fileIsInPkg = file => {
208
+ const { path, pkg } = readPkgUp . sync ( { cwd : file , normalize : false } )
209
+ const basePath = dirname ( path )
210
+
211
+ const checkPkgFieldString = pkgField => {
212
+ if ( join ( basePath , pkgField ) === file ) {
213
+ return true
214
+ }
215
+ }
216
+
217
+ const checkPkgFieldObject = pkgField => {
218
+ const pkgFieldFiles = values ( pkgField ) . map ( value => join ( basePath , value ) )
219
+ if ( pkgFieldFiles . includes ( file ) ) {
220
+ return true
221
+ }
222
+ }
223
+
224
+ const checkPkgField = pkgField => {
225
+ if ( typeof pkgField === 'string' ) {
226
+ return checkPkgFieldString ( pkgField )
227
+ }
228
+
229
+ if ( typeof pkgField === 'object' ) {
230
+ return checkPkgFieldObject ( pkgField )
231
+ }
232
+ }
233
+
234
+ if ( pkg . private === true ) {
235
+ return false
236
+ }
237
+
238
+ if ( pkg . bin ) {
239
+ if ( checkPkgField ( pkg . bin ) ) {
240
+ return true
241
+ }
242
+ }
243
+
244
+ if ( pkg . browser ) {
245
+ if ( checkPkgField ( pkg . browser ) ) {
246
+ return true
247
+ }
248
+ }
249
+
250
+ if ( pkg . main ) {
251
+ if ( checkPkgFieldString ( pkg . main ) ) {
252
+ return true
253
+ }
254
+ }
255
+
256
+ return false
257
+ }
258
+
204
259
module . exports = {
205
260
meta : {
206
261
docs : { url : docsUrl ( 'no-unused-modules' ) } ,
@@ -315,6 +370,10 @@ module.exports = {
315
370
return
316
371
}
317
372
373
+ if ( fileIsInPkg ( file ) ) {
374
+ return
375
+ }
376
+
318
377
// refresh list of source files
319
378
const srcFiles = resolveFiles ( getSrc ( src ) , ignoreExports )
320
379
@@ -325,7 +384,7 @@ module.exports = {
325
384
326
385
exports = exportList . get ( file )
327
386
328
- // special case: export * from
387
+ // special case: export * from
329
388
const exportAll = exports . get ( EXPORT_ALL_DECLARATION )
330
389
if ( typeof exportAll !== 'undefined' && exportedValue !== IMPORT_DEFAULT_SPECIFIER ) {
331
390
if ( exportAll . whereUsed . size > 0 ) {
@@ -362,7 +421,7 @@ module.exports = {
362
421
363
422
/**
364
423
* only useful for tools like vscode-eslint
365
- *
424
+ *
366
425
* update lists of existing exports during runtime
367
426
*/
368
427
const updateExportUsage = node => {
@@ -384,7 +443,7 @@ module.exports = {
384
443
node . body . forEach ( ( { type, declaration, specifiers } ) => {
385
444
if ( type === EXPORT_DEFAULT_DECLARATION ) {
386
445
newExportIdentifiers . add ( IMPORT_DEFAULT_SPECIFIER )
387
- }
446
+ }
388
447
if ( type === EXPORT_NAMED_DECLARATION ) {
389
448
if ( specifiers . length > 0 ) {
390
449
specifiers . forEach ( specifier => {
@@ -399,7 +458,7 @@ module.exports = {
399
458
declaration . type === CLASS_DECLARATION
400
459
) {
401
460
newExportIdentifiers . add ( declaration . id . name )
402
- }
461
+ }
403
462
if ( declaration . type === VARIABLE_DECLARATION ) {
404
463
declaration . declarations . forEach ( ( { id } ) => {
405
464
newExportIdentifiers . add ( id . name )
@@ -438,7 +497,7 @@ module.exports = {
438
497
439
498
/**
440
499
* only useful for tools like vscode-eslint
441
- *
500
+ *
442
501
* update lists of existing imports during runtime
443
502
*/
444
503
const updateImportUsage = node => {
0 commit comments