@@ -20,16 +20,15 @@ namespace ts.codefix {
20
20
21
21
function fixImportOfModuleExports ( importingFile : SourceFile , exportingFile : SourceFile , changes : textChanges . ChangeTracker ) {
22
22
for ( const moduleSpecifier of importingFile . imports ) {
23
- const { text } = moduleSpecifier ;
24
- const imported = getResolvedModule ( importingFile , text ) ;
23
+ const imported = getResolvedModule ( importingFile , moduleSpecifier . text ) ;
25
24
if ( ! imported || imported . resolvedFileName !== exportingFile . fileName ) {
26
25
continue ;
27
26
}
28
27
29
28
const importNode = importFromModuleSpecifier ( moduleSpecifier ) ;
30
29
switch ( importNode . kind ) {
31
30
case SyntaxKind . ImportEqualsDeclaration :
32
- changes . replaceNode ( importingFile , importNode , makeImport ( importNode . name , /*namedImports*/ undefined , text ) ) ;
31
+ changes . replaceNode ( importingFile , importNode , makeImport ( importNode . name , /*namedImports*/ undefined , moduleSpecifier ) ) ;
33
32
break ;
34
33
case SyntaxKind . CallExpression :
35
34
if ( isRequireCall ( importNode , /*checkArgumentIsStringLiteralLike*/ false ) ) {
@@ -111,7 +110,7 @@ namespace ts.codefix {
111
110
case SyntaxKind . CallExpression : {
112
111
if ( isRequireCall ( expression , /*checkArgumentIsStringLiteralLike*/ true ) ) {
113
112
// For side-effecting require() call, just make a side-effecting import.
114
- changes . replaceNode ( sourceFile , statement , makeImport ( /*name*/ undefined , /*namedImports*/ undefined , expression . arguments [ 0 ] . text ) ) ;
113
+ changes . replaceNode ( sourceFile , statement , makeImport ( /*name*/ undefined , /*namedImports*/ undefined , expression . arguments [ 0 ] ) ) ;
115
114
}
116
115
return false ;
117
116
}
@@ -139,11 +138,11 @@ namespace ts.codefix {
139
138
}
140
139
if ( isRequireCall ( initializer , /*checkArgumentIsStringLiteralLike*/ true ) ) {
141
140
foundImport = true ;
142
- return convertSingleImport ( sourceFile , name , initializer . arguments [ 0 ] . text , changes , checker , identifiers , target ) ;
141
+ return convertSingleImport ( sourceFile , name , initializer . arguments [ 0 ] , changes , checker , identifiers , target ) ;
143
142
}
144
143
else if ( isPropertyAccessExpression ( initializer ) && isRequireCall ( initializer . expression , /*checkArgumentIsStringLiteralLike*/ true ) ) {
145
144
foundImport = true ;
146
- return convertPropertyAccessImport ( name , initializer . name . text , initializer . expression . arguments [ 0 ] . text , identifiers ) ;
145
+ return convertPropertyAccessImport ( name , initializer . name . text , initializer . expression . arguments [ 0 ] , identifiers ) ;
147
146
}
148
147
else {
149
148
// Move it out to its own variable statement.
@@ -157,7 +156,7 @@ namespace ts.codefix {
157
156
}
158
157
159
158
/** Converts `const name = require("moduleSpecifier").propertyName` */
160
- function convertPropertyAccessImport ( name : BindingName , propertyName : string , moduleSpecifier : string , identifiers : Identifiers ) : ReadonlyArray < Node > {
159
+ function convertPropertyAccessImport ( name : BindingName , propertyName : string , moduleSpecifier : StringLiteralLike , identifiers : Identifiers ) : ReadonlyArray < Node > {
161
160
switch ( name . kind ) {
162
161
case SyntaxKind . ObjectBindingPattern :
163
162
case SyntaxKind . ArrayBindingPattern : {
@@ -340,7 +339,7 @@ namespace ts.codefix {
340
339
function convertSingleImport (
341
340
file : SourceFile ,
342
341
name : BindingName ,
343
- moduleSpecifier : string ,
342
+ moduleSpecifier : StringLiteralLike ,
344
343
changes : textChanges . ChangeTracker ,
345
344
checker : TypeChecker ,
346
345
identifiers : Identifiers ,
@@ -362,7 +361,7 @@ namespace ts.codefix {
362
361
import x from "x";
363
362
const [a, b, c] = x;
364
363
*/
365
- const tmp = makeUniqueName ( moduleSpecifierToValidIdentifier ( moduleSpecifier , target ) , identifiers ) ;
364
+ const tmp = makeUniqueName ( moduleSpecifierToValidIdentifier ( moduleSpecifier . text , target ) , identifiers ) ;
366
365
return [
367
366
makeImport ( createIdentifier ( tmp ) , /*namedImports*/ undefined , moduleSpecifier ) ,
368
367
makeConst ( /*modifiers*/ undefined , getSynthesizedDeepClone ( name ) , createIdentifier ( tmp ) ) ,
@@ -379,7 +378,7 @@ namespace ts.codefix {
379
378
* Convert `import x = require("x").`
380
379
* Also converts uses like `x.y()` to `y()` and uses a named import.
381
380
*/
382
- function convertSingleIdentifierImport ( file : SourceFile , name : Identifier , moduleSpecifier : string , changes : textChanges . ChangeTracker , checker : TypeChecker , identifiers : Identifiers ) : ReadonlyArray < Node > {
381
+ function convertSingleIdentifierImport ( file : SourceFile , name : Identifier , moduleSpecifier : StringLiteralLike , changes : textChanges . ChangeTracker , checker : TypeChecker , identifiers : Identifiers ) : ReadonlyArray < Node > {
383
382
const nameSymbol = checker . getSymbolAtLocation ( name ) ;
384
383
// Maps from module property name to name actually used. (The same if there isn't shadowing.)
385
384
const namedBindingsNames = createMap < string > ( ) ;
@@ -486,14 +485,14 @@ namespace ts.codefix {
486
485
getSynthesizedDeepClones ( cls . members ) ) ;
487
486
}
488
487
489
- function makeSingleImport ( localName : string , propertyName : string , moduleSpecifier : string ) : ImportDeclaration {
488
+ function makeSingleImport ( localName : string , propertyName : string , moduleSpecifier : StringLiteralLike ) : ImportDeclaration {
490
489
return propertyName === "default"
491
490
? makeImport ( createIdentifier ( localName ) , /*namedImports*/ undefined , moduleSpecifier )
492
491
: makeImport ( /*name*/ undefined , [ makeImportSpecifier ( propertyName , localName ) ] , moduleSpecifier ) ;
493
492
}
494
493
495
- function makeImport ( name : Identifier | undefined , namedImports : ReadonlyArray < ImportSpecifier > | undefined , moduleSpecifier : string ) : ImportDeclaration {
496
- return makeImportDeclaration ( name , namedImports , createLiteral ( moduleSpecifier ) ) ;
494
+ function makeImport ( name : Identifier | undefined , namedImports : ReadonlyArray < ImportSpecifier > | undefined , moduleSpecifier : StringLiteralLike ) : ImportDeclaration {
495
+ return makeImportDeclaration ( name , namedImports , moduleSpecifier ) ;
497
496
}
498
497
499
498
export function makeImportDeclaration ( name : Identifier , namedImports : ReadonlyArray < ImportSpecifier > | undefined , moduleSpecifier : Expression ) {
0 commit comments