forked from import-js/eslint-plugin-import
-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
no-unused-modules.ts
1020 lines (907 loc) · 29.2 KB
/
no-unused-modules.ts
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Ensures that modules contain exports and/or all
* modules are consumed within other modules.
*/
import path from 'node:path'
import { TSESTree } from '@typescript-eslint/utils'
import { FileEnumerator } from 'eslint/use-at-your-own-risk'
import type { FileExtension, RuleContext } from '../types'
import {
ExportMap,
recursivePatternCapture,
createRule,
resolve,
getFileExtensions,
readPkgUp,
visit,
getValue,
} from '../utils'
function listFilesToProcess(src: string[], extensions: FileExtension[]) {
const enumerator = new FileEnumerator({
extensions,
})
return Array.from(enumerator.iterateFiles(src), ({ filePath, ignored }) => ({
ignored,
filename: filePath,
}))
}
const DEFAULT = 'default'
const { AST_NODE_TYPES } = TSESTree
function forEachDeclarationIdentifier(
declaration: TSESTree.Node | null,
cb: (name: string, isTypeExport: boolean) => void,
) {
if (declaration) {
const isTypeDeclaration =
declaration.type === AST_NODE_TYPES.TSInterfaceDeclaration ||
declaration.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
declaration.type === AST_NODE_TYPES.TSEnumDeclaration
if (
declaration.type === AST_NODE_TYPES.FunctionDeclaration ||
declaration.type === AST_NODE_TYPES.ClassDeclaration ||
isTypeDeclaration
) {
cb(declaration.id!.name, isTypeDeclaration)
} else if (declaration.type === AST_NODE_TYPES.VariableDeclaration) {
for (const { id } of declaration.declarations) {
if (id.type === AST_NODE_TYPES.ObjectPattern) {
recursivePatternCapture(id, pattern => {
if (pattern.type === AST_NODE_TYPES.Identifier) {
cb(pattern.name, false)
}
})
} else if (id.type === AST_NODE_TYPES.ArrayPattern) {
for (const el of id.elements) {
if (el?.type === AST_NODE_TYPES.Identifier) {
cb(el.name, false)
}
}
} else {
cb(id.name, false)
}
}
}
}
}
/**
* List of imports per file.
*
* Represented by a two-level Map to a Set of identifiers. The upper-level Map
* keys are the paths to the modules containing the imports, while the
* lower-level Map keys are the paths to the files which are being imported
* from. Lastly, the Set of identifiers contains either names being imported
* or a special AST node name listed above (e.g ImportDefaultSpecifier).
*
* For example, if we have a file named foo.js containing:
*
* import { o2 } from './bar.js';
*
* Then we will have a structure that looks like:
*
* Map { 'foo.js' => Map { 'bar.js' => Set { 'o2' } } }
*
*/
const importList = new Map<string, Map<string, Set<string>>>()
/**
* List of exports per file.
*
* Represented by a two-level Map to an object of metadata. The upper-level Map
* keys are the paths to the modules containing the exports, while the
* lower-level Map keys are the specific identifiers or special AST node names
* being exported. The leaf-level metadata object at the moment only contains a
* `whereUsed` property, which contains a Set of paths to modules that import
* the name.
*
* For example, if we have a file named bar.js containing the following exports:
*
* const o2 = 'bar';
* export { o2 };
*
* And a file named foo.js containing the following import:
*
* import { o2 } from './bar.js';
*
* Then we will have a structure that looks like:
*
* Map { 'bar.js' => Map { 'o2' => { whereUsed: Set { 'foo.js' } } } }
*/
const exportList = new Map<string, Map<string, { whereUsed: Set<string> }>>()
const visitorKeyMap = new Map()
const ignoredFiles = new Set()
const filesOutsideSrc = new Set()
const isNodeModule = (path: string) => /([/\\])(node_modules)\1/.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: string[],
ignoreExports: string[],
context: RuleContext,
) => {
const extensions = [...getFileExtensions(context.settings)]
const srcFileList = listFilesToProcess(src, extensions)
// prepare list of ignored files
const ignoredFilesList = listFilesToProcess(ignoreExports, extensions)
for (const { filename } of ignoredFilesList) ignoredFiles.add(filename)
// prepare list of source files, don't consider files from node_modules
return new Set(
srcFileList.flatMap(({ filename }) =>
isNodeModule(filename) ? [] : filename,
),
)
}
/**
* parse all source files and build up 2 maps containing the existing imports and exports
*/
const prepareImportsAndExports = (
srcFiles: Set<string>,
context: RuleContext,
) => {
const exportAll = new Map<string, Set<string>>()
for (const file of srcFiles) {
const exports = new Map<string, { whereUsed: Set<string> }>()
const imports = new Map<string, Set<string>>()
const currentExports = ExportMap.get(file, context)
if (currentExports) {
const {
dependencies,
reexports,
imports: localImportList,
namespace,
visitorKeys,
} = currentExports
visitorKeyMap.set(file, visitorKeys)
// dependencies === export * from
const currentExportAll = new Set<string>()
for (const getDependency of dependencies) {
const dependency = getDependency()
if (dependency === null) {
continue
}
currentExportAll.add(dependency.path)
}
exportAll.set(file, currentExportAll)
for (const [key, value] of reexports.entries()) {
if (key === DEFAULT) {
exports.set(AST_NODE_TYPES.ImportDefaultSpecifier, {
whereUsed: new Set(),
})
} else {
exports.set(key, { whereUsed: new Set() })
}
const reexport = value.getImport()
if (!reexport) {
continue
}
let localImport = imports.get(reexport.path)
const currentValue =
value.local === DEFAULT
? AST_NODE_TYPES.ImportDefaultSpecifier
: value.local
localImport =
localImport === undefined
? new Set([currentValue])
: new Set([...localImport, currentValue])
imports.set(reexport.path, localImport)
}
for (const [key, value] of localImportList.entries()) {
if (isNodeModule(key)) {
continue
}
const localImport = imports.get(key) || new Set()
for (const { importedSpecifiers } of value.declarations) {
for (const specifier of importedSpecifiers!) {
localImport.add(specifier)
}
}
imports.set(key, localImport)
}
importList.set(file, imports)
// build up export list only, if file is not ignored
if (ignoredFiles.has(file)) {
continue
}
for (const [key, _value] of namespace.entries()) {
if (key === DEFAULT) {
exports.set(AST_NODE_TYPES.ImportDefaultSpecifier, {
whereUsed: new Set(),
})
} else {
exports.set(key, { whereUsed: new Set() })
}
}
}
exports.set(AST_NODE_TYPES.ExportAllDeclaration, {
whereUsed: new Set(),
})
exports.set(AST_NODE_TYPES.ImportNamespaceSpecifier, {
whereUsed: new Set(),
})
exportList.set(file, exports)
}
for (const [key, value] of exportAll.entries()) {
for (const val of value) {
const currentExports = exportList.get(val)
if (currentExports) {
const currentExport = currentExports.get(
AST_NODE_TYPES.ExportAllDeclaration,
)
currentExport!.whereUsed.add(key)
}
}
}
}
/**
* traverse through all imports and add the respective path to the whereUsed-list
* of the corresponding export
*/
const determineUsage = () => {
for (const [listKey, listValue] of importList.entries()) {
for (const [key, value] of listValue.entries()) {
const exports = exportList.get(key)
if (exports !== undefined) {
for (const currentImport of value) {
let specifier: string
if (currentImport === AST_NODE_TYPES.ImportNamespaceSpecifier) {
specifier = AST_NODE_TYPES.ImportNamespaceSpecifier
} else if (currentImport === AST_NODE_TYPES.ImportDefaultSpecifier) {
specifier = AST_NODE_TYPES.ImportDefaultSpecifier
} else {
specifier = currentImport
}
if (specifier !== undefined) {
const exportStatement = exports.get(specifier)
if (exportStatement !== undefined) {
const { whereUsed } = exportStatement
whereUsed.add(listKey)
exports.set(specifier, { whereUsed })
}
}
}
}
}
}
}
/**
* prepare the lists of existing imports and exports - should only be executed once at
* the start of a new eslint run
*/
let srcFiles: Set<string>
let lastPrepareKey: string
const doPreparation = (
src: string[],
ignoreExports: string[],
context: RuleContext,
) => {
const prepareKey = JSON.stringify({
src: src.sort(),
ignoreExports: (ignoreExports || []).sort(),
extensions: [...getFileExtensions(context.settings)].sort(),
})
if (prepareKey === lastPrepareKey) {
return
}
importList.clear()
exportList.clear()
ignoredFiles.clear()
filesOutsideSrc.clear()
srcFiles = resolveFiles(src, ignoreExports, context)
prepareImportsAndExports(srcFiles, context)
determineUsage()
lastPrepareKey = prepareKey
}
const newNamespaceImportExists = (specifiers: TSESTree.Node[]) =>
specifiers.some(
({ type }) => type === AST_NODE_TYPES.ImportNamespaceSpecifier,
)
const newDefaultImportExists = (specifiers: TSESTree.Node[]) =>
specifiers.some(({ type }) => type === AST_NODE_TYPES.ImportDefaultSpecifier)
const fileIsInPkg = (file: string) => {
const { pkg, path: pkgPath } = readPkgUp({ cwd: file })
const basePath = path.dirname(pkgPath!)
const checkPkgFieldString = (pkgField: string) => {
if (path.join(basePath, pkgField) === file) {
return true
}
}
const checkPkgFieldObject = (
pkgField: string | Partial<Record<string, string | false>>,
) => {
const pkgFieldFiles = Object.values(pkgField).flatMap(value =>
typeof value === 'boolean' ? [] : path.join(basePath, value!),
)
if (pkgFieldFiles.includes(file)) {
return true
}
}
const checkPkgField = (
pkgField: string | Partial<Record<string, string | false>>,
) => {
if (typeof pkgField === 'string') {
return checkPkgFieldString(pkgField)
}
if (typeof pkgField === 'object') {
return checkPkgFieldObject(pkgField)
}
}
if (!pkg) {
return false
}
if (pkg.private === true) {
return false
}
if (pkg.bin && checkPkgField(pkg.bin)) {
return true
}
if (pkg.browser && checkPkgField(pkg.browser)) {
return true
}
if (pkg.main && checkPkgFieldString(pkg.main)) {
return true
}
return false
}
type Options = {
src?: string[]
ignoreExports?: string[]
missingExports?: true
unusedExports?: boolean
ignoreUnusedTypeExports?: boolean
}
type MessageId = 'notFound' | 'unused'
export = createRule<Options[], MessageId>({
name: 'no-unused-modules',
meta: {
type: 'suggestion',
docs: {
category: 'Helpful warnings',
description:
'Forbid modules without exports, or exports without matching import in another module.',
},
schema: [
{
type: 'object',
properties: {
src: {
description: 'files/paths to be analyzed (only for unused exports)',
type: 'array',
uniqueItems: true,
items: {
type: 'string',
minLength: 1,
},
},
ignoreExports: {
description:
'files/paths for which unused exports will not be reported (e.g module entry points)',
type: 'array',
uniqueItems: true,
items: {
type: 'string',
minLength: 1,
},
},
missingExports: {
description: 'report modules without any exports',
type: 'boolean',
},
unusedExports: {
description: 'report exports without any usage',
type: 'boolean',
},
ignoreUnusedTypeExports: {
description: 'ignore type exports without any usage',
type: 'boolean',
},
},
anyOf: [
{
type: 'object',
properties: {
unusedExports: {
type: 'boolean',
enum: [true],
},
src: {
type: 'array',
minItems: 1,
},
},
required: ['unusedExports'],
},
{
type: 'object',
properties: {
missingExports: {
type: 'boolean',
enum: [true],
},
},
required: ['missingExports'],
},
],
},
],
messages: {
notFound: 'No exports found',
unused: "exported declaration '{{value}}' not used within other modules",
},
},
defaultOptions: [],
create(context) {
const {
src = [process.cwd()],
ignoreExports = [],
missingExports,
unusedExports,
ignoreUnusedTypeExports,
} = context.options[0] || {}
if (unusedExports) {
doPreparation(src, ignoreExports, context)
}
const filename = context.physicalFilename
const checkExportPresence = (node: TSESTree.Program) => {
if (!missingExports) {
return
}
if (ignoreUnusedTypeExports) {
return
}
if (ignoredFiles.has(filename)) {
return
}
const exportCount = exportList.get(filename)!
const exportAll = exportCount.get(AST_NODE_TYPES.ExportAllDeclaration)!
const namespaceImports = exportCount.get(
AST_NODE_TYPES.ImportNamespaceSpecifier,
)!
exportCount.delete(AST_NODE_TYPES.ExportAllDeclaration)
exportCount.delete(AST_NODE_TYPES.ImportNamespaceSpecifier)
if (exportCount.size === 0) {
// node.body[0] === 'undefined' only happens, if everything is commented out in the file
// being linted
context.report({
node: node.body[0] ?? node,
messageId: 'notFound',
})
}
exportCount.set(AST_NODE_TYPES.ExportAllDeclaration, exportAll)
exportCount.set(AST_NODE_TYPES.ImportNamespaceSpecifier, namespaceImports)
}
const checkUsage = (
node: TSESTree.Node,
exportedValue: string,
isTypeExport: boolean,
) => {
if (!unusedExports) {
return
}
if (isTypeExport && ignoreUnusedTypeExports) {
return
}
if (ignoredFiles.has(filename)) {
return
}
if (fileIsInPkg(filename)) {
return
}
if (filesOutsideSrc.has(filename)) {
return
}
// make sure file to be linted is included in source files
if (!srcFiles.has(filename)) {
srcFiles = resolveFiles(src, ignoreExports, context)
if (!srcFiles.has(filename)) {
filesOutsideSrc.add(filename)
return
}
}
const exports = exportList.get(filename)
if (!exports) {
console.error(
`file \`${filename}\` has no exports. Please update to the latest, and if it still happens, report this on https://github.com/import-js/eslint-plugin-import/issues/2866!`,
)
return
}
// special case: export * from
const exportAll = exports.get(AST_NODE_TYPES.ExportAllDeclaration)
if (
exportAll !== undefined &&
exportedValue !== AST_NODE_TYPES.ImportDefaultSpecifier &&
exportAll.whereUsed.size > 0
) {
return
}
// special case: namespace import
const namespaceImports = exports.get(
AST_NODE_TYPES.ImportNamespaceSpecifier,
)
if (
namespaceImports !== undefined &&
namespaceImports.whereUsed.size > 0
) {
return
}
// exportsList will always map any imported value of 'default' to 'ImportDefaultSpecifier'
const exportsKey =
exportedValue === DEFAULT
? AST_NODE_TYPES.ImportDefaultSpecifier
: exportedValue
const exportStatement = exports.get(exportsKey)
const value =
exportsKey === AST_NODE_TYPES.ImportDefaultSpecifier
? DEFAULT
: exportsKey
if (exportStatement === undefined) {
context.report({
node,
messageId: 'unused',
data: {
value,
},
})
} else {
if (exportStatement.whereUsed.size === 0) {
context.report({
node,
messageId: 'unused',
data: {
value,
},
})
}
}
}
/**
* only useful for tools like vscode-eslint
*
* update lists of existing exports during runtime
*/
const updateExportUsage = (node: TSESTree.Program) => {
if (ignoredFiles.has(filename)) {
return
}
// new module has been created during runtime
// include it in further processing
const exports =
exportList.get(filename) ??
new Map<string, { whereUsed: Set<string> }>()
const newExports = new Map<string, { whereUsed: Set<string> }>()
const newExportIdentifiers = new Set<string>()
for (const s of node.body) {
if (s.type === AST_NODE_TYPES.ExportDefaultDeclaration) {
newExportIdentifiers.add(AST_NODE_TYPES.ImportDefaultSpecifier)
}
if (s.type === AST_NODE_TYPES.ExportNamedDeclaration) {
if (s.specifiers.length > 0) {
for (const specifier of s.specifiers) {
if (specifier.exported) {
newExportIdentifiers.add(getValue(specifier.exported))
}
}
}
forEachDeclarationIdentifier(s.declaration!, name => {
newExportIdentifiers.add(name)
})
}
}
// old exports exist within list of new exports identifiers: add to map of new exports
for (const [key, value] of exports.entries()) {
if (newExportIdentifiers.has(key)) {
newExports.set(key, value)
}
}
// new export identifiers added: add to map of new exports
for (const key of newExportIdentifiers) {
if (!exports.has(key)) {
newExports.set(key, { whereUsed: new Set() })
}
}
// preserve information about namespace imports
const exportAll = exports.get(AST_NODE_TYPES.ExportAllDeclaration)!
const namespaceImports = exports.get(
AST_NODE_TYPES.ImportNamespaceSpecifier,
) ?? { whereUsed: new Set() }
newExports.set(AST_NODE_TYPES.ExportAllDeclaration, exportAll)
newExports.set(AST_NODE_TYPES.ImportNamespaceSpecifier, namespaceImports)
exportList.set(filename, newExports)
}
/**
* only useful for tools like vscode-eslint
*
* update lists of existing imports during runtime
*/
const updateImportUsage = (node: TSESTree.Program) => {
if (!unusedExports) {
return
}
const oldImportPaths =
importList.get(filename) ?? new Map<string, Set<string>>()
const oldNamespaceImports = new Set<string>()
const newNamespaceImports = new Set<string>()
const oldExportAll = new Set<string>()
const newExportAll = new Set<string>()
const oldDefaultImports = new Set<string>()
const newDefaultImports = new Set<string>()
const oldImports = new Map()
const newImports = new Map()
for (const [key, value] of oldImportPaths.entries()) {
if (value.has(AST_NODE_TYPES.ExportAllDeclaration)) {
oldExportAll.add(key)
}
if (value.has(AST_NODE_TYPES.ImportNamespaceSpecifier)) {
oldNamespaceImports.add(key)
}
if (value.has(AST_NODE_TYPES.ImportDefaultSpecifier)) {
oldDefaultImports.add(key)
}
for (const val of value) {
if (
val !== AST_NODE_TYPES.ImportNamespaceSpecifier &&
val !== AST_NODE_TYPES.ImportDefaultSpecifier
) {
oldImports.set(val, key)
}
}
}
function processDynamicImport(source: TSESTree.Node) {
if (source.type !== 'Literal' || typeof source.value !== 'string') {
return null
}
const p = resolve(source.value, context)
if (p == null) {
return null
}
newNamespaceImports.add(p)
}
visit(node, visitorKeyMap.get(filename), {
ImportExpression(child) {
processDynamicImport((child as TSESTree.ImportExpression).source)
},
CallExpression(child_) {
const child = child_ as TSESTree.CallExpression
// @ts-expect-error - legacy parser type
if (child.callee.type === 'Import') {
processDynamicImport(child.arguments[0])
}
},
})
for (const astNode of node.body) {
let resolvedPath: string | null | undefined
// support for export { value } from 'module'
if (
astNode.type === AST_NODE_TYPES.ExportNamedDeclaration &&
astNode.source
) {
resolvedPath = resolve(
astNode.source.raw.replaceAll(/('|")/g, ''),
context,
)
for (const specifier of astNode.specifiers) {
const name = getValue(specifier.local)
if (name === DEFAULT) {
newDefaultImports.add(resolvedPath!)
} else {
newImports.set(name, resolvedPath)
}
}
}
if (astNode.type === AST_NODE_TYPES.ExportAllDeclaration) {
resolvedPath = resolve(
astNode.source.raw.replaceAll(/('|")/g, ''),
context,
)
newExportAll.add(resolvedPath!)
}
if (astNode.type === AST_NODE_TYPES.ImportDeclaration) {
resolvedPath = resolve(
astNode.source.raw.replaceAll(/('|")/g, ''),
context,
)
if (!resolvedPath) {
continue
}
if (isNodeModule(resolvedPath)) {
continue
}
if (newNamespaceImportExists(astNode.specifiers)) {
newNamespaceImports.add(resolvedPath)
}
if (newDefaultImportExists(astNode.specifiers)) {
newDefaultImports.add(resolvedPath)
}
for (const specifier of astNode.specifiers.filter(
specifier =>
specifier.type !== AST_NODE_TYPES.ImportDefaultSpecifier &&
specifier.type !== AST_NODE_TYPES.ImportNamespaceSpecifier,
)) {
if ('imported' in specifier) {
newImports.set(getValue(specifier.imported), resolvedPath)
}
}
}
}
for (const value of newExportAll) {
if (!oldExportAll.has(value)) {
const imports = oldImportPaths.get(value) ?? new Set()
imports.add(AST_NODE_TYPES.ExportAllDeclaration)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport: { whereUsed: Set<string> } | undefined
if (exports === undefined) {
exports = new Map()
exportList.set(value, exports)
} else {
currentExport = exports.get(AST_NODE_TYPES.ExportAllDeclaration)
}
if (currentExport === undefined) {
const whereUsed = new Set<string>()
whereUsed.add(filename)
exports.set(AST_NODE_TYPES.ExportAllDeclaration, {
whereUsed,
})
} else {
currentExport.whereUsed.add(filename)
}
}
}
for (const value of oldExportAll) {
if (!newExportAll.has(value)) {
const imports = oldImportPaths.get(value)!
imports.delete(AST_NODE_TYPES.ExportAllDeclaration)
const exports = exportList.get(value)
if (exports !== undefined) {
const currentExport = exports.get(
AST_NODE_TYPES.ExportAllDeclaration,
)
if (currentExport !== undefined) {
currentExport.whereUsed.delete(filename)
}
}
}
}
for (const value of newDefaultImports) {
if (!oldDefaultImports.has(value)) {
let imports = oldImportPaths.get(value)
if (imports === undefined) {
imports = new Set()
}
imports.add(AST_NODE_TYPES.ImportDefaultSpecifier)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (exports === undefined) {
exports = new Map()
exportList.set(value, exports)
} else {
currentExport = exports.get(AST_NODE_TYPES.ImportDefaultSpecifier)
}
if (currentExport === undefined) {
const whereUsed = new Set<string>()
whereUsed.add(filename)
exports.set(AST_NODE_TYPES.ImportDefaultSpecifier, {
whereUsed,
})
} else {
currentExport.whereUsed.add(filename)
}
}
}
for (const value of oldDefaultImports) {
if (!newDefaultImports.has(value)) {
const imports = oldImportPaths.get(value)!
imports.delete(AST_NODE_TYPES.ImportDefaultSpecifier)
const exports = exportList.get(value)
if (exports !== undefined) {
const currentExport = exports.get(
AST_NODE_TYPES.ImportDefaultSpecifier,
)
if (currentExport !== undefined) {
currentExport.whereUsed.delete(filename)
}
}
}
}
for (const value of newNamespaceImports) {
if (!oldNamespaceImports.has(value)) {
let imports = oldImportPaths.get(value)
if (imports === undefined) {
imports = new Set()
}
imports.add(AST_NODE_TYPES.ImportNamespaceSpecifier)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (exports === undefined) {
exports = new Map()
exportList.set(value, exports)
} else {
currentExport = exports.get(AST_NODE_TYPES.ImportNamespaceSpecifier)
}
if (currentExport === undefined) {
const whereUsed = new Set<string>()
whereUsed.add(filename)
exports.set(AST_NODE_TYPES.ImportNamespaceSpecifier, {
whereUsed,
})
} else {
currentExport.whereUsed.add(filename)
}
}
}
for (const value of oldNamespaceImports) {
if (!newNamespaceImports.has(value)) {
const imports = oldImportPaths.get(value)!
imports.delete(AST_NODE_TYPES.ImportNamespaceSpecifier)
const exports = exportList.get(value)
if (exports !== undefined) {
const currentExport = exports.get(
AST_NODE_TYPES.ImportNamespaceSpecifier,
)
if (currentExport !== undefined) {
currentExport.whereUsed.delete(filename)
}
}
}
}
for (const [key, value] of newImports.entries()) {
if (!oldImports.has(key)) {
let imports = oldImportPaths.get(value)
if (imports === undefined) {
imports = new Set()
}
imports.add(key)
oldImportPaths.set(value, imports)
let exports = exportList.get(value)
let currentExport
if (exports === undefined) {
exports = new Map()
exportList.set(value, exports)
} else {
currentExport = exports.get(key)
}
if (currentExport === undefined) {
const whereUsed = new Set<string>()
whereUsed.add(filename)
exports.set(key, { whereUsed })
} else {
currentExport.whereUsed.add(filename)
}
}
}
for (const [key, value] of oldImports.entries()) {
if (!newImports.has(key)) {
const imports = oldImportPaths.get(value)!
imports.delete(key)
const exports = exportList.get(value)
if (exports !== undefined) {
const currentExport = exports.get(key)
if (currentExport !== undefined) {
currentExport.whereUsed.delete(filename)
}
}
}
}
}