Skip to content

Commit 79b81f1

Browse files
committed
feat: removeImportsFromReferences (enabled by default!)
1 parent 9be3e41 commit 79b81f1

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.MD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ usersList.map // -> usersList.map((user) => )
7070

7171
<https://github.com/microsoft/vscode/issues/160637>
7272

73+
### Remove Imports From References
74+
75+
(*enabled by default*)
76+
77+
Removes import statements from references when symbol has usages in the same file. Why? Because if export thing is used in another file, it might be obvious that it is imported, and most probably you are not interested in import statements.
78+
7379
## Minor Useful Features
7480

7581
### `enablePlugin` setting

src/configurationType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ export type Configuration = {
203203
* @default true
204204
*/
205205
removeDefinitionFromReferences: boolean
206+
/**
207+
* @default true
208+
*/
209+
removeImportsFromReferences: boolean
206210
/**
207211
* Small definition improvements by cleaning them out:
208212
* - remove node_modules definition on React.FC component click

typescript/src/references.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type tslib from 'typescript/lib/tsserverlibrary'
21
import { GetConfig } from './types'
2+
import { findChildContainingPositionMaxDepth, approveCast } from './utils'
33

44
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig) => {
55
proxy.findReferences = (fileName, position) => {
@@ -11,6 +11,43 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
1111
references: references.filter(({ isDefinition }) => !isDefinition),
1212
}))
1313
}
14+
if (c('removeImportsFromReferences')) {
15+
const program = languageService.getProgram()!
16+
const importsCountPerFileName: Record<
17+
string,
18+
{
19+
all: number
20+
cur: number
21+
}
22+
> = {}
23+
const allReferences = prior.flatMap(({ references }) => references)
24+
allReferences.forEach(({ fileName }) => {
25+
importsCountPerFileName[fileName] ??= {
26+
all: 0,
27+
cur: 0,
28+
}
29+
importsCountPerFileName[fileName]!.all++
30+
})
31+
prior = prior.map(({ references, ...other }) => {
32+
return {
33+
...other,
34+
references: references.filter(({ fileName, textSpan }) => {
35+
const importsCount = importsCountPerFileName[fileName]!
36+
// doesn't make sense to handle case where it gets imports twice
37+
if (importsCount.all <= 1 || importsCount.cur !== 0) return true
38+
importsCount.cur++
39+
const sourceFile = program.getSourceFile(fileName)
40+
if (!sourceFile) return true
41+
const end = textSpan.start + textSpan.length
42+
let node = findChildContainingPositionMaxDepth(sourceFile, end, 6)
43+
if (!node) return true
44+
if (ts.isIdentifier(node)) node = node.parent
45+
if (approveCast(node, ts.isNamedImports, ts.isImportSpecifier, ts.isImportClause, ts.isImportEqualsDeclaration)) return false
46+
return true
47+
}),
48+
}
49+
})
50+
}
1451
return prior
1552
}
1653
}

0 commit comments

Comments
 (0)