1
- import ts , { CodeFixAction , InferencePriority , ScriptElementKind } from 'typescript/lib/tsserverlibrary' ;
1
+ import ts , { CodeFixAction , ScriptElementKind } from 'typescript/lib/tsserverlibrary' ;
2
2
import * as path from 'path' ;
3
3
4
- type PluginOptions = {
4
+ export type PluginOptions = {
5
5
paths : readonly string [ ] ;
6
+ ignoreNamedExport ?: boolean ;
6
7
} ;
7
8
8
9
export function getCompletionEntries ( info : ts . server . PluginCreateInfo ) : ts . CompletionEntry [ ] {
9
- const filePaths = getPathsToImport ( info . config . options , info . project ) ;
10
+ const modulePaths = getModulePathsToImport ( info . config . options , info . project ) ;
10
11
11
- return filePaths . map ( ( filePath ) => {
12
- const name = getFileNameWithoutExt ( filePath ) ;
12
+ return modulePaths . map ( ( modulePath ) => {
13
+ const name = getFileNameWithoutExt ( modulePath ) ;
13
14
return {
14
15
name : name ,
15
16
kind : ts . ScriptElementKind . alias ,
16
- source : filePath ,
17
+ source : modulePath ,
17
18
sortText : name ,
18
19
hasAction : true ,
19
20
isImportStatementCompletion : true ,
20
21
data : {
21
22
exportName : name ,
22
- modulePath : filePath ,
23
+ modulePath : modulePath ,
23
24
} ,
24
25
} ;
25
26
} ) ;
26
27
}
27
28
29
+ export function filterNamedImportEntries (
30
+ entries : ts . CompletionEntry [ ] ,
31
+ info : ts . server . PluginCreateInfo ,
32
+ ) : ts . CompletionEntry [ ] {
33
+ const options : PluginOptions = info . config . options ;
34
+ if ( ! options . ignoreNamedExport ) {
35
+ return entries ;
36
+ }
37
+
38
+ const currentDir = info . project . getCurrentDirectory ( ) ;
39
+ const dirPaths = options . paths . map ( ( dirPath ) => path . resolve ( currentDir , dirPath ) ) ;
40
+ return entries . filter ( ( entry ) => {
41
+ return ! dirPaths . some ( ( dirPath ) => entry . data ?. exportName && entry . data . fileName ?. startsWith ( dirPath ) ) ;
42
+ } ) ;
43
+ }
44
+
28
45
export function getCompletionEntryDetails (
29
46
name : string ,
30
47
selfPath : string ,
@@ -52,16 +69,16 @@ export function getCodeFixActionByName(
52
69
return null ;
53
70
}
54
71
55
- const filePaths = getPathsToImport ( info . config . options , info . project ) ;
56
- const modulePath = filePaths . find ( ( filePath ) => getFileNameWithoutExt ( filePath ) === name ) ;
72
+ const modulePaths = getModulePathsToImport ( info . config . options , info . project ) ;
73
+ const modulePath = modulePaths . find ( ( filePath ) => getFileNameWithoutExt ( filePath ) === name ) ;
57
74
if ( modulePath ) {
58
75
return getCodeFixActionFromPath ( name , selfPath , modulePath , info . project ) ;
59
76
} else {
60
77
return null ;
61
78
}
62
79
}
63
80
64
- function getPathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
81
+ function getModulePathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
65
82
const currentDir = project . getCurrentDirectory ( ) ;
66
83
67
84
return options . paths . flatMap ( ( dirPath ) => {
@@ -79,23 +96,27 @@ function getFilePathWithoutExt(filePath: string): string {
79
96
return filePath . slice ( 0 , filePath . length - ext . length ) ;
80
97
}
81
98
82
- function transformModulePath ( selfPath : string , filePath : string , project : ts . server . Project ) {
99
+ function getModuleSpceifier ( selfPath : string , modulePath : string , project : ts . server . Project ) {
83
100
const compilerOptions = project . getCompilerOptions ( ) ;
101
+
102
+ let specifier : string ;
84
103
if ( compilerOptions . baseUrl ) {
85
- return path . relative ( compilerOptions . baseUrl , filePath ) ;
104
+ specifier = path . relative ( compilerOptions . baseUrl , modulePath ) ;
86
105
} else {
87
- return './' + path . relative ( path . dirname ( selfPath ) , filePath ) ;
106
+ specifier = './' + path . relative ( path . dirname ( selfPath ) , modulePath ) ;
88
107
}
108
+
109
+ return getFilePathWithoutExt ( specifier ) ;
89
110
}
90
111
91
- export function getCodeFixActionFromPath (
112
+ function getCodeFixActionFromPath (
92
113
name : string ,
93
114
selfPath : string ,
94
115
modulePath : string ,
95
116
project : ts . server . Project ,
96
117
) : CodeFixAction {
97
- const importPath = transformModulePath ( selfPath , modulePath , project ) ;
98
- const text = `import * as ${ name } from "${ getFilePathWithoutExt ( importPath ) } ";\n` ;
118
+ const moduleSpecifier = getModuleSpceifier ( selfPath , modulePath , project ) ;
119
+ const text = `import * as ${ name } from "${ moduleSpecifier } ";\n` ;
99
120
return {
100
121
fixName : 'namespace-import' ,
101
122
description : text ,
0 commit comments