@@ -26,40 +26,35 @@ namespace ts.codefix {
26
26
const variations : CodeAction [ ] = [ ] ;
27
27
28
28
// import Bluebird from "bluebird";
29
- const replacement = createImportDeclaration (
29
+ variations . push ( createAction ( context , sourceFile , node , createImportDeclaration (
30
30
/*decorators*/ undefined ,
31
31
/*modifiers*/ undefined ,
32
32
createImportClause ( namespace . name , /*namedBindings*/ undefined ) ,
33
33
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
+ ) ) ) ;
42
35
43
36
if ( getEmitModuleKind ( opts ) === ModuleKind . CommonJS ) {
44
37
// import Bluebird = require("bluebird");
45
- const replacement = createImportEqualsDeclaration (
38
+ variations . push ( createAction ( context , sourceFile , node , createImportEqualsDeclaration (
46
39
/*decorators*/ undefined ,
47
40
/*modifiers*/ undefined ,
48
41
namespace . name ,
49
42
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
+ ) ) ) ;
58
44
}
59
45
60
46
return variations ;
61
47
}
62
48
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
+
63
58
registerCodeFix ( {
64
59
errorCodes : [
65
60
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 {
85
80
if ( ! isImportCall ( relatedImport ) ) {
86
81
addRange ( fixes , getCodeFixesForImportDeclaration ( context , relatedImport ) ) ;
87
82
}
88
- const propertyAccess = createPropertyAccess ( expr , "default" ) ;
89
- const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
90
- changeTracker . replaceNode ( sourceFile , expr , propertyAccess , { } ) ;
91
- const changes = changeTracker . getChanges ( ) ;
92
83
fixes . push ( {
93
84
description : getLocaleSpecificMessage ( Diagnostics . Use_synthetic_default_member ) ,
94
- changes
85
+ changes : textChanges . ChangeTracker . with ( context , t => t . replaceNode ( sourceFile , expr , createPropertyAccess ( expr , "default" ) , { } ) ) ,
95
86
} ) ;
96
87
return fixes ;
97
88
}
0 commit comments