1- import ts , { CodeFixAction , InferencePriority , ScriptElementKind } from 'typescript/lib/tsserverlibrary' ;
1+ import ts , { CodeFixAction , ScriptElementKind } from 'typescript/lib/tsserverlibrary' ;
22import * as path from 'path' ;
33
4- type PluginOptions = {
4+ export type PluginOptions = {
55 paths : readonly string [ ] ;
6+ ignoreNamedExport ?: boolean ;
67} ;
78
89export 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 ) ;
1011
11- return filePaths . map ( ( filePath ) => {
12- const name = getFileNameWithoutExt ( filePath ) ;
12+ return modulePaths . map ( ( modulePath ) => {
13+ const name = getFileNameWithoutExt ( modulePath ) ;
1314 return {
1415 name : name ,
1516 kind : ts . ScriptElementKind . alias ,
16- source : filePath ,
17+ source : modulePath ,
1718 sortText : name ,
1819 hasAction : true ,
1920 isImportStatementCompletion : true ,
2021 data : {
2122 exportName : name ,
22- modulePath : filePath ,
23+ modulePath : modulePath ,
2324 } ,
2425 } ;
2526 } ) ;
2627}
2728
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+
2845export function getCompletionEntryDetails (
2946 name : string ,
3047 selfPath : string ,
@@ -52,16 +69,16 @@ export function getCodeFixActionByName(
5269 return null ;
5370 }
5471
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 ) ;
5774 if ( modulePath ) {
5875 return getCodeFixActionFromPath ( name , selfPath , modulePath , info . project ) ;
5976 } else {
6077 return null ;
6178 }
6279}
6380
64- function getPathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
81+ function getModulePathsToImport ( options : PluginOptions , project : ts . server . Project ) : string [ ] {
6582 const currentDir = project . getCurrentDirectory ( ) ;
6683
6784 return options . paths . flatMap ( ( dirPath ) => {
@@ -79,23 +96,27 @@ function getFilePathWithoutExt(filePath: string): string {
7996 return filePath . slice ( 0 , filePath . length - ext . length ) ;
8097}
8198
82- function transformModulePath ( selfPath : string , filePath : string , project : ts . server . Project ) {
99+ function getModuleSpceifier ( selfPath : string , modulePath : string , project : ts . server . Project ) {
83100 const compilerOptions = project . getCompilerOptions ( ) ;
101+
102+ let specifier : string ;
84103 if ( compilerOptions . baseUrl ) {
85- return path . relative ( compilerOptions . baseUrl , filePath ) ;
104+ specifier = path . relative ( compilerOptions . baseUrl , modulePath ) ;
86105 } else {
87- return './' + path . relative ( path . dirname ( selfPath ) , filePath ) ;
106+ specifier = './' + path . relative ( path . dirname ( selfPath ) , modulePath ) ;
88107 }
108+
109+ return getFilePathWithoutExt ( specifier ) ;
89110}
90111
91- export function getCodeFixActionFromPath (
112+ function getCodeFixActionFromPath (
92113 name : string ,
93114 selfPath : string ,
94115 modulePath : string ,
95116 project : ts . server . Project ,
96117) : 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` ;
99120 return {
100121 fixName : 'namespace-import' ,
101122 description : text ,
0 commit comments