@@ -14,12 +14,12 @@ interface Cache {
1414let gopkgsNotified : boolean = false ;
1515let cacheTimeout : number = 5000 ;
1616
17- let gopkgsSubscriptions : Map < string , GopkgsDone [ ] > = new Map < string , GopkgsDone [ ] > ( ) ;
18- let gopkgsRunning : Set < string > = new Set < string > ( ) ;
17+ const gopkgsSubscriptions : Map < string , GopkgsDone [ ] > = new Map < string , GopkgsDone [ ] > ( ) ;
18+ const gopkgsRunning : Set < string > = new Set < string > ( ) ;
1919
20- let allPkgsCache : Map < string , Cache > = new Map < string , Cache > ( ) ;
20+ const allPkgsCache : Map < string , Cache > = new Map < string , Cache > ( ) ;
2121
22- let pkgRootDirs = new Map < string , string > ( ) ;
22+ const pkgRootDirs = new Map < string , string > ( ) ;
2323
2424function gopkgs ( workDir ?: string ) : Promise < Map < string , string > > {
2525 const gopkgsBinPath = getBinPath ( 'gopkgs' ) ;
@@ -81,7 +81,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
8181 pkgs . set ( pkgPath , pkgName ) ;
8282 } ) ;
8383
84- let timeTaken = Date . now ( ) - t0 ;
84+ const timeTaken = Date . now ( ) - t0 ;
8585 /* __GDPR__
8686 "gopkgs" : {
8787 "tool" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
@@ -127,28 +127,26 @@ function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> {
127127 * @argument workDir. The workspace directory of the project.
128128 * @returns Map<string, string> mapping between package import path and package name
129129 */
130- export function getAllPackages ( workDir : string ) : Promise < Map < string , string > > {
131- let cache = allPkgsCache . get ( workDir ) ;
132- let useCache = cache && ( new Date ( ) . getTime ( ) - cache . lastHit ) < cacheTimeout ;
130+ export async function getAllPackages ( workDir : string ) : Promise < Map < string , string > > {
131+ const cache = allPkgsCache . get ( workDir ) ;
132+ const useCache = cache && ( new Date ( ) . getTime ( ) - cache . lastHit ) < cacheTimeout ;
133133 if ( useCache ) {
134134 cache . lastHit = new Date ( ) . getTime ( ) ;
135135 return Promise . resolve ( cache . entry ) ;
136136 }
137137
138- return getAllPackagesNoCache ( workDir ) . then ( ( pkgs ) => {
139- if ( ! pkgs || pkgs . size === 0 ) {
140- if ( ! gopkgsNotified ) {
141- vscode . window . showInformationMessage ( 'Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.' ) ;
142- gopkgsNotified = true ;
143- }
138+ const pkgs = await getAllPackagesNoCache ( workDir ) ;
139+ if ( ! pkgs || pkgs . size === 0 ) {
140+ if ( ! gopkgsNotified ) {
141+ vscode . window . showInformationMessage ( 'Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.' ) ;
142+ gopkgsNotified = true ;
144143 }
145-
146- allPkgsCache . set ( workDir , {
147- entry : pkgs ,
148- lastHit : new Date ( ) . getTime ( )
149- } ) ;
150- return pkgs ;
144+ }
145+ allPkgsCache . set ( workDir , {
146+ entry : pkgs ,
147+ lastHit : new Date ( ) . getTime ( )
151148 } ) ;
149+ return pkgs ;
152150}
153151
154152/**
@@ -160,52 +158,48 @@ export function getAllPackages(workDir: string): Promise<Map<string, string>> {
160158 */
161159export function getImportablePackages ( filePath : string , useCache : boolean = false ) : Promise < Map < string , string > > {
162160 filePath = fixDriveCasingInWindows ( filePath ) ;
163- let getAllPackagesPromise : Promise < Map < string , string > > ;
164- let fileDirPath = path . dirname ( filePath ) ;
161+ const fileDirPath = path . dirname ( filePath ) ;
165162
166163 let foundPkgRootDir = pkgRootDirs . get ( fileDirPath ) ;
167- let workDir = foundPkgRootDir || fileDirPath ;
168- let cache = allPkgsCache . get ( workDir ) ;
164+ const workDir = foundPkgRootDir || fileDirPath ;
165+ const cache = allPkgsCache . get ( workDir ) ;
169166
170- if ( useCache && cache ) {
171- getAllPackagesPromise = Promise . race ( [ getAllPackages ( workDir ) , cache . entry ] ) ;
172- } else {
173- getAllPackagesPromise = getAllPackages ( workDir ) ;
174- }
167+ const getAllPackagesPromise : Promise < Map < string , string > > = useCache && cache
168+ ? Promise . race ( [ getAllPackages ( workDir ) , cache . entry ] )
169+ : getAllPackages ( workDir ) ;
175170
176171 return Promise . all ( [ isVendorSupported ( ) , getAllPackagesPromise ] ) . then ( ( [ vendorSupported , pkgs ] ) => {
177- let pkgMap = new Map < string , string > ( ) ;
172+ const pkgMap = new Map < string , string > ( ) ;
178173 if ( ! pkgs ) {
179174 return pkgMap ;
180175 }
181176
182- let currentWorkspace = getCurrentGoWorkspaceFromGOPATH ( getCurrentGoPath ( ) , fileDirPath ) ;
177+ const currentWorkspace = getCurrentGoWorkspaceFromGOPATH ( getCurrentGoPath ( ) , fileDirPath ) ;
183178 pkgs . forEach ( ( pkgName , pkgPath ) => {
184179 if ( pkgName === 'main' ) {
185180 return ;
186181 }
187182
188-
189183 if ( ! vendorSupported || ! currentWorkspace ) {
190184 pkgMap . set ( pkgPath , pkgName ) ;
191185 return ;
192186 }
193187
194188 if ( ! foundPkgRootDir ) {
195189 // try to guess package root dir
196- let vendorIndex = pkgPath . indexOf ( '/vendor/' ) ;
190+ const vendorIndex = pkgPath . indexOf ( '/vendor/' ) ;
197191 if ( vendorIndex !== - 1 ) {
198192 foundPkgRootDir = path . join ( currentWorkspace , pkgPath . substring ( 0 , vendorIndex ) . replace ( '/' , path . sep ) ) ;
199193 pkgRootDirs . set ( fileDirPath , foundPkgRootDir ) ;
200194 }
201195 }
202196
203- let relativePkgPath = getRelativePackagePath ( fileDirPath , currentWorkspace , pkgPath ) ;
197+ const relativePkgPath = getRelativePackagePath ( fileDirPath , currentWorkspace , pkgPath ) ;
204198 if ( ! relativePkgPath ) {
205199 return ;
206200 }
207201
208- let allowToImport = isAllowToImportPackage ( fileDirPath , currentWorkspace , relativePkgPath ) ;
202+ const allowToImport = isAllowToImportPackage ( fileDirPath , currentWorkspace , relativePkgPath ) ;
209203 if ( allowToImport ) {
210204 pkgMap . set ( relativePkgPath , pkgName ) ;
211205 }
0 commit comments