@@ -181,6 +181,7 @@ namespace ts.codefix {
181
181
Named ,
182
182
Default ,
183
183
Namespace ,
184
+ Equals
184
185
}
185
186
186
187
export function getCodeActionForImport ( moduleSymbol : Symbol , context : ImportCodeFixOptions ) : ImportCodeAction [ ] {
@@ -212,7 +213,7 @@ namespace ts.codefix {
212
213
213
214
function getNamespaceImportName ( declaration : AnyImportSyntax ) : Identifier {
214
215
if ( declaration . kind === SyntaxKind . ImportDeclaration ) {
215
- const namedBindings = declaration . importClause && declaration . importClause . namedBindings ;
216
+ const namedBindings = declaration . importClause && isImportClause ( declaration . importClause ) && declaration . importClause . namedBindings ;
216
217
return namedBindings && namedBindings . kind === SyntaxKind . NamespaceImport ? namedBindings . name : undefined ;
217
218
}
218
219
else {
@@ -237,6 +238,8 @@ namespace ts.codefix {
237
238
return parent as ImportDeclaration ;
238
239
case SyntaxKind . ExternalModuleReference :
239
240
return ( parent as ExternalModuleReference ) . parent ;
241
+ case SyntaxKind . ImportEqualsDeclaration :
242
+ return parent as ImportEqualsDeclaration ;
240
243
default :
241
244
Debug . assert ( parent . kind === SyntaxKind . ExportDeclaration ) ;
242
245
// Ignore these, can't add imports to them.
@@ -249,11 +252,19 @@ namespace ts.codefix {
249
252
const lastImportDeclaration = findLast ( sourceFile . statements , isAnyImportSyntax ) ;
250
253
251
254
const moduleSpecifierWithoutQuotes = stripQuotes ( moduleSpecifier ) ;
252
- const importDecl = createImportDeclaration (
253
- /*decorators*/ undefined ,
254
- /*modifiers*/ undefined ,
255
- createImportClauseOfKind ( kind , symbolName ) ,
256
- createStringLiteralWithQuoteStyle ( sourceFile , moduleSpecifierWithoutQuotes ) ) ;
255
+ const quotedModuleSpecifier = createStringLiteralWithQuoteStyle ( sourceFile , moduleSpecifierWithoutQuotes ) ;
256
+ const importDecl = kind !== ImportKind . Equals
257
+ ? createImportDeclaration (
258
+ /*decorators*/ undefined ,
259
+ /*modifiers*/ undefined ,
260
+ createImportClauseOfKind ( kind , symbolName ) ,
261
+ quotedModuleSpecifier )
262
+ : createImportEqualsDeclaration (
263
+ /*decorators*/ undefined ,
264
+ /*modifiers*/ undefined ,
265
+ createIdentifier ( symbolName ) ,
266
+ createExternalModuleReference ( quotedModuleSpecifier ) ) ;
267
+
257
268
const changes = ChangeTracker . with ( context , changeTracker => {
258
269
if ( lastImportDeclaration ) {
259
270
changeTracker . insertNodeAfter ( sourceFile , lastImportDeclaration , importDecl , { suffix : newLineCharacter } ) ;
@@ -263,11 +274,17 @@ namespace ts.codefix {
263
274
}
264
275
} ) ;
265
276
277
+ const actionFormat = kind === ImportKind . Equals
278
+ ? Diagnostics . Import_0_require_1
279
+ : kind === ImportKind . Namespace
280
+ ? Diagnostics . Import_Asterisk_as_0_from_1
281
+ : Diagnostics . Import_0_from_1 ;
282
+
266
283
// if this file doesn't have any import statements, insert an import statement and then insert a new line
267
284
// between the only import statement and user code. Otherwise just insert the statement because chances
268
285
// are there are already a new line seperating code and import statements.
269
286
return createCodeAction (
270
- Diagnostics . Import_0_from_1 ,
287
+ actionFormat ,
271
288
[ symbolName , moduleSpecifierWithoutQuotes ] ,
272
289
changes ,
273
290
"NewImport" ,
@@ -282,7 +299,7 @@ namespace ts.codefix {
282
299
return literal ;
283
300
}
284
301
285
- function createImportClauseOfKind ( kind : ImportKind , symbolName : string ) {
302
+ function createImportClauseOfKind ( kind : ImportKind . Default | ImportKind . Named | ImportKind . Namespace , symbolName : string ) {
286
303
const id = createIdentifier ( symbolName ) ;
287
304
switch ( kind ) {
288
305
case ImportKind . Default :
@@ -534,7 +551,7 @@ namespace ts.codefix {
534
551
declarations : ReadonlyArray < AnyImportSyntax > ) : ImportCodeAction {
535
552
const fromExistingImport = firstDefined ( declarations , declaration => {
536
553
if ( declaration . kind === SyntaxKind . ImportDeclaration && declaration . importClause ) {
537
- const changes = tryUpdateExistingImport ( ctx , declaration . importClause ) ;
554
+ const changes = tryUpdateExistingImport ( ctx , isImportClause ( declaration . importClause ) && declaration . importClause || undefined ) ;
538
555
if ( changes ) {
539
556
const moduleSpecifierWithoutQuotes = stripQuotes ( declaration . moduleSpecifier . getText ( ) ) ;
540
557
return createCodeAction (
@@ -564,9 +581,10 @@ namespace ts.codefix {
564
581
return expression && isStringLiteral ( expression ) ? expression . text : undefined ;
565
582
}
566
583
567
- function tryUpdateExistingImport ( context : SymbolContext & { kind : ImportKind } , importClause : ImportClause ) : FileTextChanges [ ] | undefined {
584
+ function tryUpdateExistingImport ( context : SymbolContext & { kind : ImportKind } , importClause : ImportClause | ImportEqualsDeclaration ) : FileTextChanges [ ] | undefined {
568
585
const { symbolName, sourceFile, kind } = context ;
569
- const { name, namedBindings } = importClause ;
586
+ const { name } = importClause ;
587
+ const { namedBindings } = importClause . kind !== SyntaxKind . ImportEqualsDeclaration && importClause ;
570
588
switch ( kind ) {
571
589
case ImportKind . Default :
572
590
return name ? undefined : ChangeTracker . with ( context , t =>
@@ -592,6 +610,9 @@ namespace ts.codefix {
592
610
return namedBindings ? undefined : ChangeTracker . with ( context , t =>
593
611
t . replaceNode ( sourceFile , importClause , createImportClause ( name , createNamespaceImport ( createIdentifier ( symbolName ) ) ) ) ) ;
594
612
613
+ case ImportKind . Equals :
614
+ return undefined ;
615
+
595
616
default :
596
617
Debug . assertNever ( kind ) ;
597
618
}
@@ -644,6 +665,19 @@ namespace ts.codefix {
644
665
Debug . fail ( "Either the symbol or the JSX namespace should be a UMD global if we got here" ) ;
645
666
}
646
667
668
+ const { module, allowSyntheticDefaultImports } = context . compilerOptions ;
669
+
670
+ // Prefer to import as a synthetic `default` if available.
671
+ if ( allowSyntheticDefaultImports || module === ModuleKind . System && allowSyntheticDefaultImports !== false ) {
672
+ return getCodeActionForImport ( symbol , { ...context , symbolName, kind : ImportKind . Default } ) ;
673
+ }
674
+
675
+ // When a synthetic `default` is unavailable, use `import..require` if the module kind supports it.
676
+ if ( module === ModuleKind . AMD || module === ModuleKind . CommonJS || module === ModuleKind . UMD ) {
677
+ return getCodeActionForImport ( symbol , { ...context , symbolName, kind : ImportKind . Equals } ) ;
678
+ }
679
+
680
+ // Fall back to `* as ns` style imports.
647
681
return getCodeActionForImport ( symbol , { ...context , symbolName, kind : ImportKind . Namespace } ) ;
648
682
}
649
683
0 commit comments