diff --git a/src/services/codefixes/convertToTypeOnlyExport.ts b/src/services/codefixes/convertToTypeOnlyExport.ts index cc36a7e7a86..117aca7b983 100644 --- a/src/services/codefixes/convertToTypeOnlyExport.ts +++ b/src/services/codefixes/convertToTypeOnlyExport.ts @@ -15,7 +15,7 @@ namespace ts.codefix { const fixedExportDeclarations = new Map(); return codeFixAll(context, errorCodes, (changes, diag) => { const exportSpecifier = getExportSpecifierForDiagnosticSpan(diag, context.sourceFile); - if (exportSpecifier && !addToSeen(fixedExportDeclarations, getNodeId(exportSpecifier.parent.parent))) { + if (exportSpecifier && addToSeen(fixedExportDeclarations, getNodeId(exportSpecifier.parent.parent))) { fixSingleExportDeclaration(changes, exportSpecifier, context); } }); @@ -35,16 +35,7 @@ namespace ts.codefix { const exportDeclaration = exportClause.parent; const typeExportSpecifiers = getTypeExportSpecifiers(exportSpecifier, context); if (typeExportSpecifiers.length === exportClause.elements.length) { - changes.replaceNode( - context.sourceFile, - exportDeclaration, - factory.updateExportDeclaration( - exportDeclaration, - exportDeclaration.decorators, - exportDeclaration.modifiers, - /*isTypeOnly*/ true, - exportClause, - exportDeclaration.moduleSpecifier)); + changes.insertModifierBefore(context.sourceFile, SyntaxKind.TypeKeyword, exportClause); } else { const valueExportDeclaration = factory.updateExportDeclaration( @@ -61,7 +52,10 @@ namespace ts.codefix { factory.createNamedExports(typeExportSpecifiers), exportDeclaration.moduleSpecifier); - changes.replaceNode(context.sourceFile, exportDeclaration, valueExportDeclaration); + changes.replaceNode(context.sourceFile, exportDeclaration, valueExportDeclaration, { + leadingTriviaOption: textChanges.LeadingTriviaOption.IncludeAll, + trailingTriviaOption: textChanges.TrailingTriviaOption.Exclude + }); changes.insertNodeAfter(context.sourceFile, exportDeclaration, typeExportDeclaration); } } diff --git a/tests/cases/fourslash/codeFixConvertToTypeOnlyExport1.ts b/tests/cases/fourslash/codeFixConvertToTypeOnlyExport1.ts index 00c9083f377..df2d1be4279 100644 --- a/tests/cases/fourslash/codeFixConvertToTypeOnlyExport1.ts +++ b/tests/cases/fourslash/codeFixConvertToTypeOnlyExport1.ts @@ -7,6 +7,7 @@ ////export type B = {}; // @Filename: /b.ts +/////* Comment */ ////export { A, B } from './a'; goTo.file("/b.ts"); @@ -14,5 +15,7 @@ verify.codeFix({ index: 0, description: ts.Diagnostics.Convert_to_type_only_export.message, errorCode: ts.Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type.code, - newFileContent: "export type { A, B } from './a';" + newFileContent: +`/* Comment */ +export type { A, B } from './a';` }); diff --git a/tests/cases/fourslash/codeFixConvertToTypeOnlyExport3.ts b/tests/cases/fourslash/codeFixConvertToTypeOnlyExport3.ts index f90e0657d51..c3a8334a379 100644 --- a/tests/cases/fourslash/codeFixConvertToTypeOnlyExport3.ts +++ b/tests/cases/fourslash/codeFixConvertToTypeOnlyExport3.ts @@ -11,19 +11,34 @@ ////export type T3 = {}; ////export const V2 = {}; ////export type T4 = {}; +////export type T5 = {}; // @Filename: /c.ts -////export { T1, V1, T2 } from './a'; -////export { T3, V2, T4 } from './b'; +////export type T6 = {}; +////export type T7 = {}; -goTo.file("/c.ts"); +// @Filename: /d.ts +/////* Test comment */ +////export { T1, V1, T2 } from './a'; +////export { T3, V2, T4, T5 } from './b'; +////// TODO: fix messy formatting +////export { +//// T6 // need to export this +//// , T7, /* and this */ } from "./c"; + + +goTo.file("/d.ts"); verify.codeFixAll({ fixAllDescription: ts.Diagnostics.Convert_all_re_exported_types_to_type_only_exports.message, fixId: "convertToTypeOnlyExport", newFileContent: -`export { V1 } from './a'; +`/* Test comment */ +export { V1 } from './a'; export type { T1, T2 } from './a'; export { V2 } from './b'; -export type { T3, T4 } from './b'; -` +export type { T3, T4, T5 } from './b'; +// TODO: fix messy formatting +export type { + T6 // need to export this + , T7, /* and this */ } from "./c";` });