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
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