-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathno-unused-modules.js
737 lines (656 loc) · 22 KB
/
no-unused-modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/**
* @fileOverview Ensures that modules contain exports and/or all
* modules are consumed within other modules.
* @author René Fermann
*/
import Exports from '../ExportMap'
import resolve from 'eslint-module-utils/resolve'
import docsUrl from '../docsUrl'
// eslint/lib/util/glob-util has been moved to eslint/lib/util/glob-utils with version 5.3
// and has been moved to eslint/lib/cli-engine/file-enumerator in version 6
let listFilesToProcess
try {
var FileEnumerator = require('eslint/lib/cli-engine/file-enumerator').FileEnumerator
listFilesToProcess = function (src) {
var e = new FileEnumerator()
return Array.from(e.iterateFiles(src), ({ filePath, ignored }) => ({
ignored,
filename: filePath,
}))
}
} catch (e1) {
try {
listFilesToProcess = require('eslint/lib/util/glob-utils').listFilesToProcess
} catch (e2) {
listFilesToProcess = require('eslint/lib/util/glob-util').listFilesToProcess
}
}
const EXPORT_DEFAULT_DECLARATION = 'ExportDefaultDeclaration'
const EXPORT_NAMED_DECLARATION = 'ExportNamedDeclaration'
const EXPORT_ALL_DECLARATION = 'ExportAllDeclaration'
const IMPORT_DECLARATION = 'ImportDeclaration'
const IMPORT_NAMESPACE_SPECIFIER = 'ImportNamespaceSpecifier'
const IMPORT_DEFAULT_SPECIFIER = 'ImportDefaultSpecifier'
const VARIABLE_DECLARATION = 'VariableDeclaration'
const FUNCTION_DECLARATION = 'FunctionDeclaration'
const CLASS_DECLARATION = 'ClassDeclaration'
const DEFAULT = 'default'
let preparationDone = false
const importList = new Map()
const exportList = new Map()
const ignoredFiles = new Set()
const isNodeModule = path => {
return /\/(node_modules)\//.test(path)
}
/**
* read all files matching the patterns in src and ignoreExports
*
* return all files matching src pattern, which are not matching the ignoreExports pattern
*/
const resolveFiles = (src, ignoreExports) => {
const srcFiles = new Set()
const srcFileList = listFilesToProcess(src)
// prepare list of ignored files
const ignoredFilesList = listFilesToProcess(ignoreExports)
ignoredFilesList.forEach(({ filename }) => ignoredFiles.add(filename))
// prepare list of source files, don't consider files from node_modules
srcFileList.filter(({ filename }) => !isNodeModule(filename)).forEach(({ filename }) => {
srcFiles.add(filename)
})
return srcFiles
}
/**
* parse all source files and build up 2 maps containing the existing imports and exports
*/
const prepareImportsAndExports = (srcFiles, context) => {
const exportAll = new Map()
srcFiles.forEach(file => {
const exports = new Map()
const imports = new Map()
const currentExports = Exports.get(file, context)
if (currentExports) {
const { dependencies, reexports, imports: localImportList, namespace } = currentExports
// dependencies === export * from
const currentExportAll = new Set()
dependencies.forEach(value => {
currentExportAll.add(value().path)
})
exportAll.set(file, currentExportAll)
reexports.forEach((value, key) => {
if (key === DEFAULT) {
exports.set(IMPORT_DEFAULT_SPECIFIER, { whereUsed: new Set() })
} else {
exports.set(key, { whereUsed: new Set() })
}
const reexport = value.getImport()
if (!reexport) {
return
}
let localImport = imports.get(reexport.path)
let currentValue
if (value.local === DEFAULT) {
currentValue = IMPORT_DEFAULT_SPECIFIER
} else {
currentValue = value.local
}
if (typeof localImport !== 'undefined') {
localImport = new Set([...localImport, currentValue])
} else {
localImport = new Set([currentValue])
}
imports.set(reexport.path, localImport)
})
localImportList.forEach((value, key) => {
if (isNodeModule(key)) {
return
}
imports.set(key, value.importedSpecifiers)
})
importList.set(file, imports)
// build up export list only, if file is not ignored
if (ignoredFiles.has(file)) {
return
}
namespace.forEach((value, key) => {
if (key === DEFAULT) {
exports.set(IMPORT_DEFAULT_SPECIFIER, { whereUsed: new Set() })
} else {
exports.set(key, { whereUsed: new Set() })
}
})
}
exports.set(EXPORT_ALL_DECLARATION, { whereUsed: new Set() })
exports.set(IMPORT_NAMESPACE_SPECIFIER, { whereUsed: new Set() })
exportList.set(file, exports)
})
exportAll.forEach((value, key) => {
value.forEach(val => {
const currentExports = exportList.get(val)
const currentExport = currentExports.get(EXPORT_ALL_DECLARATION)
currentExport.whereUsed.add(key)
})
})
}
/**
* traverse through all imports and add the respective path to the whereUsed-list
* of the corresponding export
*/
const determineUsage = () => {
importList.forEach((listValue, listKey) => {
listValue.forEach((value, key) => {
const exports = exportList.get(key)
if (typeof exports !== 'undefined') {
value.forEach(currentImport => {
let specifier
if (currentImport === IMPORT_NAMESPACE_SPECIFIER) {
specifier = IMPORT_NAMESPACE_SPECIFIER
} else if (currentImport === IMPORT_DEFAULT_SPECIFIER) {
specifier = IMPORT_DEFAULT_SPECIFIER
} else {
specifier = currentImport
}
if (typeof specifier !== 'undefined') {
const exportStatement = exports.get(specifier)
if (typeof exportStatement !== 'undefined') {
const { whereUsed } = exportStatement
whereUsed.add(listKey)
exports.set(specifier, { whereUsed })
}
}
})
}
})
})
}
const getSrc = src => {
if (src) {
return src
}
return [process.cwd()]
}
/**
* prepare the lists of existing imports and exports - should only be executed once at
* the start of a new eslint run
*/
const doPreparation = (src, ignoreExports, context) => {
const srcFiles = resolveFiles(getSrc(src), ignoreExports)
prepareImportsAndExports(srcFiles, context)
determineUsage()
preparationDone = true
}
const newNamespaceImportExists = specifiers =>
specifiers.some(({ type }) => type === IMPORT_NAMESPACE_SPECIFIER)
const newDefaultImportExists = specifiers =>
specifiers.some(({ type }) => type === IMPORT_DEFAULT_SPECIFIER)
module.exports = {
meta: {
docs: { url: docsUrl('no-unused-modules') },
schema: [{
properties: {
src: {
description: 'files/paths to be analyzed (only for unused exports)',
type: 'array',
minItems: 1,
items: {
type: 'string',
minLength: 1,
},
},
ignoreExports: {
description:
'files/paths for which unused exports will not be reported (e.g module entry points)',
type: 'array',
minItems: 1,
items: {
type: 'string',
minLength: 1,
},
},
missingExports: {
description: 'report modules without any exports',
type: 'boolean',
},
unusedExports: {
description: 'report exports without any usage',
type: 'boolean',
},
},
not: {
properties: {
unusedExports: { enum: [false] },
missingExports: { enum: [false] },
},
},
anyOf:[{
not: {
properties: {
unusedExports: { enum: [true] },
},
},
required: ['missingExports'],
}, {
not: {
properties: {
missingExports: { enum: [true] },
},
},
required: ['unusedExports'],
}, {
properties: {
unusedExports: { enum: [true] },
},
required: ['unusedExports'],
}, {
properties: {
missingExports: { enum: [true] },
},
required: ['missingExports'],
}],
}],
},
create: context => {
const {
src,
ignoreExports = [],
missingExports,
unusedExports,
} = context.options[0] || {}
if (unusedExports && !preparationDone) {
doPreparation(src, ignoreExports, context)
}
const file = context.getFilename()
const checkExportPresence = node => {
if (!missingExports) {
return
}
if (ignoredFiles.has(file)) {
return
}
const exportCount = exportList.get(file)
const exportAll = exportCount.get(EXPORT_ALL_DECLARATION)
const namespaceImports = exportCount.get(IMPORT_NAMESPACE_SPECIFIER)
exportCount.delete(EXPORT_ALL_DECLARATION)
exportCount.delete(IMPORT_NAMESPACE_SPECIFIER)
if (missingExports && exportCount.size < 1) {
// node.body[0] === 'undefined' only happens, if everything is commented out in the file
// being linted
context.report(node.body[0] ? node.body[0] : node, 'No exports found')
}
exportCount.set(EXPORT_ALL_DECLARATION, exportAll)
exportCount.set(IMPORT_NAMESPACE_SPECIFIER, namespaceImports)
}
const checkUsage = (node, exportedValue) => {
if (!unusedExports) {
return
}
if (ignoredFiles.has(file)) {
return
}
// refresh list of source files
const srcFiles = resolveFiles(getSrc(src), ignoreExports)
// make sure file to be linted is included in source files
if (!srcFiles.has(file)) {
return
}
exports = exportList.get(file)
// special case: export * from
const exportAll = exports.get(EXPORT_ALL_DECLARATION)
if (typeof exportAll !== 'undefined' && exportedValue !== IMPORT_DEFAULT_SPECIFIER) {
if (exportAll.whereUsed.size > 0) {
return
}
}
// special case: namespace import
const namespaceImports = exports.get(IMPORT_NAMESPACE_SPECIFIER)
if (typeof namespaceImports !== 'undefined') {
if (namespaceImports.whereUsed.size > 0) {
return
}
}
const exportStatement = exports.get(exportedValue)
const value = exportedValue === IMPORT_DEFAULT_SPECIFIER ? DEFAULT : exportedValue
if (typeof exportStatement !== 'undefined'){
if (exportStatement.whereUsed.size < 1) {
context.report(
node,
`exported declaration '${value}' not used within other modules`
)
}
} else {
context.report(
node,
`exported declaration '${value}' not used within other modules`
)
}
}
/**
* only useful for tools like vscode-eslint
*
* update lists of existing exports during runtime
*/
const updateExportUsage = node => {
if (ignoredFiles.has(file)) {
return
}
let exports = exportList.get(file)
// new module has been created during runtime
// include it in further processing
if (typeof exports === 'undefined') {
exports = new Map()
}
const newExports = new Map()
const newExportIdentifiers = new Set()
node.body.forEach(({ type, declaration, specifiers }) => {
if (type === EXPORT_DEFAULT_DECLARATION) {
newExportIdentifiers.add(IMPORT_DEFAULT_SPECIFIER)
}
if (type === EXPORT_NAMED_DECLARATION) {
if (specifiers.length > 0) {
specifiers.forEach(specifier => {
if (specifier.exported) {
newExportIdentifiers.add(specifier.exported.name)
}
})
}
if (declaration) {
if (
declaration.type === FUNCTION_DECLARATION ||
declaration.type === CLASS_DECLARATION
) {
newExportIdentifiers.add(declaration.id.name)
}
if (declaration.type === VARIABLE_DECLARATION) {
declaration.declarations.forEach(({ id }) => {
newExportIdentifiers.add(id.name)
})
}
}
}
})
// old exports exist within list of new exports identifiers: add to map of new exports
exports.forEach((value, key) => {
if (newExportIdentifiers.has(key)) {
newExports.set(key, value)
}
})
// new export identifiers added: add to map of new exports
newExportIdentifiers.forEach(key => {
if (!exports.has(key)) {
newExports.set(key, { whereUsed: new Set() })
}
})
// preserve information about namespace imports
let exportAll = exports.get(EXPORT_ALL_DECLARATION)
let namespaceImports = exports.get(IMPORT_NAMESPACE_SPECIFIER)
if (typeof namespaceImports === 'undefined') {
namespaceImports = { whereUsed: new Set() }
}
newExports.set(EXPORT_ALL_DECLARATION, exportAll)
newExports.set(IMPORT_NAMESPACE_SPECIFIER, namespaceImports)
exportList.set(file, newExports)
}
/**
* only useful for tools like vscode-eslint
*
* update lists of existing imports during runtime
*/
const updateImportUsage = node => {
if (!unusedExports) {
return
}
let oldImportPaths = importList.get(file)
if (typeof oldImportPaths === 'undefined') {
oldImportPaths = new Map()
}
const oldNamespaceImports = new Set()
const newNamespaceImports = new Set()
const oldExportAll = new Set()
const newExportAll = new Set()
const oldDefaultImports = new Set()
const newDefaultImports = new Set()
const oldImports = new Map()
const newImports = new Map()
oldImportPaths.forEach((value, key) => {
if (value.has(EXPORT_ALL_DECLARATION)) {
oldExportAll.add(key)
}
if (value.has(IMPORT_NAMESPACE_SPECIFIER)) {
oldNamespaceImports.add(key)
}
if (value.has(IMPORT_DEFAULT_SPECIFIER)) {
oldDefaultImports.add(key)
}
value.forEach(val => {
if (val !== IMPORT_NAMESPACE_SPECIFIER &&
val !== IMPORT_DEFAULT_SPECIFIER) {
oldImports.set(val, key)
}
})
})
node.body.forEach(astNode => {
let resolvedPath
// support for export { value } from 'module'
if (astNode.type === EXPORT_NAMED_DECLARATION) {
if (astNode.source) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
astNode.specifiers.forEach(specifier => {
let name
if (specifier.exported.name === DEFAULT) {
name = IMPORT_DEFAULT_SPECIFIER
} else {
name = specifier.local.name
}
newImports.set(name, resolvedPath)
})
}
}
if (astNode.type === EXPORT_ALL_DECLARATION) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
newExportAll.add(resolvedPath)
}
if (astNode.type === IMPORT_DECLARATION) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
if (!resolvedPath) {
return
}
if (isNodeModule(resolvedPath)) {
return
}
if (newNamespaceImportExists(astNode.specifiers)) {
newNamespaceImports.add(resolvedPath)
}
if (newDefaultImportExists(astNode.specifiers)) {
newDefaultImports.add(resolvedPath)
}
astNode.specifiers.forEach(specifier => {
if (specifier.type === IMPORT_DEFAULT_SPECIFIER ||
specifier.type === IMPORT_NAMESPACE_SPECIFIER) {
return
}
newImports.set(specifier.imported.name, resolvedPath)
})
}
})
newExportAll.forEach(value => {
if (!oldExportAll.has(value)) {
let imports = oldImportPaths.get(value)
if (typeof imports === 'undefined') {
imports = new Set()
}
imports.add(EXPORT_ALL_DECLARATION)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (typeof exports !== 'undefined') {
currentExport = exports.get(EXPORT_ALL_DECLARATION)
} else {
exports = new Map()
exportList.set(value, exports)
}
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.add(file)
} else {
const whereUsed = new Set()
whereUsed.add(file)
exports.set(EXPORT_ALL_DECLARATION, { whereUsed })
}
}
})
oldExportAll.forEach(value => {
if (!newExportAll.has(value)) {
const imports = oldImportPaths.get(value)
imports.delete(EXPORT_ALL_DECLARATION)
const exports = exportList.get(value)
if (typeof exports !== 'undefined') {
const currentExport = exports.get(EXPORT_ALL_DECLARATION)
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.delete(file)
}
}
}
})
newDefaultImports.forEach(value => {
if (!oldDefaultImports.has(value)) {
let imports = oldImportPaths.get(value)
if (typeof imports === 'undefined') {
imports = new Set()
}
imports.add(IMPORT_DEFAULT_SPECIFIER)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (typeof exports !== 'undefined') {
currentExport = exports.get(IMPORT_DEFAULT_SPECIFIER)
} else {
exports = new Map()
exportList.set(value, exports)
}
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.add(file)
} else {
const whereUsed = new Set()
whereUsed.add(file)
exports.set(IMPORT_DEFAULT_SPECIFIER, { whereUsed })
}
}
})
oldDefaultImports.forEach(value => {
if (!newDefaultImports.has(value)) {
const imports = oldImportPaths.get(value)
imports.delete(IMPORT_DEFAULT_SPECIFIER)
const exports = exportList.get(value)
if (typeof exports !== 'undefined') {
const currentExport = exports.get(IMPORT_DEFAULT_SPECIFIER)
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.delete(file)
}
}
}
})
newNamespaceImports.forEach(value => {
if (!oldNamespaceImports.has(value)) {
let imports = oldImportPaths.get(value)
if (typeof imports === 'undefined') {
imports = new Set()
}
imports.add(IMPORT_NAMESPACE_SPECIFIER)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (typeof exports !== 'undefined') {
currentExport = exports.get(IMPORT_NAMESPACE_SPECIFIER)
} else {
exports = new Map()
exportList.set(value, exports)
}
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.add(file)
} else {
const whereUsed = new Set()
whereUsed.add(file)
exports.set(IMPORT_NAMESPACE_SPECIFIER, { whereUsed })
}
}
})
oldNamespaceImports.forEach(value => {
if (!newNamespaceImports.has(value)) {
const imports = oldImportPaths.get(value)
imports.delete(IMPORT_NAMESPACE_SPECIFIER)
const exports = exportList.get(value)
if (typeof exports !== 'undefined') {
const currentExport = exports.get(IMPORT_NAMESPACE_SPECIFIER)
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.delete(file)
}
}
}
})
newImports.forEach((value, key) => {
if (!oldImports.has(key)) {
let imports = oldImportPaths.get(value)
if (typeof imports === 'undefined') {
imports = new Set()
}
imports.add(key)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (typeof exports !== 'undefined') {
currentExport = exports.get(key)
} else {
exports = new Map()
exportList.set(value, exports)
}
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.add(file)
} else {
const whereUsed = new Set()
whereUsed.add(file)
exports.set(key, { whereUsed })
}
}
})
oldImports.forEach((value, key) => {
if (!newImports.has(key)) {
const imports = oldImportPaths.get(value)
imports.delete(key)
const exports = exportList.get(value)
if (typeof exports !== 'undefined') {
const currentExport = exports.get(key)
if (typeof currentExport !== 'undefined') {
currentExport.whereUsed.delete(file)
}
}
}
})
}
return {
'Program:exit': node => {
updateExportUsage(node)
updateImportUsage(node)
checkExportPresence(node)
},
'ExportDefaultDeclaration': node => {
checkUsage(node, IMPORT_DEFAULT_SPECIFIER)
},
'ExportNamedDeclaration': node => {
node.specifiers.forEach(specifier => {
checkUsage(node, specifier.exported.name)
})
if (node.declaration) {
if (
node.declaration.type === FUNCTION_DECLARATION ||
node.declaration.type === CLASS_DECLARATION
) {
checkUsage(node, node.declaration.id.name)
}
if (node.declaration.type === VARIABLE_DECLARATION) {
node.declaration.declarations.forEach(declaration => {
checkUsage(node, declaration.id.name)
})
}
}
},
}
},
}