@@ -30,6 +30,7 @@ namespace ts.codefix {
30
30
compilerOptions : CompilerOptions ;
31
31
getCanonicalFileName : GetCanonicalFileName ;
32
32
cachedImportDeclarations ?: ImportDeclarationMap ;
33
+ options : Options ;
33
34
}
34
35
35
36
function createCodeAction ( descriptionDiagnostic : DiagnosticMessage , diagnosticArgs : string [ ] , changes : FileTextChanges [ ] ) : CodeFixAction {
@@ -53,7 +54,8 @@ namespace ts.codefix {
53
54
cachedImportDeclarations : [ ] ,
54
55
getCanonicalFileName : createGetCanonicalFileName ( useCaseSensitiveFileNames ) ,
55
56
symbolName,
56
- symbolToken
57
+ symbolToken,
58
+ options : context . options ,
57
59
} ;
58
60
}
59
61
@@ -95,12 +97,13 @@ namespace ts.codefix {
95
97
formatContext : ts . formatting . FormatContext ,
96
98
getCanonicalFileName : GetCanonicalFileName ,
97
99
symbolToken : Node | undefined ,
100
+ options : Options ,
98
101
) : { readonly moduleSpecifier : string , readonly codeAction : CodeAction } {
99
102
const exportInfos = getAllReExportingModules ( exportedSymbol , checker , allSourceFiles ) ;
100
103
Debug . assert ( exportInfos . some ( info => info . moduleSymbol === moduleSymbol ) ) ;
101
104
// We sort the best codefixes first, so taking `first` is best for completions.
102
105
const moduleSpecifier = first ( getNewImportInfos ( program , sourceFile , exportInfos , compilerOptions , getCanonicalFileName , host ) ) . moduleSpecifier ;
103
- const ctx : ImportCodeFixContext = { host, program, checker, compilerOptions, sourceFile, formatContext, symbolName, getCanonicalFileName, symbolToken } ;
106
+ const ctx : ImportCodeFixContext = { host, program, checker, compilerOptions, sourceFile, formatContext, symbolName, getCanonicalFileName, symbolToken, options } ;
104
107
return { moduleSpecifier, codeAction : first ( getCodeActionsForImport ( exportInfos , ctx ) ) } ;
105
108
}
106
109
function getAllReExportingModules ( exportedSymbol : Symbol , checker : TypeChecker , allSourceFiles : ReadonlyArray < SourceFile > ) : ReadonlyArray < SymbolExportInfo > {
@@ -181,12 +184,12 @@ namespace ts.codefix {
181
184
}
182
185
}
183
186
184
- function getCodeActionForNewImport ( context : SymbolContext , { moduleSpecifier, importKind } : NewImportInfo ) : CodeFixAction {
187
+ function getCodeActionForNewImport ( context : SymbolContext & { options : Options } , { moduleSpecifier, importKind } : NewImportInfo ) : CodeFixAction {
185
188
const { sourceFile, symbolName } = context ;
186
189
const lastImportDeclaration = findLast ( sourceFile . statements , isAnyImportSyntax ) ;
187
190
188
191
const moduleSpecifierWithoutQuotes = stripQuotes ( moduleSpecifier ) ;
189
- const quotedModuleSpecifier = createStringLiteralWithQuoteStyle ( sourceFile , moduleSpecifierWithoutQuotes ) ;
192
+ const quotedModuleSpecifier = createStringLiteralWithQuoteStyle ( sourceFile , moduleSpecifierWithoutQuotes , context . options ) ;
190
193
const importDecl = importKind !== ImportKind . Equals
191
194
? createImportDeclaration (
192
195
/*decorators*/ undefined ,
@@ -214,12 +217,20 @@ namespace ts.codefix {
214
217
return createCodeAction ( Diagnostics . Import_0_from_module_1 , [ symbolName , moduleSpecifierWithoutQuotes ] , changes ) ;
215
218
}
216
219
217
- function createStringLiteralWithQuoteStyle ( sourceFile : SourceFile , text : string ) : StringLiteral {
220
+ function createStringLiteralWithQuoteStyle ( sourceFile : SourceFile , text : string , options : Options ) : StringLiteral {
218
221
const literal = createLiteral ( text ) ;
219
- const firstModuleSpecifier = firstOrUndefined ( sourceFile . imports ) ;
220
- literal . singleQuote = ! ! firstModuleSpecifier && ! isStringDoubleQuoted ( firstModuleSpecifier , sourceFile ) ;
222
+ literal . singleQuote = shouldUseSingleQuote ( sourceFile , options ) ;
221
223
return literal ;
222
224
}
225
+ function shouldUseSingleQuote ( sourceFile : SourceFile , options : Options ) : boolean {
226
+ if ( options . quote ) {
227
+ return options . quote === "single" ;
228
+ }
229
+ else {
230
+ const firstModuleSpecifier = firstOrUndefined ( sourceFile . imports ) ;
231
+ return ! ! firstModuleSpecifier && ! isStringDoubleQuoted ( firstModuleSpecifier , sourceFile ) ;
232
+ }
233
+ }
223
234
224
235
function usesJsExtensionOnImports ( sourceFile : SourceFile ) : boolean {
225
236
return firstDefined ( sourceFile . imports , ( { text } ) => pathIsRelative ( text ) ? fileExtensionIs ( text , Extension . Js ) : undefined ) || false ;
0 commit comments