@@ -24,6 +24,7 @@ import {
2424 PrettierVSCodeConfig ,
2525} from "./types" ;
2626import { getConfig , getWorkspaceRelativePath } from "./util" ;
27+ import { PrettierWorkerInstance } from "./PrettierWorkerInstance" ;
2728
2829const minPrettierVersion = "1.13.0" ;
2930declare const __webpack_require__ : typeof require ;
@@ -93,7 +94,8 @@ function globalPathGet(packageManager: PackageManagers): string | undefined {
9394export class ModuleResolver implements ModuleResolverInterface {
9495 private findPkgCache : Map < string , string > ;
9596 private ignorePathCache = new Map < string , string > ( ) ;
96- private path2Module = new Map < string , PrettierNodeModule > ( ) ;
97+
98+ private path2Module = new Map < string , PrettierWorkerInstance > ( ) ;
9799
98100 constructor ( private loggingService : LoggingService ) {
99101 this . findPkgCache = new Map ( ) ;
@@ -109,7 +111,7 @@ export class ModuleResolver implements ModuleResolverInterface {
109111 */
110112 public async getPrettierInstance (
111113 fileName : string
112- ) : Promise < PrettierNodeModule | undefined > {
114+ ) : Promise < PrettierNodeModule | PrettierWorkerInstance | undefined > {
113115 if ( ! workspace . isTrusted ) {
114116 this . loggingService . logDebug ( UNTRUSTED_WORKSPACE_USING_BUNDLED_PRETTIER ) ;
115117 return prettier ;
@@ -172,7 +174,8 @@ export class ModuleResolver implements ModuleResolverInterface {
172174 }
173175 }
174176
175- let moduleInstance : PrettierNodeModule | undefined = undefined ;
177+ let moduleInstance : PrettierWorkerInstance | undefined = undefined ;
178+
176179 if ( modulePath !== undefined ) {
177180 this . loggingService . logDebug (
178181 `Local prettier module path: '${ modulePath } '`
@@ -183,7 +186,7 @@ export class ModuleResolver implements ModuleResolverInterface {
183186 return moduleInstance ;
184187 } else {
185188 try {
186- moduleInstance = this . loadNodeModule < PrettierNodeModule > ( modulePath ) ;
189+ moduleInstance = new PrettierWorkerInstance ( modulePath ) ;
187190 if ( moduleInstance ) {
188191 this . path2Module . set ( modulePath , moduleInstance ) ;
189192 }
@@ -202,31 +205,23 @@ export class ModuleResolver implements ModuleResolverInterface {
202205 }
203206
204207 if ( moduleInstance ) {
205- // If the instance is missing `format`, it's probably
206- // not an instance of Prettier
207- const isPrettierInstance = ! ! moduleInstance . format ;
208- const isValidVersion =
209- moduleInstance . version &&
210- ! ! moduleInstance . getSupportInfo &&
211- ! ! moduleInstance . getFileInfo &&
212- ! ! moduleInstance . resolveConfig &&
213- semver . gte ( moduleInstance . version , minPrettierVersion ) ;
214-
215- if ( ! isPrettierInstance && prettierPath ) {
208+ const version = await moduleInstance . import ( ) ;
209+
210+ if ( ! version && prettierPath ) {
216211 this . loggingService . logError ( INVALID_PRETTIER_PATH_MESSAGE ) ;
217212 return undefined ;
218213 }
219214
215+ const isValidVersion = version && semver . gte ( version , minPrettierVersion ) ;
216+
220217 if ( ! isValidVersion ) {
221218 this . loggingService . logInfo (
222219 `Attempted to load Prettier module from ${ modulePath } `
223220 ) ;
224221 this . loggingService . logError ( OUTDATED_PRETTIER_VERSION_MESSAGE ) ;
225222 return undefined ;
226223 } else {
227- this . loggingService . logDebug (
228- `Using prettier version ${ moduleInstance . version } `
229- ) ;
224+ this . loggingService . logDebug ( `Using prettier version ${ version } ` ) ;
230225 }
231226 return moduleInstance ;
232227 } else {
@@ -351,6 +346,7 @@ export class ModuleResolver implements ModuleResolverInterface {
351346 await prettier . clearConfigCache ( ) ;
352347 this . path2Module . forEach ( ( module ) => {
353348 try {
349+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
354350 module . clearConfigCache ( ) ;
355351 } catch ( error ) {
356352 this . loggingService . logError ( "Error clearing module cache." , error ) ;
@@ -365,19 +361,6 @@ export class ModuleResolver implements ModuleResolverInterface {
365361 : require ;
366362 }
367363
368- // Source: https://github.com/microsoft/vscode-eslint/blob/master/server/src/eslintServer.ts
369- private loadNodeModule < T > ( moduleName : string ) : T | undefined {
370- try {
371- return this . nodeModuleLoader ( moduleName ) ;
372- } catch ( error ) {
373- this . loggingService . logError (
374- `Error loading node module '${ moduleName } '` ,
375- error
376- ) ;
377- }
378- return undefined ;
379- }
380-
381364 private resolveNodeModule ( moduleName : string , options ?: { paths : string [ ] } ) {
382365 try {
383366 return this . nodeModuleLoader . resolve ( moduleName , options ) ;
0 commit comments