Skip to content

Commit 57a50f4

Browse files
author
Andy
authored
fixInvalidImportSyntax: Preserve comment (#21684)
1 parent 044fb53 commit 57a50f4

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/services/codefixes/fixInvalidImportSyntax.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/cases/fourslash/codeFixCalledES2015Import11.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
////export = foo;
77

88
// @Filename: index.ts
9+
////// Comment
910
////import * as foo from "./foo";
1011
////[|foo()|];
1112

1213
goTo.file(1);
1314
verify.codeFix({
1415
description: `Replace import with 'import foo from "./foo";'.`,
15-
newFileContent: `import foo from "./foo";
16+
newFileContent: `// Comment
17+
import foo from "./foo";
1618
foo();`,
1719
index: 0,
1820
});

0 commit comments

Comments
 (0)