88 "github.com/microsoft/TypeScript/tsc/internal/ast"
99 "github.com/microsoft/TypeScript/tsc/internal/astnav"
1010 "github.com/microsoft/TypeScript/tsc/internal/checker"
11- "github.com/microsoft/TypeScript/tsc/internal/compiler"
1211 "github.com/microsoft/TypeScript/tsc/internal/core"
1312 "github.com/microsoft/TypeScript/tsc/internal/diagnostics"
1413 "github.com/microsoft/TypeScript/tsc/internal/locale"
@@ -111,17 +110,17 @@ func getAllImportCodeActions(ctx context.Context, fixContext *CodeFixContext) (*
111110 return nil , nil
112111 }
113112
114- view , err := fixContext .LS .getPreparedAutoImportView (fixContext .SourceFile )
113+ ch , done := fixContext .Program .GetTypeChecker (ctx )
114+ defer done ()
115+
116+ view , err := fixContext .LS .getPreparedAutoImportView (fixContext .SourceFile , ch )
115117 if err != nil {
116118 return nil , err
117119 }
118120 if view == nil {
119- view = fixContext .LS .getCurrentAutoImportView (fixContext .SourceFile )
121+ view = fixContext .LS .getCurrentAutoImportView (fixContext .SourceFile , ch )
120122 }
121123
122- ch , done := fixContext .Program .GetTypeChecker (ctx )
123- defer done ()
124-
125124 importAdder := autoimport .NewImportAdder (
126125 ctx ,
127126 fixContext .Program ,
@@ -176,18 +175,20 @@ func getFixInfos(ctx context.Context, fixContext *CodeFixContext, errorCode int3
176175 }
177176
178177 symbolToken := astnav .GetTokenAtPosition (fixContext .SourceFile , pos )
178+ if errorCode != diagnostics .X_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead .Code () && ! ast .IsIdentifier (symbolToken ) {
179+ return nil , nil
180+ }
181+
182+ ch , done := fixContext .Program .GetTypeChecker (ctx )
183+ defer done ()
179184
180185 var view * autoimport.View
181186 var info []* fixInfo
182187
183188 if errorCode == diagnostics .X_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead .Code () {
184- view = fixContext .LS .getCurrentAutoImportView (fixContext .SourceFile )
185- info = getFixesInfoForUMDImport (ctx , fixContext , symbolToken , view )
186- } else if ! ast .IsIdentifier (symbolToken ) {
187- return nil , nil
189+ view = fixContext .LS .getCurrentAutoImportView (fixContext .SourceFile , ch )
190+ info = getFixesInfoForUMDImport (symbolToken , view , ch )
188191 } else if errorCode == diagnostics .X_0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type .Code () {
189- ch , done := fixContext .Program .GetTypeChecker (ctx )
190- defer done ()
191192 compilerOptions := fixContext .Program .Options ()
192193 symbolNames := getSymbolNamesToImport (fixContext .SourceFile , ch , symbolToken , compilerOptions )
193194
@@ -196,7 +197,7 @@ func getFixInfos(ctx context.Context, fixContext *CodeFixContext, errorCode int3
196197 if ! sn .isTypeOnly {
197198 continue
198199 }
199- fix := getTypeOnlyPromotionFix (ctx , fixContext .SourceFile , symbolToken , sn .name , fixContext . Program )
200+ fix := getTypeOnlyPromotionFix (fixContext .SourceFile , symbolToken , sn .name , ch )
200201 if fix != nil {
201202 allTypeOnlyFixes = append (allTypeOnlyFixes , & fixInfo {fix : fix , symbolName : sn .name , errorIdentifierText : symbolToken .Text ()})
202203 }
@@ -224,26 +225,23 @@ func getFixInfos(ctx context.Context, fixContext *CodeFixContext, errorCode int3
224225 return info , nil
225226 } else {
226227 var err error
227- view , err = fixContext .LS .getPreparedAutoImportView (fixContext .SourceFile )
228+ view , err = fixContext .LS .getPreparedAutoImportView (fixContext .SourceFile , ch )
228229 if err != nil {
229230 return nil , err
230231 }
231232 if view != nil {
232- info = getFixesInfoForNonUMDImport (ctx , fixContext , symbolToken , view )
233+ info = getFixesInfoForNonUMDImport (fixContext , symbolToken , view , ch )
233234 }
234235 }
235236
236237 // Sort fixes by preference
237238 if view == nil {
238- view = fixContext .LS .getCurrentAutoImportView (fixContext .SourceFile )
239+ view = fixContext .LS .getCurrentAutoImportView (fixContext .SourceFile , ch )
239240 }
240241 return sortFixInfo (info , fixContext , view ), nil
241242}
242243
243- func getFixesInfoForUMDImport (ctx context.Context , fixContext * CodeFixContext , token * ast.Node , view * autoimport.View ) []* fixInfo {
244- ch , done := fixContext .Program .GetTypeChecker (ctx )
245- defer done ()
246-
244+ func getFixesInfoForUMDImport (token * ast.Node , view * autoimport.View , ch * checker.Checker ) []* fixInfo {
247245 umdSymbol := getUmdSymbol (token , ch )
248246 if umdSymbol == nil {
249247 return nil
@@ -253,7 +251,7 @@ func getFixesInfoForUMDImport(ctx context.Context, fixContext *CodeFixContext, t
253251 isValidTypeOnlyUseSite := ast .IsValidTypeOnlyAliasUseSite (token )
254252
255253 var result []* fixInfo
256- for _ , fix := range view .GetFixes (ctx , export , false , isValidTypeOnlyUseSite , nil ) {
254+ for _ , fix := range view .GetFixes (export , false , isValidTypeOnlyUseSite , nil ) {
257255 errorIdentifierText := ""
258256 if ast .IsIdentifier (token ) {
259257 errorIdentifierText = token .Text ()
@@ -302,9 +300,7 @@ func isUMDExportSymbol(symbol *ast.Symbol) bool {
302300 ast .IsNamespaceExportDeclaration (symbol .Declarations [0 ])
303301}
304302
305- func getFixesInfoForNonUMDImport (ctx context.Context , fixContext * CodeFixContext , symbolToken * ast.Node , view * autoimport.View ) []* fixInfo {
306- ch , done := fixContext .Program .GetTypeChecker (ctx )
307- defer done ()
303+ func getFixesInfoForNonUMDImport (fixContext * CodeFixContext , symbolToken * ast.Node , view * autoimport.View , ch * checker.Checker ) []* fixInfo {
308304 compilerOptions := fixContext .Program .Options ()
309305
310306 isValidTypeOnlyUseSite := ast .IsValidTypeOnlyAliasUseSite (symbolToken )
@@ -341,7 +337,7 @@ func getFixesInfoForNonUMDImport(ctx context.Context, fixContext *CodeFixContext
341337 continue
342338 }
343339
344- fixes := view .GetFixes (ctx , export , isJSXTagName , isValidTypeOnlyUseSite , & usagePosition )
340+ fixes := view .GetFixes (export , isJSXTagName , isValidTypeOnlyUseSite , & usagePosition )
345341 for _ , fix := range fixes {
346342 allInfo = append (allInfo , & fixInfo {
347343 fix : fix ,
@@ -355,10 +351,7 @@ func getFixesInfoForNonUMDImport(ctx context.Context, fixContext *CodeFixContext
355351 return allInfo
356352}
357353
358- func getTypeOnlyPromotionFix (ctx context.Context , sourceFile * ast.SourceFile , symbolToken * ast.Node , symbolName string , program * compiler.Program ) * autoimport.Fix {
359- ch , done := program .GetTypeChecker (ctx )
360- defer done ()
361-
354+ func getTypeOnlyPromotionFix (sourceFile * ast.SourceFile , symbolToken * ast.Node , symbolName string , ch * checker.Checker ) * autoimport.Fix {
362355 // Get the symbol at the token location
363356 symbol := ch .ResolveName (symbolName , symbolToken , ast .SymbolFlagsValue , true /* excludeGlobals */ )
364357 if symbol == nil {
0 commit comments