@@ -192,13 +192,17 @@ namespace ts.codefix {
192
192
changes . deleteNode ( sourceFile , assignment . parent ) ;
193
193
}
194
194
else {
195
- let newNodes = isObjectLiteralExpression ( right ) ? tryChangeModuleExportsObject ( right ) : undefined ;
196
- let changedToDefaultExport = false ;
197
- if ( ! newNodes ) {
198
- ( [ newNodes , changedToDefaultExport ] = convertModuleExportsToExportDefault ( right , checker ) ) ;
195
+ const replacement = isObjectLiteralExpression ( right ) ? tryChangeModuleExportsObject ( right )
196
+ : isRequireCall ( right , /*checkArgumentIsStringLiteralLike*/ true ) ? convertReExportAll ( right . arguments [ 0 ] , checker )
197
+ : undefined ;
198
+ if ( replacement ) {
199
+ changes . replaceNodeWithNodes ( sourceFile , assignment . parent , replacement [ 0 ] ) ;
200
+ return replacement [ 1 ] ;
201
+ }
202
+ else {
203
+ changes . replaceRangeWithText ( sourceFile , createTextRange ( left . getStart ( sourceFile ) , right . pos ) , "export default" ) ;
204
+ return true ;
199
205
}
200
- changes . replaceNodeWithNodes ( sourceFile , assignment . parent , newNodes ) ;
201
- return changedToDefaultExport ;
202
206
}
203
207
}
204
208
else if ( isExportsOrModuleExportsOrAlias ( sourceFile , left . expression ) ) {
@@ -212,8 +216,8 @@ namespace ts.codefix {
212
216
* Convert `module.exports = { ... }` to individual exports..
213
217
* We can't always do this if the module has interesting members -- then it will be a default export instead.
214
218
*/
215
- function tryChangeModuleExportsObject ( object : ObjectLiteralExpression ) : ReadonlyArray < Statement > | undefined {
216
- return mapAllOrFail ( object . properties , prop => {
219
+ function tryChangeModuleExportsObject ( object : ObjectLiteralExpression ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] | undefined {
220
+ const statements = mapAllOrFail ( object . properties , prop => {
217
221
switch ( prop . kind ) {
218
222
case SyntaxKind . GetAccessor :
219
223
case SyntaxKind . SetAccessor :
@@ -229,6 +233,7 @@ namespace ts.codefix {
229
233
Debug . assertNever ( prop ) ;
230
234
}
231
235
} ) ;
236
+ return statements && [ statements , true ] ;
232
237
}
233
238
234
239
function convertNamedExport (
@@ -256,31 +261,6 @@ namespace ts.codefix {
256
261
}
257
262
}
258
263
259
- function convertModuleExportsToExportDefault ( exported : Expression , checker : TypeChecker ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
260
- const modifiers = [ createToken ( SyntaxKind . ExportKeyword ) , createToken ( SyntaxKind . DefaultKeyword ) ] ;
261
- switch ( exported . kind ) {
262
- case SyntaxKind . FunctionExpression :
263
- case SyntaxKind . ArrowFunction : {
264
- // `module.exports = function f() {}` --> `export default function f() {}`
265
- const fn = exported as FunctionExpression | ArrowFunction ;
266
- return [ [ functionExpressionToDeclaration ( fn . name && fn . name . text , modifiers , fn ) ] , true ] ;
267
- }
268
- case SyntaxKind . ClassExpression : {
269
- // `module.exports = class C {}` --> `export default class C {}`
270
- const cls = exported as ClassExpression ;
271
- return [ [ classExpressionToDeclaration ( cls . name && cls . name . text , modifiers , cls ) ] , true ] ;
272
- }
273
- case SyntaxKind . CallExpression :
274
- if ( isRequireCall ( exported , /*checkArgumentIsStringLiteralLike*/ true ) ) {
275
- return convertReExportAll ( exported . arguments [ 0 ] , checker ) ;
276
- }
277
- // falls through
278
- default :
279
- // `module.exports = 0;` --> `export default 0;`
280
- return [ [ createExportAssignment ( /*decorators*/ undefined , /*modifiers*/ undefined , /*isExportEquals*/ false , exported ) ] , true ] ;
281
- }
282
- }
283
-
284
264
function convertReExportAll ( reExported : StringLiteralLike , checker : TypeChecker ) : [ ReadonlyArray < Statement > , ModuleExportsChanged ] {
285
265
// `module.exports = require("x");` ==> `export * from "x"; export { default } from "x";`
286
266
const moduleSpecifier = reExported . text ;
0 commit comments