@@ -26,40 +26,35 @@ namespace ts.codefix {
2626 const variations : CodeAction [ ] = [ ] ;
2727
2828 // import Bluebird from "bluebird";
29- const replacement = createImportDeclaration (
29+ variations . push ( createAction ( context , sourceFile , node , createImportDeclaration (
3030 /*decorators*/ undefined ,
3131 /*modifiers*/ undefined ,
3232 createImportClause ( namespace . name , /*namedBindings*/ undefined ) ,
3333 node . moduleSpecifier
34- ) ;
35- const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
36- changeTracker . replaceNode ( sourceFile , node , replacement , { useNonAdjustedEndPosition : true } ) ;
37- const changes = changeTracker . getChanges ( ) ;
38- variations . push ( {
39- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Replace_import_with_0 ) , [ changes [ 0 ] . textChanges [ 0 ] . newText ] ) ,
40- changes
41- } ) ;
34+ ) ) ) ;
4235
4336 if ( getEmitModuleKind ( opts ) === ModuleKind . CommonJS ) {
4437 // import Bluebird = require("bluebird");
45- const replacement = createImportEqualsDeclaration (
38+ variations . push ( createAction ( context , sourceFile , node , createImportEqualsDeclaration (
4639 /*decorators*/ undefined ,
4740 /*modifiers*/ undefined ,
4841 namespace . name ,
4942 createExternalModuleReference ( node . moduleSpecifier )
50- ) ;
51- const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
52- changeTracker . replaceNode ( sourceFile , node , replacement , { useNonAdjustedEndPosition : true } ) ;
53- const changes = changeTracker . getChanges ( ) ;
54- variations . push ( {
55- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Replace_import_with_0 ) , [ changes [ 0 ] . textChanges [ 0 ] . newText ] ) ,
56- changes
57- } ) ;
43+ ) ) ) ;
5844 }
5945
6046 return variations ;
6147 }
6248
49+ function createAction ( context : CodeFixContext , sourceFile : SourceFile , node : Node , replacement : Node ) : CodeAction {
50+ // TODO: GH#21246 Should be able to use `replaceNode`, but be sure to preserve comments (see `codeFixCalledES2015Import11.ts`)
51+ const changes = textChanges . ChangeTracker . with ( context , t => t . replaceRange ( sourceFile , { pos : node . getStart ( ) , end : node . end } , replacement ) ) ;
52+ return {
53+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Replace_import_with_0 ) , [ changes [ 0 ] . textChanges [ 0 ] . newText ] ) ,
54+ changes,
55+ } ;
56+ }
57+
6358 registerCodeFix ( {
6459 errorCodes : [
6560 Diagnostics . Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures . code ,
@@ -85,13 +80,9 @@ namespace ts.codefix {
8580 if ( ! isImportCall ( relatedImport ) ) {
8681 addRange ( fixes , getCodeFixesForImportDeclaration ( context , relatedImport ) ) ;
8782 }
88- const propertyAccess = createPropertyAccess ( expr , "default" ) ;
89- const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
90- changeTracker . replaceNode ( sourceFile , expr , propertyAccess , { } ) ;
91- const changes = changeTracker . getChanges ( ) ;
9283 fixes . push ( {
9384 description : getLocaleSpecificMessage ( Diagnostics . Use_synthetic_default_member ) ,
94- changes
85+ changes : textChanges . ChangeTracker . with ( context , t => t . replaceNode ( sourceFile , expr , createPropertyAccess ( expr , "default" ) , { } ) ) ,
9586 } ) ;
9687 return fixes ;
9788 }
0 commit comments