Fix bug: Converting 'module.exports = {...}' to ES6 doesn't introduce a default export (#24141)

This commit is contained in:
Andy 2018-05-15 16:22:58 -07:00 committed by GitHub
parent 2200c94a43
commit b4ca23d8f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 16 deletions

View file

@ -2293,9 +2293,13 @@ Actual: ${stringify(fullActual)}`);
}
public verifyCurrentFileContent(text: string) {
const actual = this.getFileContent(this.activeFile.fileName);
this.verifyFileContent(this.activeFile.fileName, text);
}
private verifyFileContent(fileName: string, text: string) {
const actual = this.getFileContent(fileName);
if (actual !== text) {
throw new Error(`verifyCurrentFileContent failed:\n${showTextDiff(text, actual)}`);
throw new Error(`verifyFileContent failed:\n${showTextDiff(text, actual)}`);
}
}
@ -2483,7 +2487,7 @@ Actual: ${stringify(fullActual)}`);
this.applyCodeActions(details.codeActions);
this.verifyNewContent(options);
this.verifyNewContent(options, ts.flatMap(details.codeActions, a => a.changes.map(c => c.fileName)));
}
public verifyRangeIs(expectedText: string, includeWhiteSpace?: boolean) {
@ -2570,13 +2574,25 @@ Actual: ${stringify(fullActual)}`);
this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false);
}
this.verifyNewContent(options);
this.verifyNewContent(options, action.changes.map(c => c.fileName));
}
private verifyNewContent(options: FourSlashInterface.NewContentOptions) {
private verifyNewContent(options: FourSlashInterface.NewContentOptions, changedFiles: ReadonlyArray<string>) {
const assertedChangedFiles = !options.newFileContent || typeof options.newFileContent === "string"
? [this.activeFile.fileName]
: ts.getOwnKeys(options.newFileContent);
assert.deepEqual(assertedChangedFiles, changedFiles);
if (options.newFileContent !== undefined) {
assert(!options.newRangeContent);
this.verifyCurrentFileContent(options.newFileContent);
if (typeof options.newFileContent === "string") {
this.verifyCurrentFileContent(options.newFileContent);
}
else {
for (const fileName in options.newFileContent) {
this.verifyFileContent(fileName, options.newFileContent[fileName]);
}
}
}
else {
this.verifyRangeIs(options.newRangeContent, /*includeWhitespace*/ true);
@ -4781,7 +4797,7 @@ namespace FourSlashInterface {
export interface NewContentOptions {
// Exactly one of these should be defined.
newFileContent?: string;
newFileContent?: string | { readonly [filename: string]: string };
newRangeContent?: string;
}

View file

@ -233,7 +233,7 @@ namespace ts.codefix {
Debug.assertNever(prop);
}
});
return statements && [statements, true];
return statements && [statements, false];
}
function convertNamedExport(

View file

@ -179,7 +179,7 @@ declare namespace FourSlashInterface {
isInCommentAtPosition(onlyMultiLineDiverges?: boolean): void;
codeFix(options: {
description: string,
newFileContent?: string,
newFileContent?: string | { readonly [fileName: string]: string },
newRangeContent?: string,
errorCode?: number,
index?: number,

View file

@ -14,11 +14,9 @@
verify.codeFix({
description: "Convert to ES6 module",
newFileContent: "export default 0;",
newFileContent: {
"/a.js": "export default 0;",
"/b.ts": 'import a from "./a";',
"/c.js": 'const a = require("./a").default;',
}
});
goTo.file("/b.ts");
verify.currentFileContentIs('import a from "./a";');
goTo.file("/c.js");
verify.currentFileContentIs('const a = require("./a").default;');

View file

@ -12,6 +12,9 @@
//// C: class {},
////};
// @Filename: /b.js
////const a = require("./a.js");
verify.codeFix({
description: "Convert to ES6 module",
newFileContent: