Skip to content

Commit 38cedc5

Browse files
authored
fix(39410): don't remove variables with type definition during converting named export to default (microsoft#39505)
1 parent 3a75838 commit 38cedc5

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/services/refactors/convertExport.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ namespace ts.refactor {
110110
changes.insertNodeAfter(exportingSourceFile, exportKeyword, factory.createToken(SyntaxKind.DefaultKeyword));
111111
break;
112112
case SyntaxKind.VariableStatement:
113-
// If 'x' isn't used in this file, `export const x = 0;` --> `export default 0;`
114-
if (!FindAllReferences.Core.isSymbolReferencedInFile(exportName, checker, exportingSourceFile)) {
113+
// If 'x' isn't used in this file and doesn't have type definition, `export const x = 0;` --> `export default 0;`
114+
const decl = first(exportNode.declarationList.declarations);
115+
if (!FindAllReferences.Core.isSymbolReferencedInFile(exportName, checker, exportingSourceFile) && !decl.type) {
115116
// We checked in `getInfo` that an initializer exists.
116-
changes.replaceNode(exportingSourceFile, exportNode, factory.createExportDefault(Debug.checkDefined(first(exportNode.declarationList.declarations).initializer, "Initializer was previously known to be present")));
117+
changes.replaceNode(exportingSourceFile, exportNode, factory.createExportDefault(Debug.checkDefined(decl.initializer, "Initializer was previously known to be present")));
117118
break;
118119
}
119120
// falls through

tests/cases/fourslash/refactorConvertExport_exportNodeKinds.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
////export const x = 0;
2929
////x;
3030

31+
// @Filename: /var_with_type.ts
32+
////export const fn: (n: number) => number = (n) => 1;
33+
3134
const tests: { [fileName: string]: string | undefined } = {
3235
fn: `export default function f() {}`,
3336

@@ -59,6 +62,11 @@ export default T;
5962
`const x = 0;
6063
export default x;
6164
x;`,
65+
66+
var_with_type:
67+
`const fn: (n: number) => number = (n) => 1;
68+
export default fn;
69+
`,
6270
};
6371

6472
for (const name in tests) {

0 commit comments

Comments
 (0)