@@ -7,20 +7,20 @@ export type PluginOptions = {
7
7
} ;
8
8
9
9
export function getCompletionEntries ( info : ts . server . PluginCreateInfo ) : ts . CompletionEntry [ ] {
10
- const filePaths = getPathsToImport ( info . config . options , info . project ) ;
10
+ const modulePaths = getModulePathsToImport ( info . config . options , info . project ) ;
11
11
12
- return filePaths . map ( ( filePath ) => {
13
- const name = getFileNameWithoutExt ( filePath ) ;
12
+ return modulePaths . map ( ( modulePath ) => {
13
+ const name = getFileNameWithoutExt ( modulePath ) ;
14
14
return {
15
15
name : name ,
16
16
kind : ts . ScriptElementKind . alias ,
17
- source : filePath ,
17
+ source : modulePath ,
18
18
sortText : name ,
19
19
hasAction : true ,
20
20
isImportStatementCompletion : true ,
21
21
data : {
22
22
exportName : name ,
23
- modulePath : filePath ,
23
+ modulePath : modulePath ,
24
24
} ,
25
25
} ;
26
26
} ) ;
@@ -69,16 +69,16 @@ export function getCodeFixActionByName(
69
69
return null ;
70
70
}
71
71
72
- const filePaths = getPathsToImport ( info . config . options , info . project ) ;
73
- 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 ) ;
74
74
if ( modulePath ) {
75
75
return getCodeFixActionFromPath ( name , selfPath , modulePath , info . project ) ;
76
76
} else {
77
77
return null ;
78
78
}
79
79
}
80
80
81
- function getPathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
81
+ function getModulePathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
82
82
const currentDir = project . getCurrentDirectory ( ) ;
83
83
84
84
return options . paths . flatMap ( ( dirPath ) => {
@@ -96,13 +96,17 @@ function getFilePathWithoutExt(filePath: string): string {
96
96
return filePath . slice ( 0 , filePath . length - ext . length ) ;
97
97
}
98
98
99
- function transformModulePath ( selfPath : string , filePath : string , project : ts . server . Project ) {
99
+ function getModuleSpceifier ( selfPath : string , modulePath : string , project : ts . server . Project ) {
100
100
const compilerOptions = project . getCompilerOptions ( ) ;
101
+
102
+ let specifier : string ;
101
103
if ( compilerOptions . baseUrl ) {
102
- return path . relative ( compilerOptions . baseUrl , filePath ) ;
104
+ specifier = path . relative ( compilerOptions . baseUrl , modulePath ) ;
103
105
} else {
104
- return './' + path . relative ( path . dirname ( selfPath ) , filePath ) ;
106
+ specifier = './' + path . relative ( path . dirname ( selfPath ) , modulePath ) ;
105
107
}
108
+
109
+ return getFilePathWithoutExt ( specifier ) ;
106
110
}
107
111
108
112
function getCodeFixActionFromPath (
@@ -111,8 +115,8 @@ function getCodeFixActionFromPath(
111
115
modulePath : string ,
112
116
project : ts . server . Project ,
113
117
) : CodeFixAction {
114
- const importPath = transformModulePath ( selfPath , modulePath , project ) ;
115
- 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` ;
116
120
return {
117
121
fixName : 'namespace-import' ,
118
122
description : text ,
0 commit comments