@@ -187,7 +187,7 @@ namespace ts.refactor {
187187 }
188188 case SyntaxKind . BinaryExpression : {
189189 const { left, operatorToken, right } = expression as BinaryExpression ;
190- return operatorToken . kind === SyntaxKind . EqualsToken && convertAssignment ( sourceFile , statement as ExpressionStatement , left , right , changes , newLine , exports ) ;
190+ return operatorToken . kind === SyntaxKind . EqualsToken && convertAssignment ( sourceFile , checker , statement as ExpressionStatement , left , right , changes , newLine , exports ) ;
191191 }
192192 }
193193 }
@@ -248,6 +248,7 @@ namespace ts.refactor {
248248
249249 function convertAssignment (
250250 sourceFile : SourceFile ,
251+ checker : TypeChecker ,
251252 statement : ExpressionStatement ,
252253 left : Expression ,
253254 right : Expression ,
@@ -268,7 +269,7 @@ namespace ts.refactor {
268269 let newNodes = isObjectLiteralExpression ( right ) ? tryChangeModuleExportsObject ( right ) : undefined ;
269270 let changedToDefaultExport = false ;
270271 if ( ! newNodes ) {
271- ( [ newNodes , changedToDefaultExport ] = convertModuleExportsToExportDefault ( right ) ) ;
272+ ( [ newNodes , changedToDefaultExport ] = convertModuleExportsToExportDefault ( right , checker ) ) ;
272273 }
273274 changes . replaceNodeWithNodes ( sourceFile , statement , newNodes , { nodeSeparator : newLine , useNonAdjustedEndPosition : true } ) ;
274275 return changedToDefaultExport ;
@@ -336,7 +337,7 @@ namespace ts.refactor {
336337 }
337338 }
338339
339- function convertModuleExportsToExportDefault ( exported : Expression ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
340+ function convertModuleExportsToExportDefault ( exported : Expression , checker : TypeChecker ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
340341 const modifiers = [ createToken ( SyntaxKind . ExportKeyword ) , createToken ( SyntaxKind . DefaultKeyword ) ] ;
341342 switch ( exported . kind ) {
342343 case SyntaxKind . FunctionExpression :
@@ -351,14 +352,8 @@ namespace ts.refactor {
351352 return [ [ classExpressionToDeclaration ( cls . name && cls . name . text , modifiers , cls ) ] , true ] ;
352353 }
353354 case SyntaxKind . CallExpression :
354- if ( isRequireCall ( exported , /*checkArguementIsStringLiteral*/ true ) ) {
355- // `module.exports = require("x");` ==> `export * from "x"; export { default } from "x";`
356- const moduleSpecifier = exported . arguments [ 0 ] . text ;
357- const newNodes = [
358- makeExportDeclaration ( /*exportClause*/ undefined , moduleSpecifier ) ,
359- makeExportDeclaration ( [ createExportSpecifier ( /*propertyName*/ undefined , "default" ) ] , moduleSpecifier ) ,
360- ] ;
361- return [ newNodes , false ] ;
355+ if ( isRequireCall ( exported , /*checkArgumentIsStringLiteral*/ true ) ) {
356+ return convertReExportAll ( exported . arguments [ 0 ] , checker ) ;
362357 }
363358 // falls through
364359 default :
@@ -367,6 +362,25 @@ namespace ts.refactor {
367362 }
368363 }
369364
365+ function convertReExportAll ( reExported : StringLiteralLike , checker : TypeChecker ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
366+ // `module.exports = require("x");` ==> `export * from "x"; export { default } from "x";`
367+ const moduleSpecifier = reExported . text ;
368+ const moduleSymbol = checker . getSymbolAtLocation ( reExported ) ;
369+ const exports = moduleSymbol ? moduleSymbol . exports : emptyUnderscoreEscapedMap ;
370+ return exports . has ( "export=" as __String )
371+ ? [ [ reExportDefault ( moduleSpecifier ) ] , true ]
372+ : ! exports . has ( "default" as __String )
373+ ? [ [ reExportStar ( moduleSpecifier ) ] , false ]
374+ // If there's some non-default export, must include both `export *` and `export default`.
375+ : exports . size > 1 ? [ [ reExportStar ( moduleSpecifier ) , reExportDefault ( moduleSpecifier ) ] , true ] : [ [ reExportDefault ( moduleSpecifier ) ] , true ] ;
376+ }
377+ function reExportStar ( moduleSpecifier : string ) : ExportDeclaration {
378+ return makeExportDeclaration ( /*exportClause*/ undefined , moduleSpecifier ) ;
379+ }
380+ function reExportDefault ( moduleSpecifier : string ) : ExportDeclaration {
381+ return makeExportDeclaration ( [ createExportSpecifier ( /*propertyName*/ undefined , "default" ) ] , moduleSpecifier ) ;
382+ }
383+
370384 function convertExportsDotXEquals ( name : string | undefined , exported : Expression ) : Statement {
371385 const modifiers = [ createToken ( SyntaxKind . ExportKeyword ) ] ;
372386 switch ( exported . kind ) {
0 commit comments