@@ -295,125 +295,82 @@ export default class Editor extends Plugin {
295295 const code = model . getValue ( )
296296
297297 try {
298- console . log ( '[DIAGNOSE-ONCHANGE] Change detected, analyzing imports...' )
299- const extractPackageName = ( p ) => p . startsWith ( '@' ) ? p . split ( '/' ) . slice ( 0 , 2 ) . join ( '/' ) : p . split ( '/' ) [ 0 ]
300298 const IMPORT_ANY_RE =
301299 / (?: i m p o r t | e x p o r t ) \s + [ ^ ' " ] * ?f r o m \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] | i m p o r t \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] | r e q u i r e \( \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] \s * \) / g
300+
302301 const rawImports = [ ...code . matchAll ( IMPORT_ANY_RE ) ]
303302 . map ( m => ( m [ 1 ] || m [ 2 ] || m [ 3 ] || '' ) . trim ( ) )
304- . filter ( Boolean )
305- const uniquePackages = [ ...new Set ( rawImports . map ( extractPackageName ) ) ]
303+ . filter ( p => p && ! p . startsWith ( '.' ) && ! p . startsWith ( 'file://' ) )
304+
305+ const uniqueImports = [ ...new Set ( rawImports ) ]
306+ const getBasePackage = ( p ) => p . startsWith ( '@' ) ? p . split ( '/' ) . slice ( 0 , 2 ) . join ( '/' ) : p . split ( '/' ) [ 0 ]
306307
307- const newPackages = uniquePackages . filter ( p => ! this . processedPackages . has ( p ) )
308- if ( newPackages . length === 0 ) {
309- console . log ( '[DIAGNOSE-ONCHANGE] No new packages to process.' )
310- return
311- }
308+ const newBasePackages = [ ...new Set ( uniqueImports . map ( getBasePackage ) ) ]
309+ . filter ( p => ! this . processedPackages . has ( p ) )
310+
311+ if ( newBasePackages . length === 0 ) return
312+
313+ console . log ( '[DIAGNOSE] New base packages for analysis:' , newBasePackages )
312314
313- console . log ( '[DIAGNOSE-ONCHANGE] New packages to process:' , newPackages )
315+ this . beginTypesBatch ( )
314316
315- for ( const pkg of newPackages ) {
316- this . addShimForPackage ( pkg )
317- }
317+ uniqueImports . forEach ( pkgImport => {
318+ this . addShimForPackage ( pkgImport )
319+ } )
320+
318321 this . updateTsCompilerOptions ( )
322+ console . log ( '[DIAGNOSE] Shims added. Red lines should disappear.' )
319323
320- this . beginTypesBatch ( )
321-
322- let newPathsFound = false
323- await Promise . all ( newPackages . map ( async ( pkg ) => {
324+ await Promise . all ( newBasePackages . map ( async ( basePackage ) => {
325+ this . processedPackages . add ( basePackage )
326+
327+ console . log ( `[DIAGNOSE-DEEP-PASS] Starting deep pass for " ${ basePackage } "` )
324328 try {
325- this . processedPackages . add ( pkg )
326-
327- const result = await startTypeLoadingProcess ( pkg )
328- console . log ( `[DIAGNOSE-ONCHANGE] Result received for "${ pkg } ":` , result ? { mainVirtualPath : result . mainVirtualPath , libsCount : result . libs . length } : 'null' )
329-
329+ const result = await startTypeLoadingProcess ( basePackage )
330330 if ( result && result . libs && result . libs . length > 0 ) {
331+ console . log ( `[DIAGNOSE-DEEP-PASS] "${ basePackage } " deep pass complete. Adding ${ result . libs . length } files.` )
331332 this . addExtraLibs ( result . libs )
332-
333- function cleanupBadPathKeys ( paths , pkg ) {
334- for ( const k of Object . keys ( paths ) ) {
335- const badDot = k . startsWith ( `${ pkg } .` )
336- const noSlash = k . startsWith ( pkg ) && ! k . includes ( '/' ) && k !== pkg
337- if ( badDot || noSlash ) delete paths [ k ]
338- }
339- }
340-
341- cleanupBadPathKeys ( this . tsModuleMappings , pkg )
342-
333+
343334 if ( result . subpathMap ) {
344- if ( result . subpathMap ) {
345- for ( const [ subpath , virtualPath ] of Object . entries ( result . subpathMap ) ) {
346- this . tsModuleMappings [ subpath ] = [ virtualPath ]
347- }
335+ for ( const [ subpath , virtualPath ] of Object . entries ( result . subpathMap ) ) {
336+ this . tsModuleMappings [ subpath ] = [ virtualPath ]
348337 }
349338 }
350-
351339 if ( result . mainVirtualPath ) {
352- this . tsModuleMappings [ pkg ] = [ result . mainVirtualPath . replace ( 'file:///node_modules/' , '' ) ]
353- } else {
354- console . warn ( `[DIAGNOSE-ONCHANGE] No mainVirtualPath found for "${ pkg } ", path mapping may be incomplete` )
340+ this . tsModuleMappings [ basePackage ] = [ result . mainVirtualPath . replace ( 'file:///node_modules/' , '' ) ]
355341 }
342+ this . tsModuleMappings [ `${ basePackage } /*` ] = [ `${ basePackage } /*` ]
343+
344+ uniqueImports
345+ . filter ( p => getBasePackage ( p ) === basePackage )
346+ . forEach ( p => this . removeShimsForPackage ( p ) )
356347
357- this . tsModuleMappings [ `${ pkg } /*` ] = [ `${ pkg } /*` ]
358-
359- const libsForPkg = result . libs . filter ( l => l . filePath . includes ( `/node_modules/${ pkg } /` ) )
360- for ( const lib of libsForPkg ) {
361- const asPath = lib . filePath . replace ( 'file:///node_modules/' , '' )
362- if ( asPath . endsWith ( '/index.d.ts' ) ) {
363- const dirSpec = asPath . replace ( '/index.d.ts' , '' )
364- if ( dirSpec . startsWith ( `${ pkg } /` ) ) {
365- this . tsModuleMappings [ dirSpec ] = [ asPath ]
366- }
367- }
368- if ( asPath . endsWith ( '.d.ts' ) ) {
369- const fileSpec = asPath . replace ( / \. d \. t s $ / , '' )
370- if ( fileSpec . startsWith ( `${ pkg } /` ) ) {
371- this . tsModuleMappings [ fileSpec ] = [ asPath ]
372- }
373- }
374- }
375-
376- this . removeShimsForPackage ( pkg )
377- newPathsFound = true
378348 } else {
379- console . warn ( `[DIAGNOSE-ONCHANGE ] No types found for "${ pkg } ", keeping shim ` )
349+ console . warn ( `[DIAGNOSE-DEEP-PASS ] No types found for "${ basePackage } ". Shim will remain. ` )
380350 }
381351 } catch ( e ) {
382- console . error ( ' [DIAGNOSE-ONCHANGE] Type load failed for' , pkg , e )
352+ console . error ( ` [DIAGNOSE-DEEP-PASS] Crawler failed for " ${ basePackage } ":` , e )
383353 }
384354 } ) )
355+
356+ console . log ( '[DIAGNOSE] All processes finished.' )
357+ this . endTypesBatch ( )
385358
386- if ( newPathsFound ) {
387- this . updateTsCompilerOptions ( )
388- } else {
389- console . log ( '[DIAGNOSE-ONCHANGE] No new paths were mapped.' )
390- }
391-
392- this . endTypesBatch ( )
393359 } catch ( error ) {
394- console . error ( '[DIAGNOSE-ONCHANGE] Error during type loading process:' , error )
360+ console . error ( '[DIAGNOSE-ONCHANGE] Critical error during type loading process:' , error )
361+ this . endTypesBatch ( )
395362 }
396363 } , 1500 )
397364 }
365+
398366 const currentFile = await this . call ( 'fileManager' , 'file' )
399- if ( ! currentFile ) {
400- return
401- }
402- if ( currentFile !== file ) {
403- return
404- }
367+ if ( ! currentFile || currentFile !== file ) return
368+
405369 const input = this . get ( currentFile )
406- if ( ! input ) {
407- return
408- }
409- // if there's no change, don't do anything
410- if ( input === this . previousInput ) {
411- return
412- }
370+ if ( ! input || input === this . previousInput ) return
371+
413372 this . previousInput = input
414373
415- // fire storage update
416- // NOTE: save at most once per 5 seconds
417374 if ( this . saveTimeout ) {
418375 window . clearTimeout ( this . saveTimeout )
419376 }
0 commit comments