Fix bug: Converting 'module.exports = {...}' to ES6 doesn't introduce a default export (#24141)
This commit is contained in:
parent
2200c94a43
commit
b4ca23d8f9
5 changed files with 33 additions and 16 deletions
|
@ -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,14 +2574,26 @@ 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);
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -233,7 +233,7 @@ namespace ts.codefix {
|
|||
Debug.assertNever(prop);
|
||||
}
|
||||
});
|
||||
return statements && [statements, true];
|
||||
return statements && [statements, false];
|
||||
}
|
||||
|
||||
function convertNamedExport(
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;');
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
//// C: class {},
|
||||
////};
|
||||
|
||||
// @Filename: /b.js
|
||||
////const a = require("./a.js");
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
newFileContent:
|
||||
|
|
Loading…
Reference in a new issue