@@ -112,7 +112,10 @@ namespace ts.codefix {
112
112
}
113
113
114
114
registerCodeFix ( {
115
- errorCodes : [ Diagnostics . Cannot_find_name_0 . code ] ,
115
+ errorCodes : [
116
+ Diagnostics . Cannot_find_name_0 . code ,
117
+ Diagnostics . _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead . code
118
+ ] ,
116
119
getCodeActions : ( context : CodeFixContext ) => {
117
120
const sourceFile = context . sourceFile ;
118
121
const checker = context . program . getTypeChecker ( ) ;
@@ -127,14 +130,19 @@ namespace ts.codefix {
127
130
const cachedImportDeclarations = createMap < ( ImportDeclaration | ImportEqualsDeclaration ) [ ] > ( ) ;
128
131
let cachedNewImportInsertPosition : number ;
129
132
133
+ const currentTokenMeaning = getMeaningFromLocation ( token ) ;
134
+ if ( context . errorCode === Diagnostics . _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead . code ) {
135
+ const symbol = checker . getAliasedSymbol ( checker . getSymbolAtLocation ( token ) ) ;
136
+ return getCodeActionForImport ( symbol , /*isDefault*/ false , /*isNamespaceImport*/ true ) ;
137
+ }
138
+
130
139
const allPotentialModules = checker . getAmbientModules ( ) ;
131
140
for ( const otherSourceFile of allSourceFiles ) {
132
141
if ( otherSourceFile !== sourceFile && isExternalOrCommonJsModule ( otherSourceFile ) ) {
133
142
allPotentialModules . push ( otherSourceFile . symbol ) ;
134
143
}
135
144
}
136
145
137
- const currentTokenMeaning = getMeaningFromLocation ( token ) ;
138
146
for ( const moduleSymbol of allPotentialModules ) {
139
147
context . cancellationToken . throwIfCancellationRequested ( ) ;
140
148
@@ -203,7 +211,7 @@ namespace ts.codefix {
203
211
return declarations ? some ( symbol . declarations , decl => ! ! ( getMeaningFromDeclaration ( decl ) & meaning ) ) : false ;
204
212
}
205
213
206
- function getCodeActionForImport ( moduleSymbol : Symbol , isDefault ?: boolean ) : ImportCodeAction [ ] {
214
+ function getCodeActionForImport ( moduleSymbol : Symbol , isDefault ?: boolean , isNamespaceImport ?: boolean ) : ImportCodeAction [ ] {
207
215
const existingDeclarations = getImportDeclarations ( moduleSymbol ) ;
208
216
if ( existingDeclarations . length > 0 ) {
209
217
// With an existing import statement, there are more than one actions the user can do.
@@ -213,8 +221,6 @@ namespace ts.codefix {
213
221
return [ getCodeActionForNewImport ( ) ] ;
214
222
}
215
223
216
-
217
-
218
224
function getCodeActionsForExistingImport ( declarations : ( ImportDeclaration | ImportEqualsDeclaration ) [ ] ) : ImportCodeAction [ ] {
219
225
const actions : ImportCodeAction [ ] = [ ] ;
220
226
@@ -262,7 +268,7 @@ namespace ts.codefix {
262
268
actions . push ( getCodeActionForNamespaceImport ( namespaceImportDeclaration ) ) ;
263
269
}
264
270
265
- if ( namedImportDeclaration && namedImportDeclaration . importClause &&
271
+ if ( ! isNamespaceImport && namedImportDeclaration && namedImportDeclaration . importClause &&
266
272
( namedImportDeclaration . importClause . name || namedImportDeclaration . importClause . namedBindings ) ) {
267
273
/**
268
274
* If the existing import declaration already has a named import list, just
@@ -386,7 +392,9 @@ namespace ts.codefix {
386
392
const moduleSpecifierWithoutQuotes = stripQuotes ( moduleSpecifier || getModuleSpecifierForNewImport ( ) ) ;
387
393
const importStatementText = isDefault
388
394
? `import ${ name } from "${ moduleSpecifierWithoutQuotes } "`
389
- : `import { ${ name } } from "${ moduleSpecifierWithoutQuotes } "` ;
395
+ : isNamespaceImport
396
+ ? `import * as ${ name } from "${ moduleSpecifierWithoutQuotes } "`
397
+ : `import { ${ name } } from "${ moduleSpecifierWithoutQuotes } "` ;
390
398
391
399
// if this file doesn't have any import statements, insert an import statement and then insert a new line
392
400
// between the only import statement and user code. Otherwise just insert the statement because chances
0 commit comments