Skip to content

Commit b4ca23d

Browse files
author
Andy
authored
Fix bug: Converting 'module.exports = {...}' to ES6 doesn't introduce a default export (microsoft#24141)
1 parent 2200c94 commit b4ca23d

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

src/harness/fourslash.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -2293,9 +2293,13 @@ Actual: ${stringify(fullActual)}`);
22932293
}
22942294

22952295
public verifyCurrentFileContent(text: string) {
2296-
const actual = this.getFileContent(this.activeFile.fileName);
2296+
this.verifyFileContent(this.activeFile.fileName, text);
2297+
}
2298+
2299+
private verifyFileContent(fileName: string, text: string) {
2300+
const actual = this.getFileContent(fileName);
22972301
if (actual !== text) {
2298-
throw new Error(`verifyCurrentFileContent failed:\n${showTextDiff(text, actual)}`);
2302+
throw new Error(`verifyFileContent failed:\n${showTextDiff(text, actual)}`);
22992303
}
23002304
}
23012305

@@ -2483,7 +2487,7 @@ Actual: ${stringify(fullActual)}`);
24832487

24842488
this.applyCodeActions(details.codeActions);
24852489

2486-
this.verifyNewContent(options);
2490+
this.verifyNewContent(options, ts.flatMap(details.codeActions, a => a.changes.map(c => c.fileName)));
24872491
}
24882492

24892493
public verifyRangeIs(expectedText: string, includeWhiteSpace?: boolean) {
@@ -2570,13 +2574,25 @@ Actual: ${stringify(fullActual)}`);
25702574
this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false);
25712575
}
25722576

2573-
this.verifyNewContent(options);
2577+
this.verifyNewContent(options, action.changes.map(c => c.fileName));
25742578
}
25752579

2576-
private verifyNewContent(options: FourSlashInterface.NewContentOptions) {
2580+
private verifyNewContent(options: FourSlashInterface.NewContentOptions, changedFiles: ReadonlyArray<string>) {
2581+
const assertedChangedFiles = !options.newFileContent || typeof options.newFileContent === "string"
2582+
? [this.activeFile.fileName]
2583+
: ts.getOwnKeys(options.newFileContent);
2584+
assert.deepEqual(assertedChangedFiles, changedFiles);
2585+
25772586
if (options.newFileContent !== undefined) {
25782587
assert(!options.newRangeContent);
2579-
this.verifyCurrentFileContent(options.newFileContent);
2588+
if (typeof options.newFileContent === "string") {
2589+
this.verifyCurrentFileContent(options.newFileContent);
2590+
}
2591+
else {
2592+
for (const fileName in options.newFileContent) {
2593+
this.verifyFileContent(fileName, options.newFileContent[fileName]);
2594+
}
2595+
}
25802596
}
25812597
else {
25822598
this.verifyRangeIs(options.newRangeContent, /*includeWhitespace*/ true);
@@ -4781,7 +4797,7 @@ namespace FourSlashInterface {
47814797

47824798
export interface NewContentOptions {
47834799
// Exactly one of these should be defined.
4784-
newFileContent?: string;
4800+
newFileContent?: string | { readonly [filename: string]: string };
47854801
newRangeContent?: string;
47864802
}
47874803

src/services/codefixes/convertToEs6Module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ namespace ts.codefix {
233233
Debug.assertNever(prop);
234234
}
235235
});
236-
return statements && [statements, true];
236+
return statements && [statements, false];
237237
}
238238

239239
function convertNamedExport(

tests/cases/fourslash/fourslash.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ declare namespace FourSlashInterface {
179179
isInCommentAtPosition(onlyMultiLineDiverges?: boolean): void;
180180
codeFix(options: {
181181
description: string,
182-
newFileContent?: string,
182+
newFileContent?: string | { readonly [fileName: string]: string },
183183
newRangeContent?: string,
184184
errorCode?: number,
185185
index?: number,

tests/cases/fourslash/refactorConvertToEs6Module_export_moduleDotExports_changesImports.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414

1515
verify.codeFix({
1616
description: "Convert to ES6 module",
17-
newFileContent: "export default 0;",
17+
newFileContent: {
18+
"/a.js": "export default 0;",
19+
"/b.ts": 'import a from "./a";',
20+
"/c.js": 'const a = require("./a").default;',
21+
}
1822
});
19-
20-
goTo.file("/b.ts");
21-
verify.currentFileContentIs('import a from "./a";');
22-
23-
goTo.file("/c.js");
24-
verify.currentFileContentIs('const a = require("./a").default;');

tests/cases/fourslash/refactorConvertToEs6Module_export_object.ts

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
//// C: class {},
1313
////};
1414

15+
// @Filename: /b.js
16+
////const a = require("./a.js");
17+
1518
verify.codeFix({
1619
description: "Convert to ES6 module",
1720
newFileContent:

0 commit comments

Comments
 (0)