Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 38cedc5

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

2 files changed

+12-3Lines changed: 12 additions & 3 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/services/refactors/convertExport.ts‎

Copy file name to clipboardExpand all lines: src/services/refactors/convertExport.ts
+4-3Lines 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
Collapse file

‎tests/cases/fourslash/refactorConvertExport_exportNodeKinds.ts‎

Copy file name to clipboardExpand all lines: tests/cases/fourslash/refactorConvertExport_exportNodeKinds.ts
+8Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.