@@ -187,7 +187,7 @@ namespace ts.refactor {
187
187
}
188
188
case SyntaxKind . BinaryExpression : {
189
189
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 ) ;
191
191
}
192
192
}
193
193
}
@@ -248,6 +248,7 @@ namespace ts.refactor {
248
248
249
249
function convertAssignment (
250
250
sourceFile : SourceFile ,
251
+ checker : TypeChecker ,
251
252
statement : ExpressionStatement ,
252
253
left : Expression ,
253
254
right : Expression ,
@@ -268,7 +269,7 @@ namespace ts.refactor {
268
269
let newNodes = isObjectLiteralExpression ( right ) ? tryChangeModuleExportsObject ( right ) : undefined ;
269
270
let changedToDefaultExport = false ;
270
271
if ( ! newNodes ) {
271
- ( [ newNodes , changedToDefaultExport ] = convertModuleExportsToExportDefault ( right ) ) ;
272
+ ( [ newNodes , changedToDefaultExport ] = convertModuleExportsToExportDefault ( right , checker ) ) ;
272
273
}
273
274
changes . replaceNodeWithNodes ( sourceFile , statement , newNodes , { nodeSeparator : newLine , useNonAdjustedEndPosition : true } ) ;
274
275
return changedToDefaultExport ;
@@ -336,7 +337,7 @@ namespace ts.refactor {
336
337
}
337
338
}
338
339
339
- function convertModuleExportsToExportDefault ( exported : Expression ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
340
+ function convertModuleExportsToExportDefault ( exported : Expression , checker : TypeChecker ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
340
341
const modifiers = [ createToken ( SyntaxKind . ExportKeyword ) , createToken ( SyntaxKind . DefaultKeyword ) ] ;
341
342
switch ( exported . kind ) {
342
343
case SyntaxKind . FunctionExpression :
@@ -351,14 +352,8 @@ namespace ts.refactor {
351
352
return [ [ classExpressionToDeclaration ( cls . name && cls . name . text , modifiers , cls ) ] , true ] ;
352
353
}
353
354
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 ) ;
362
357
}
363
358
// falls through
364
359
default :
@@ -367,6 +362,25 @@ namespace ts.refactor {
367
362
}
368
363
}
369
364
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
+
370
384
function convertExportsDotXEquals ( name : string | undefined , exported : Expression ) : Statement {
371
385
const modifiers = [ createToken ( SyntaxKind . ExportKeyword ) ] ;
372
386
switch ( exported . kind ) {
0 commit comments