@@ -3,12 +3,12 @@ namespace ts.codefix {
3
3
registerCodeFix ( {
4
4
errorCodes : [ Diagnostics . File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module . code ] ,
5
5
getCodeActions ( context ) {
6
- const { sourceFile, program } = context ;
6
+ const { sourceFile, program, preferences } = context ;
7
7
const changes = textChanges . ChangeTracker . with ( context , changes => {
8
- const moduleExportsChangedToDefault = convertFileToEs6Module ( sourceFile , program . getTypeChecker ( ) , changes , program . getCompilerOptions ( ) . target ! ) ;
8
+ const moduleExportsChangedToDefault = convertFileToEs6Module ( sourceFile , program . getTypeChecker ( ) , changes , program . getCompilerOptions ( ) . target ! , preferences ) ;
9
9
if ( moduleExportsChangedToDefault ) {
10
10
for ( const importingFile of program . getSourceFiles ( ) ) {
11
- fixImportOfModuleExports ( importingFile , sourceFile , changes ) ;
11
+ fixImportOfModuleExports ( importingFile , sourceFile , changes , preferences ) ;
12
12
}
13
13
}
14
14
} ) ;
@@ -17,7 +17,7 @@ namespace ts.codefix {
17
17
} ,
18
18
} ) ;
19
19
20
- function fixImportOfModuleExports ( importingFile : SourceFile , exportingFile : SourceFile , changes : textChanges . ChangeTracker ) {
20
+ function fixImportOfModuleExports ( importingFile : SourceFile , exportingFile : SourceFile , changes : textChanges . ChangeTracker , preferences : UserPreferences ) {
21
21
for ( const moduleSpecifier of importingFile . imports ) {
22
22
const imported = getResolvedModule ( importingFile , moduleSpecifier . text ) ;
23
23
if ( ! imported || imported . resolvedFileName !== exportingFile . fileName ) {
@@ -27,7 +27,7 @@ namespace ts.codefix {
27
27
const importNode = importFromModuleSpecifier ( moduleSpecifier ) ;
28
28
switch ( importNode . kind ) {
29
29
case SyntaxKind . ImportEqualsDeclaration :
30
- changes . replaceNode ( importingFile , importNode , makeImport ( importNode . name , /*namedImports*/ undefined , moduleSpecifier ) ) ;
30
+ changes . replaceNode ( importingFile , importNode , makeImport ( importNode . name , /*namedImports*/ undefined , moduleSpecifier , preferences ) ) ;
31
31
break ;
32
32
case SyntaxKind . CallExpression :
33
33
if ( isRequireCall ( importNode , /*checkArgumentIsStringLiteralLike*/ false ) ) {
@@ -39,13 +39,13 @@ namespace ts.codefix {
39
39
}
40
40
41
41
/** @returns Whether we converted a `module.exports =` to a default export. */
42
- function convertFileToEs6Module ( sourceFile : SourceFile , checker : TypeChecker , changes : textChanges . ChangeTracker , target : ScriptTarget ) : ModuleExportsChanged {
42
+ function convertFileToEs6Module ( sourceFile : SourceFile , checker : TypeChecker , changes : textChanges . ChangeTracker , target : ScriptTarget , preferences : UserPreferences ) : ModuleExportsChanged {
43
43
const identifiers : Identifiers = { original : collectFreeIdentifiers ( sourceFile ) , additional : createMap < true > ( ) } ;
44
44
const exports = collectExportRenames ( sourceFile , checker , identifiers ) ;
45
45
convertExportsAccesses ( sourceFile , exports , changes ) ;
46
46
let moduleExportsChangedToDefault = false ;
47
47
for ( const statement of sourceFile . statements ) {
48
- const moduleExportsChanged = convertStatement ( sourceFile , statement , checker , changes , identifiers , target , exports ) ;
48
+ const moduleExportsChanged = convertStatement ( sourceFile , statement , checker , changes , identifiers , target , exports , preferences ) ;
49
49
moduleExportsChangedToDefault = moduleExportsChangedToDefault || moduleExportsChanged ;
50
50
}
51
51
return moduleExportsChangedToDefault ;
@@ -98,18 +98,18 @@ namespace ts.codefix {
98
98
/** Whether `module.exports =` was changed to `export default` */
99
99
type ModuleExportsChanged = boolean ;
100
100
101
- function convertStatement ( sourceFile : SourceFile , statement : Statement , checker : TypeChecker , changes : textChanges . ChangeTracker , identifiers : Identifiers , target : ScriptTarget , exports : ExportRenames ) : ModuleExportsChanged {
101
+ function convertStatement ( sourceFile : SourceFile , statement : Statement , checker : TypeChecker , changes : textChanges . ChangeTracker , identifiers : Identifiers , target : ScriptTarget , exports : ExportRenames , preferences : UserPreferences ) : ModuleExportsChanged {
102
102
switch ( statement . kind ) {
103
103
case SyntaxKind . VariableStatement :
104
- convertVariableStatement ( sourceFile , statement as VariableStatement , changes , checker , identifiers , target ) ;
104
+ convertVariableStatement ( sourceFile , statement as VariableStatement , changes , checker , identifiers , target , preferences ) ;
105
105
return false ;
106
106
case SyntaxKind . ExpressionStatement : {
107
107
const { expression } = statement as ExpressionStatement ;
108
108
switch ( expression . kind ) {
109
109
case SyntaxKind . CallExpression : {
110
110
if ( isRequireCall ( expression , /*checkArgumentIsStringLiteralLike*/ true ) ) {
111
111
// For side-effecting require() call, just make a side-effecting import.
112
- changes . replaceNode ( sourceFile , statement , makeImport ( /*name*/ undefined , /*namedImports*/ undefined , expression . arguments [ 0 ] ) ) ;
112
+ changes . replaceNode ( sourceFile , statement , makeImport ( /*name*/ undefined , /*namedImports*/ undefined , expression . arguments [ 0 ] , preferences ) ) ;
113
113
}
114
114
return false ;
115
115
}
@@ -125,7 +125,7 @@ namespace ts.codefix {
125
125
}
126
126
}
127
127
128
- function convertVariableStatement ( sourceFile : SourceFile , statement : VariableStatement , changes : textChanges . ChangeTracker , checker : TypeChecker , identifiers : Identifiers , target : ScriptTarget ) : void {
128
+ function convertVariableStatement ( sourceFile : SourceFile , statement : VariableStatement , changes : textChanges . ChangeTracker , checker : TypeChecker , identifiers : Identifiers , target : ScriptTarget , preferences : UserPreferences ) : void {
129
129
const { declarationList } = statement ;
130
130
let foundImport = false ;
131
131
const newNodes = flatMap ( declarationList . declarations , decl => {
@@ -138,11 +138,11 @@ namespace ts.codefix {
138
138
}
139
139
else if ( isRequireCall ( initializer , /*checkArgumentIsStringLiteralLike*/ true ) ) {
140
140
foundImport = true ;
141
- return convertSingleImport ( sourceFile , name , initializer . arguments [ 0 ] , changes , checker , identifiers , target ) ;
141
+ return convertSingleImport ( sourceFile , name , initializer . arguments [ 0 ] , changes , checker , identifiers , target , preferences ) ;
142
142
}
143
143
else if ( isPropertyAccessExpression ( initializer ) && isRequireCall ( initializer . expression , /*checkArgumentIsStringLiteralLike*/ true ) ) {
144
144
foundImport = true ;
145
- return convertPropertyAccessImport ( name , initializer . name . text , initializer . expression . arguments [ 0 ] , identifiers ) ;
145
+ return convertPropertyAccessImport ( name , initializer . name . text , initializer . expression . arguments [ 0 ] , identifiers , preferences ) ;
146
146
}
147
147
}
148
148
// Move it out to its own variable statement. (This will not be used if `!foundImport`)
@@ -155,20 +155,20 @@ namespace ts.codefix {
155
155
}
156
156
157
157
/** Converts `const name = require("moduleSpecifier").propertyName` */
158
- function convertPropertyAccessImport ( name : BindingName , propertyName : string , moduleSpecifier : StringLiteralLike , identifiers : Identifiers ) : ReadonlyArray < Node > {
158
+ function convertPropertyAccessImport ( name : BindingName , propertyName : string , moduleSpecifier : StringLiteralLike , identifiers : Identifiers , preferences : UserPreferences ) : ReadonlyArray < Node > {
159
159
switch ( name . kind ) {
160
160
case SyntaxKind . ObjectBindingPattern :
161
161
case SyntaxKind . ArrayBindingPattern : {
162
162
// `const [a, b] = require("c").d` --> `import { d } from "c"; const [a, b] = d;`
163
163
const tmp = makeUniqueName ( propertyName , identifiers ) ;
164
164
return [
165
- makeSingleImport ( tmp , propertyName , moduleSpecifier ) ,
165
+ makeSingleImport ( tmp , propertyName , moduleSpecifier , preferences ) ,
166
166
makeConst ( /*modifiers*/ undefined , name , createIdentifier ( tmp ) ) ,
167
167
] ;
168
168
}
169
169
case SyntaxKind . Identifier :
170
170
// `const a = require("b").c` --> `import { c as a } from "./b";
171
- return [ makeSingleImport ( name . text , propertyName , moduleSpecifier ) ] ;
171
+ return [ makeSingleImport ( name . text , propertyName , moduleSpecifier , preferences ) ] ;
172
172
default :
173
173
return Debug . assertNever ( name ) ;
174
174
}
@@ -340,6 +340,7 @@ namespace ts.codefix {
340
340
checker : TypeChecker ,
341
341
identifiers : Identifiers ,
342
342
target : ScriptTarget ,
343
+ preferences : UserPreferences ,
343
344
) : ReadonlyArray < Node > {
344
345
switch ( name . kind ) {
345
346
case SyntaxKind . ObjectBindingPattern : {
@@ -348,7 +349,7 @@ namespace ts.codefix {
348
349
? undefined
349
350
: makeImportSpecifier ( e . propertyName && ( e . propertyName as Identifier ) . text , e . name . text ) ) ; // tslint:disable-line no-unnecessary-type-assertion (TODO: GH#18217)
350
351
if ( importSpecifiers ) {
351
- return [ makeImport ( /*name*/ undefined , importSpecifiers , moduleSpecifier ) ] ;
352
+ return [ makeImport ( /*name*/ undefined , importSpecifiers , moduleSpecifier , preferences ) ] ;
352
353
}
353
354
}
354
355
// falls through -- object destructuring has an interesting pattern and must be a variable declaration
@@ -359,12 +360,12 @@ namespace ts.codefix {
359
360
*/
360
361
const tmp = makeUniqueName ( moduleSpecifierToValidIdentifier ( moduleSpecifier . text , target ) , identifiers ) ;
361
362
return [
362
- makeImport ( createIdentifier ( tmp ) , /*namedImports*/ undefined , moduleSpecifier ) ,
363
+ makeImport ( createIdentifier ( tmp ) , /*namedImports*/ undefined , moduleSpecifier , preferences ) ,
363
364
makeConst ( /*modifiers*/ undefined , getSynthesizedDeepClone ( name ) , createIdentifier ( tmp ) ) ,
364
365
] ;
365
366
}
366
367
case SyntaxKind . Identifier :
367
- return convertSingleIdentifierImport ( file , name , moduleSpecifier , changes , checker , identifiers ) ;
368
+ return convertSingleIdentifierImport ( file , name , moduleSpecifier , changes , checker , identifiers , preferences ) ;
368
369
default :
369
370
return Debug . assertNever ( name ) ;
370
371
}
@@ -374,7 +375,7 @@ namespace ts.codefix {
374
375
* Convert `import x = require("x").`
375
376
* Also converts uses like `x.y()` to `y()` and uses a named import.
376
377
*/
377
- function convertSingleIdentifierImport ( file : SourceFile , name : Identifier , moduleSpecifier : StringLiteralLike , changes : textChanges . ChangeTracker , checker : TypeChecker , identifiers : Identifiers ) : ReadonlyArray < Node > {
378
+ function convertSingleIdentifierImport ( file : SourceFile , name : Identifier , moduleSpecifier : StringLiteralLike , changes : textChanges . ChangeTracker , checker : TypeChecker , identifiers : Identifiers , preferences : UserPreferences ) : ReadonlyArray < Node > {
378
379
const nameSymbol = checker . getSymbolAtLocation ( name ) ;
379
380
// Maps from module property name to name actually used. (The same if there isn't shadowing.)
380
381
const namedBindingsNames = createMap < string > ( ) ;
@@ -409,7 +410,7 @@ namespace ts.codefix {
409
410
// If it was unused, ensure that we at least import *something*.
410
411
needDefaultImport = true ;
411
412
}
412
- return [ makeImport ( needDefaultImport ? getSynthesizedDeepClone ( name ) : undefined , namedBindings , moduleSpecifier ) ] ;
413
+ return [ makeImport ( needDefaultImport ? getSynthesizedDeepClone ( name ) : undefined , namedBindings , moduleSpecifier , preferences ) ] ;
413
414
}
414
415
415
416
// Identifiers helpers
@@ -481,10 +482,10 @@ namespace ts.codefix {
481
482
getSynthesizedDeepClones ( cls . members ) ) ;
482
483
}
483
484
484
- function makeSingleImport ( localName : string , propertyName : string , moduleSpecifier : StringLiteralLike ) : ImportDeclaration {
485
+ function makeSingleImport ( localName : string , propertyName : string , moduleSpecifier : StringLiteralLike , preferences : UserPreferences ) : ImportDeclaration {
485
486
return propertyName === "default"
486
- ? makeImport ( createIdentifier ( localName ) , /*namedImports*/ undefined , moduleSpecifier )
487
- : makeImport ( /*name*/ undefined , [ makeImportSpecifier ( propertyName , localName ) ] , moduleSpecifier ) ;
487
+ ? makeImport ( createIdentifier ( localName ) , /*namedImports*/ undefined , moduleSpecifier , preferences )
488
+ : makeImport ( /*name*/ undefined , [ makeImportSpecifier ( propertyName , localName ) ] , moduleSpecifier , preferences ) ;
488
489
}
489
490
490
491
function makeImportSpecifier ( propertyName : string | undefined , name : string ) : ImportSpecifier {
0 commit comments