From 57a50f4b9d9ef0a48a1639ff179397de1b9f47ed Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 6 Feb 2018 10:19:14 -0800 Subject: [PATCH] fixInvalidImportSyntax: Preserve comment (#21684) --- .../codefixes/fixInvalidImportSyntax.ts | 37 +++++++------------ .../fourslash/codeFixCalledES2015Import11.ts | 4 +- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/services/codefixes/fixInvalidImportSyntax.ts b/src/services/codefixes/fixInvalidImportSyntax.ts index f98f1eaea7c..e6f18cf5bd7 100644 --- a/src/services/codefixes/fixInvalidImportSyntax.ts +++ b/src/services/codefixes/fixInvalidImportSyntax.ts @@ -26,40 +26,35 @@ namespace ts.codefix { const variations: CodeAction[] = []; // import Bluebird from "bluebird"; - const replacement = createImportDeclaration( + variations.push(createAction(context, sourceFile, node, createImportDeclaration( /*decorators*/ undefined, /*modifiers*/ undefined, createImportClause(namespace.name, /*namedBindings*/ undefined), node.moduleSpecifier - ); - const changeTracker = textChanges.ChangeTracker.fromContext(context); - changeTracker.replaceNode(sourceFile, node, replacement, { useNonAdjustedEndPosition: true }); - const changes = changeTracker.getChanges(); - variations.push({ - description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Replace_import_with_0), [changes[0].textChanges[0].newText]), - changes - }); + ))); if (getEmitModuleKind(opts) === ModuleKind.CommonJS) { // import Bluebird = require("bluebird"); - const replacement = createImportEqualsDeclaration( + variations.push(createAction(context, sourceFile, node, createImportEqualsDeclaration( /*decorators*/ undefined, /*modifiers*/ undefined, namespace.name, createExternalModuleReference(node.moduleSpecifier) - ); - const changeTracker = textChanges.ChangeTracker.fromContext(context); - changeTracker.replaceNode(sourceFile, node, replacement, { useNonAdjustedEndPosition: true }); - const changes = changeTracker.getChanges(); - variations.push({ - description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Replace_import_with_0), [changes[0].textChanges[0].newText]), - changes - }); + ))); } return variations; } + function createAction(context: CodeFixContext, sourceFile: SourceFile, node: Node, replacement: Node): CodeAction { + // TODO: GH#21246 Should be able to use `replaceNode`, but be sure to preserve comments (see `codeFixCalledES2015Import11.ts`) + const changes = textChanges.ChangeTracker.with(context, t => t.replaceRange(sourceFile, { pos: node.getStart(), end: node.end }, replacement)); + return { + description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Replace_import_with_0), [changes[0].textChanges[0].newText]), + changes, + }; + } + registerCodeFix({ errorCodes: [ 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 { if (!isImportCall(relatedImport)) { addRange(fixes, getCodeFixesForImportDeclaration(context, relatedImport)); } - const propertyAccess = createPropertyAccess(expr, "default"); - const changeTracker = textChanges.ChangeTracker.fromContext(context); - changeTracker.replaceNode(sourceFile, expr, propertyAccess, {}); - const changes = changeTracker.getChanges(); fixes.push({ description: getLocaleSpecificMessage(Diagnostics.Use_synthetic_default_member), - changes + changes: textChanges.ChangeTracker.with(context, t => t.replaceNode(sourceFile, expr, createPropertyAccess(expr, "default"), {})), }); return fixes; } diff --git a/tests/cases/fourslash/codeFixCalledES2015Import11.ts b/tests/cases/fourslash/codeFixCalledES2015Import11.ts index 14b02fa38bc..af0d5c9ed30 100644 --- a/tests/cases/fourslash/codeFixCalledES2015Import11.ts +++ b/tests/cases/fourslash/codeFixCalledES2015Import11.ts @@ -6,13 +6,15 @@ ////export = foo; // @Filename: index.ts +////// Comment ////import * as foo from "./foo"; ////[|foo()|]; goTo.file(1); verify.codeFix({ description: `Replace import with 'import foo from "./foo";'.`, - newFileContent: `import foo from "./foo"; + newFileContent: `// Comment +import foo from "./foo"; foo();`, index: 0, });