fix: ES6 Modules => ES Modules in error messages (#46378)
* fix: ES6 Modules => ES Modules in error messages * chore: Rename test file names * chore: Rename function names
This commit is contained in:
parent
6a75689a25
commit
dfb40549fc
|
@ -6216,7 +6216,7 @@
|
|||
"code": 18003
|
||||
},
|
||||
|
||||
"File is a CommonJS module; it may be converted to an ES6 module.": {
|
||||
"File is a CommonJS module; it may be converted to an ES module.": {
|
||||
"category": "Suggestion",
|
||||
"code": 80001
|
||||
},
|
||||
|
@ -6465,7 +6465,7 @@
|
|||
"category": "Message",
|
||||
"code": 95016
|
||||
},
|
||||
"Convert to ES6 module": {
|
||||
"Convert to ES module": {
|
||||
"category": "Message",
|
||||
"code": 95017
|
||||
},
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
registerCodeFix({
|
||||
errorCodes: [Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module.code],
|
||||
errorCodes: [Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module.code],
|
||||
getCodeActions(context) {
|
||||
const { sourceFile, program, preferences } = context;
|
||||
const changes = textChanges.ChangeTracker.with(context, changes => {
|
||||
const moduleExportsChangedToDefault = convertFileToEs6Module(sourceFile, program.getTypeChecker(), changes, getEmitScriptTarget(program.getCompilerOptions()), getQuotePreference(sourceFile, preferences));
|
||||
const moduleExportsChangedToDefault = convertFileToEsModule(sourceFile, program.getTypeChecker(), changes, getEmitScriptTarget(program.getCompilerOptions()), getQuotePreference(sourceFile, preferences));
|
||||
if (moduleExportsChangedToDefault) {
|
||||
for (const importingFile of program.getSourceFiles()) {
|
||||
fixImportOfModuleExports(importingFile, sourceFile, changes, getQuotePreference(importingFile, preferences));
|
||||
|
@ -13,7 +13,7 @@ namespace ts.codefix {
|
|||
}
|
||||
});
|
||||
// No support for fix-all since this applies to the whole file at once anyway.
|
||||
return [createCodeFixActionWithoutFixAll("convertToEs6Module", changes, Diagnostics.Convert_to_ES6_module)];
|
||||
return [createCodeFixActionWithoutFixAll("convertToEsModule", changes, Diagnostics.Convert_to_ES_module)];
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -39,7 +39,7 @@ namespace ts.codefix {
|
|||
}
|
||||
|
||||
/** @returns Whether we converted a `module.exports =` to a default export. */
|
||||
function convertFileToEs6Module(sourceFile: SourceFile, checker: TypeChecker, changes: textChanges.ChangeTracker, target: ScriptTarget, quotePreference: QuotePreference): ModuleExportsChanged {
|
||||
function convertFileToEsModule(sourceFile: SourceFile, checker: TypeChecker, changes: textChanges.ChangeTracker, target: ScriptTarget, quotePreference: QuotePreference): ModuleExportsChanged {
|
||||
const identifiers: Identifiers = { original: collectFreeIdentifiers(sourceFile), additional: new Set() };
|
||||
const exports = collectExportRenames(sourceFile, checker, identifiers);
|
||||
convertExportsAccesses(sourceFile, exports, changes);
|
||||
|
@ -210,7 +210,7 @@ namespace ts.codefix {
|
|||
// `const a = require("b").c` --> `import { c as a } from "./b";
|
||||
return convertedImports([makeSingleImport(name.text, propertyName, moduleSpecifier, quotePreference)]);
|
||||
default:
|
||||
return Debug.assertNever(name, `Convert to ES6 module got invalid syntax form ${(name as BindingName).kind}`);
|
||||
return Debug.assertNever(name, `Convert to ES module got invalid syntax form ${(name as BindingName).kind}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -430,7 +430,7 @@ namespace ts.codefix {
|
|||
case SyntaxKind.Identifier:
|
||||
return convertSingleIdentifierImport(name, moduleSpecifier, checker, identifiers, quotePreference);
|
||||
default:
|
||||
return Debug.assertNever(name, `Convert to ES6 module got invalid name kind ${(name as BindingName).kind}`);
|
||||
return Debug.assertNever(name, `Convert to ES module got invalid name kind ${(name as BindingName).kind}`);
|
||||
}
|
||||
}
|
||||
|
|
@ -1993,10 +1993,10 @@ namespace ts.Completions {
|
|||
if (isNonContextualObjectLiteral) return false;
|
||||
// If not already a module, must have modules enabled.
|
||||
if (!preferences.includeCompletionsForModuleExports) return false;
|
||||
// If already using ES6 modules, OK to continue using them.
|
||||
// If already using ES modules, OK to continue using them.
|
||||
if (sourceFile.externalModuleIndicator || sourceFile.commonJsModuleIndicator) return true;
|
||||
// If module transpilation is enabled or we're targeting es6 or above, or not emitting, OK.
|
||||
if (compilerOptionsIndicateEs6Modules(program.getCompilerOptions())) return true;
|
||||
if (compilerOptionsIndicateEsModules(program.getCompilerOptions())) return true;
|
||||
// If some file is using ES6 modules, assume that it's OK to add more.
|
||||
return programContainsModules(program);
|
||||
}
|
||||
|
|
|
@ -138,9 +138,9 @@ namespace ts.refactor {
|
|||
return [...prologueDirectives, ...toMove.all];
|
||||
}
|
||||
|
||||
const useEs6ModuleSyntax = !!oldFile.externalModuleIndicator;
|
||||
const useEsModuleSyntax = !!oldFile.externalModuleIndicator;
|
||||
const quotePreference = getQuotePreference(oldFile, preferences);
|
||||
const importsFromNewFile = createOldFileImportsFromNewFile(usage.oldFileImportsFromNewFile, newModuleName, useEs6ModuleSyntax, quotePreference);
|
||||
const importsFromNewFile = createOldFileImportsFromNewFile(usage.oldFileImportsFromNewFile, newModuleName, useEsModuleSyntax, quotePreference);
|
||||
if (importsFromNewFile) {
|
||||
insertImports(changes, oldFile, importsFromNewFile, /*blankLineBetween*/ true);
|
||||
}
|
||||
|
@ -149,8 +149,8 @@ namespace ts.refactor {
|
|||
deleteMovedStatements(oldFile, toMove.ranges, changes);
|
||||
updateImportsInOtherFiles(changes, program, oldFile, usage.movedSymbols, newModuleName);
|
||||
|
||||
const imports = getNewFileImportsAndAddExportInOldFile(oldFile, usage.oldImportsNeededByNewFile, usage.newFileImportsFromOldFile, changes, checker, useEs6ModuleSyntax, quotePreference);
|
||||
const body = addExports(oldFile, toMove.all, usage.oldFileImportsFromNewFile, useEs6ModuleSyntax);
|
||||
const imports = getNewFileImportsAndAddExportInOldFile(oldFile, usage.oldImportsNeededByNewFile, usage.newFileImportsFromOldFile, changes, checker, useEsModuleSyntax, quotePreference);
|
||||
const body = addExports(oldFile, toMove.all, usage.oldFileImportsFromNewFile, useEsModuleSyntax);
|
||||
if (imports.length && body.length) {
|
||||
return [
|
||||
...prologueDirectives,
|
||||
|
@ -429,7 +429,7 @@ namespace ts.refactor {
|
|||
newFileImportsFromOldFile: ReadonlySymbolSet,
|
||||
changes: textChanges.ChangeTracker,
|
||||
checker: TypeChecker,
|
||||
useEs6ModuleSyntax: boolean,
|
||||
useEsModuleSyntax: boolean,
|
||||
quotePreference: QuotePreference,
|
||||
): readonly SupportedImportStatement[] {
|
||||
const copiedOldImports: SupportedImportStatement[] = [];
|
||||
|
@ -454,7 +454,7 @@ namespace ts.refactor {
|
|||
|
||||
const top = getTopLevelDeclarationStatement(decl);
|
||||
if (markSeenTop(top)) {
|
||||
addExportToChanges(oldFile, top, name, changes, useEs6ModuleSyntax);
|
||||
addExportToChanges(oldFile, top, name, changes, useEsModuleSyntax);
|
||||
}
|
||||
if (hasSyntacticModifier(decl, ModifierFlags.Default)) {
|
||||
oldFileDefault = name;
|
||||
|
@ -465,7 +465,7 @@ namespace ts.refactor {
|
|||
}
|
||||
});
|
||||
|
||||
append(copiedOldImports, makeImportOrRequire(oldFileDefault, oldFileNamedImports, removeFileExtension(getBaseFileName(oldFile.fileName)), useEs6ModuleSyntax, quotePreference));
|
||||
append(copiedOldImports, makeImportOrRequire(oldFileDefault, oldFileNamedImports, removeFileExtension(getBaseFileName(oldFile.fileName)), useEsModuleSyntax, quotePreference));
|
||||
return copiedOldImports;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,9 @@ namespace ts {
|
|||
|
||||
if (!isCommonJSFile &&
|
||||
sourceFile.commonJsModuleIndicator &&
|
||||
(programContainsEs6Modules(program) || compilerOptionsIndicateEs6Modules(program.getCompilerOptions())) &&
|
||||
(programContainsEsModules(program) || compilerOptionsIndicateEsModules(program.getCompilerOptions())) &&
|
||||
containsTopLevelCommonjs(sourceFile)) {
|
||||
diags.push(createDiagnosticForNode(getErrorNodeFromCommonJsIndicator(sourceFile.commonJsModuleIndicator), Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module));
|
||||
diags.push(createDiagnosticForNode(getErrorNodeFromCommonJsIndicator(sourceFile.commonJsModuleIndicator), Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module));
|
||||
}
|
||||
|
||||
const isJsFile = isSourceFileJS(sourceFile);
|
||||
|
@ -66,7 +66,7 @@ namespace ts {
|
|||
}
|
||||
}
|
||||
|
||||
// convertToEs6Module only works on top-level, so don't trigger it if commonjs code only appears in nested scopes.
|
||||
// convertToEsModule only works on top-level, so don't trigger it if commonjs code only appears in nested scopes.
|
||||
function containsTopLevelCommonjs(sourceFile: SourceFile): boolean {
|
||||
return sourceFile.statements.some(statement => {
|
||||
switch (statement.kind) {
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
"codefixes/annotateWithTypeFromJSDoc.ts",
|
||||
"codefixes/convertFunctionToEs6Class.ts",
|
||||
"codefixes/convertToAsyncFunction.ts",
|
||||
"codefixes/convertToEs6Module.ts",
|
||||
"codefixes/convertToEsModule.ts",
|
||||
"codefixes/correctQualifiedNameToIndexedAccessType.ts",
|
||||
"codefixes/convertToTypeOnlyExport.ts",
|
||||
"codefixes/convertToTypeOnlyImport.ts",
|
||||
|
|
|
@ -1897,10 +1897,10 @@ namespace ts {
|
|||
export function programContainsModules(program: Program): boolean {
|
||||
return program.getSourceFiles().some(s => !s.isDeclarationFile && !program.isSourceFileFromExternalLibrary(s) && !!(s.externalModuleIndicator || s.commonJsModuleIndicator));
|
||||
}
|
||||
export function programContainsEs6Modules(program: Program): boolean {
|
||||
export function programContainsEsModules(program: Program): boolean {
|
||||
return program.getSourceFiles().some(s => !s.isDeclarationFile && !program.isSourceFileFromExternalLibrary(s) && !!s.externalModuleIndicator);
|
||||
}
|
||||
export function compilerOptionsIndicateEs6Modules(compilerOptions: CompilerOptions): boolean {
|
||||
export function compilerOptionsIndicateEsModules(compilerOptions: CompilerOptions): boolean {
|
||||
return !!compilerOptions.module || getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 || !!compilerOptions.noEmit;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
////};
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import { x } from "./b";
|
||||
export default class C {
|
|
@ -9,7 +9,7 @@
|
|||
////module.exports = exportsAlias;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: `
|
||||
export function f() {}
|
||||
`,
|
|
@ -10,7 +10,7 @@
|
|||
////exports.default;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`const _default = 0;
|
||||
export { _default as default };
|
|
@ -10,7 +10,7 @@
|
|||
////exports.async = 1;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`const _class = 0;
|
||||
export { _class as class };
|
|
@ -13,7 +13,7 @@
|
|||
// See also `refactorConvertToEs6Module_export_moduleDotExportsEqualsRequire.ts`
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export default function() {}
|
||||
export default function f() {}
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
goTo.file("/z.js");
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export * from "./a";
|
||||
export { default } from "./b";
|
|
@ -13,7 +13,7 @@
|
|||
////const a = require("./a");
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: {
|
||||
"/a.js": "export default 0;",
|
||||
"/b.ts": 'import a from "./a";',
|
|
@ -14,11 +14,11 @@
|
|||
|
||||
const [r0, r1, r2] = test.ranges();
|
||||
verify.getSuggestionDiagnostics([
|
||||
{ message: "File is a CommonJS module; it may be converted to an ES6 module.", code: 80001, range: r0 },
|
||||
{ message: "File is a CommonJS module; it may be converted to an ES module.", code: 80001, range: r0 },
|
||||
]);
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export function f() {}
|
||||
export class C {}
|
|
@ -8,7 +8,7 @@
|
|||
////exports.D = class D { static instance = new D(); }
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export const C = class E { static instance = new E(); }
|
||||
export class D { static instance = new D(); }`,
|
|
@ -8,7 +8,7 @@
|
|||
////exports.h = function h() { h(); }
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export const f = function g() { g(); }
|
||||
export function h() { h(); }`,
|
|
@ -16,7 +16,7 @@
|
|||
////const a = require("./a.js");
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export const x = 0;
|
||||
export function f() { }
|
|
@ -10,7 +10,7 @@
|
|||
////module.exports = { f };
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`function f() {}
|
||||
export default { f };`,
|
|
@ -17,7 +17,7 @@
|
|||
////}
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`export const x = 0;
|
||||
x;
|
|
@ -10,7 +10,7 @@
|
|||
////exports.C = class C extends D { m() {} }
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`var C = {};
|
||||
console.log(C);
|
|
@ -8,7 +8,7 @@
|
|||
////x; y;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: `import _x from "x";
|
||||
const [x, y] = _x;
|
||||
x; y;`,
|
|
@ -10,7 +10,7 @@
|
|||
////y();
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import x from 'x';
|
||||
x();
|
|
@ -9,7 +9,7 @@
|
|||
////x.y;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import x, { y } from "x";
|
||||
x();
|
|
@ -10,7 +10,7 @@
|
|||
////x; a; b; c; d;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import x from "x";
|
||||
import _x from "x";
|
|
@ -10,7 +10,7 @@
|
|||
////x; y; z;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import x from "x";
|
||||
const y = 0;
|
|
@ -9,7 +9,7 @@
|
|||
////const [] = require("1a");
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import aB from "a-b";
|
||||
const [] = aB;
|
|
@ -8,7 +8,7 @@
|
|||
////a; b;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import x from "x";
|
||||
const { x: { a, b } } = x;
|
|
@ -8,7 +8,7 @@
|
|||
////x; z;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import { x, y as z } from "x";
|
||||
x; z;`,
|
|
@ -8,7 +8,7 @@
|
|||
////x.y;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import { y } from "x";
|
||||
y;`,
|
|
@ -12,7 +12,7 @@
|
|||
////x; a; b;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import x from "x";
|
||||
import { c as a } from "b";
|
|
@ -9,7 +9,7 @@
|
|||
////mod.x(x);
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import { x as _x } from "mod";
|
||||
const x = 0;
|
|
@ -9,6 +9,6 @@
|
|||
////require("foo");
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: 'import "foo";',
|
||||
});
|
|
@ -26,6 +26,6 @@ verify.codeFixAvailable([]);
|
|||
|
||||
goTo.file("/d.js");
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: 'export default 0;',
|
||||
});
|
|
@ -8,7 +8,7 @@
|
|||
////let x; x;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import "m";
|
||||
let x; x;`
|
|
@ -9,6 +9,6 @@
|
|||
////export const y = 0;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: "export const x = 0;",
|
||||
});
|
|
@ -7,6 +7,6 @@
|
|||
////const a = require('a'); a;
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent: "import a from 'a'; a;",
|
||||
});
|
|
@ -24,7 +24,7 @@
|
|||
|
||||
verify.codeFix({
|
||||
index: 2,
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`import { defer } from "q";
|
||||
|
|
@ -10,12 +10,12 @@
|
|||
////console.log(privateUnrelated);
|
||||
|
||||
verify.getSuggestionDiagnostics([{
|
||||
message: "File is a CommonJS module; it may be converted to an ES6 module.",
|
||||
message: "File is a CommonJS module; it may be converted to an ES module.",
|
||||
code: 80001,
|
||||
}]);
|
||||
|
||||
verify.codeFix({
|
||||
description: "Convert to ES6 module",
|
||||
description: "Convert to ES module",
|
||||
newFileContent:
|
||||
`var privateUnrelated;
|
||||
export function f() {}
|
Loading…
Reference in a new issue