fix(39410): don't remove variables with type definition during converting named export to default (#39505)

This commit is contained in:
Alexander T 2020-09-03 03:00:43 +03:00 committed by GitHub
parent 3a75838cb7
commit 38cedc5b5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -110,10 +110,11 @@ namespace ts.refactor {
changes.insertNodeAfter(exportingSourceFile, exportKeyword, factory.createToken(SyntaxKind.DefaultKeyword));
break;
case SyntaxKind.VariableStatement:
// If 'x' isn't used in this file, `export const x = 0;` --> `export default 0;`
if (!FindAllReferences.Core.isSymbolReferencedInFile(exportName, checker, exportingSourceFile)) {
// If 'x' isn't used in this file and doesn't have type definition, `export const x = 0;` --> `export default 0;`
const decl = first(exportNode.declarationList.declarations);
if (!FindAllReferences.Core.isSymbolReferencedInFile(exportName, checker, exportingSourceFile) && !decl.type) {
// We checked in `getInfo` that an initializer exists.
changes.replaceNode(exportingSourceFile, exportNode, factory.createExportDefault(Debug.checkDefined(first(exportNode.declarationList.declarations).initializer, "Initializer was previously known to be present")));
changes.replaceNode(exportingSourceFile, exportNode, factory.createExportDefault(Debug.checkDefined(decl.initializer, "Initializer was previously known to be present")));
break;
}
// falls through

View File

@ -28,6 +28,9 @@
////export const x = 0;
////x;
// @Filename: /var_with_type.ts
////export const fn: (n: number) => number = (n) => 1;
const tests: { [fileName: string]: string | undefined } = {
fn: `export default function f() {}`,
@ -59,6 +62,11 @@ export default T;
`const x = 0;
export default x;
x;`,
var_with_type:
`const fn: (n: number) => number = (n) => 1;
export default fn;
`,
};
for (const name in tests) {