@@ -10,21 +10,25 @@ import {
10
10
factory ,
11
11
first ,
12
12
getAllowSyntheticDefaultImports ,
13
+ getQuotePreference ,
13
14
getTokenAtPosition ,
14
15
Identifier ,
15
16
ImportSpecifier ,
16
17
isIdentifier ,
18
+ isNoSubstitutionTemplateLiteral ,
17
19
isObjectBindingPattern ,
18
20
isRequireCall ,
19
21
isVariableDeclaration ,
20
22
isVariableStatement ,
21
23
NamedImports ,
22
24
ObjectBindingPattern ,
23
25
Program ,
26
+ QuotePreference ,
24
27
SourceFile ,
25
28
StringLiteralLike ,
26
29
textChanges ,
27
30
tryCast ,
31
+ UserPreferences ,
28
32
VariableStatement ,
29
33
} from "../_namespaces/ts.js" ;
30
34
@@ -33,7 +37,7 @@ const errorCodes = [Diagnostics.require_call_may_be_converted_to_an_import.code]
33
37
registerCodeFix ( {
34
38
errorCodes,
35
39
getCodeActions ( context ) {
36
- const info = getInfo ( context . sourceFile , context . program , context . span . start ) ;
40
+ const info = getInfo ( context . sourceFile , context . program , context . span . start , context . preferences ) ;
37
41
if ( ! info ) {
38
42
return undefined ;
39
43
}
@@ -43,21 +47,21 @@ registerCodeFix({
43
47
fixIds : [ fixId ] ,
44
48
getAllCodeActions : context =>
45
49
codeFixAll ( context , errorCodes , ( changes , diag ) => {
46
- const info = getInfo ( diag . file , context . program , diag . start ) ;
50
+ const info = getInfo ( diag . file , context . program , diag . start , context . preferences ) ;
47
51
if ( info ) {
48
52
doChange ( changes , context . sourceFile , info ) ;
49
53
}
50
54
} ) ,
51
55
} ) ;
52
56
53
57
function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , info : Info ) {
54
- const { allowSyntheticDefaults, defaultImportName, namedImports, statement, required } = info ;
58
+ const { allowSyntheticDefaults, defaultImportName, namedImports, statement, moduleSpecifier } = info ;
55
59
changes . replaceNode (
56
60
sourceFile ,
57
61
statement ,
58
62
defaultImportName && ! allowSyntheticDefaults
59
- ? factory . createImportEqualsDeclaration ( /*modifiers*/ undefined , /*isTypeOnly*/ false , defaultImportName , factory . createExternalModuleReference ( required ) )
60
- : factory . createImportDeclaration ( /*modifiers*/ undefined , factory . createImportClause ( /*isTypeOnly*/ false , defaultImportName , namedImports ) , required , /*attributes*/ undefined ) ,
63
+ ? factory . createImportEqualsDeclaration ( /*modifiers*/ undefined , /*isTypeOnly*/ false , defaultImportName , factory . createExternalModuleReference ( moduleSpecifier ) )
64
+ : factory . createImportDeclaration ( /*modifiers*/ undefined , factory . createImportClause ( /*isTypeOnly*/ false , defaultImportName , namedImports ) , moduleSpecifier , /*attributes*/ undefined ) ,
61
65
) ;
62
66
}
63
67
@@ -66,25 +70,27 @@ interface Info {
66
70
readonly defaultImportName : Identifier | undefined ;
67
71
readonly namedImports : NamedImports | undefined ;
68
72
readonly statement : VariableStatement ;
69
- readonly required : StringLiteralLike ;
73
+ readonly moduleSpecifier : StringLiteralLike ;
70
74
}
71
75
72
- function getInfo ( sourceFile : SourceFile , program : Program , pos : number ) : Info | undefined {
76
+ function getInfo ( sourceFile : SourceFile , program : Program , pos : number , preferences : UserPreferences ) : Info | undefined {
73
77
const { parent } = getTokenAtPosition ( sourceFile , pos ) ;
74
78
if ( ! isRequireCall ( parent , /*requireStringLiteralLikeArgument*/ true ) ) {
75
79
Debug . failBadSyntaxKind ( parent ) ;
76
80
}
77
81
78
82
const decl = cast ( parent . parent , isVariableDeclaration ) ;
83
+ const quotePreference = getQuotePreference ( sourceFile , preferences ) ;
79
84
const defaultImportName = tryCast ( decl . name , isIdentifier ) ;
80
85
const namedImports = isObjectBindingPattern ( decl . name ) ? tryCreateNamedImportsFromObjectBindingPattern ( decl . name ) : undefined ;
81
86
if ( defaultImportName || namedImports ) {
87
+ const moduleSpecifier = first ( parent . arguments ) ;
82
88
return {
83
89
allowSyntheticDefaults : getAllowSyntheticDefaultImports ( program . getCompilerOptions ( ) ) ,
84
90
defaultImportName,
85
91
namedImports,
86
92
statement : cast ( decl . parent . parent , isVariableStatement ) ,
87
- required : first ( parent . arguments ) ,
93
+ moduleSpecifier : isNoSubstitutionTemplateLiteral ( moduleSpecifier ) ? factory . createStringLiteral ( moduleSpecifier . text , quotePreference === QuotePreference . Single ) : moduleSpecifier ,
88
94
} ;
89
95
}
90
96
}
0 commit comments