Skip to content

Commit 39c9eeb

Browse files
authored
Pick #58857 to release-5.5 (#58858)
1 parent 2b0009c commit 39c9eeb

File tree

87 files changed

+324
-2987
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+324
-2987
lines changed

src/compiler/_namespaces/ts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export * from "../transformers/generators.js";
5555
export * from "../transformers/module/module.js";
5656
export * from "../transformers/module/system.js";
5757
export * from "../transformers/module/esnextAnd2015.js";
58-
export * from "../transformers/module/impliedNodeFormatDependent.js";
58+
export * from "../transformers/module/node.js";
5959
export * from "../transformers/declarations/diagnostics.js";
6060
export * from "../transformers/declarations.js";
6161
export * from "../transformer.js";

src/compiler/checker.ts

+34-63
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

-4
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,6 @@
967967
"category": "Error",
968968
"code": 1292
969969
},
970-
"ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": {
971-
"category": "Error",
972-
"code": 1293
973-
},
974970

975971
"'with' statements are not allowed in an async function block.": {
976972
"category": "Error",

src/compiler/emitter.ts

-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ import {
131131
getEmitFlags,
132132
getEmitHelpers,
133133
getEmitModuleKind,
134-
getEmitModuleResolutionKind,
135134
getEmitScriptTarget,
136135
getExternalModuleName,
137136
getIdentifierTypeArguments,
@@ -818,7 +817,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
818817
newLine: compilerOptions.newLine,
819818
noEmitHelpers: compilerOptions.noEmitHelpers,
820819
module: getEmitModuleKind(compilerOptions),
821-
moduleResolution: getEmitModuleResolutionKind(compilerOptions),
822820
target: getEmitScriptTarget(compilerOptions),
823821
sourceMap: compilerOptions.sourceMap,
824822
inlineSourceMap: compilerOptions.inlineSourceMap,
@@ -894,7 +892,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
894892
newLine: compilerOptions.newLine,
895893
noEmitHelpers: true,
896894
module: compilerOptions.module,
897-
moduleResolution: compilerOptions.moduleResolution,
898895
target: compilerOptions.target,
899896
sourceMap: !forceDtsEmit && compilerOptions.declarationMap,
900897
inlineSourceMap: compilerOptions.inlineSourceMap,

src/compiler/factory/utilities.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ import {
5353
getAllAccessorDeclarations,
5454
getEmitFlags,
5555
getEmitHelpers,
56-
getEmitModuleFormatOfFileWorker,
5756
getEmitModuleKind,
5857
getESModuleInterop,
5958
getExternalModuleName,
6059
getExternalModuleNameFromPath,
61-
getImpliedNodeFormatForEmitWorker,
6260
getJSDocType,
6361
getJSDocTypeTag,
6462
getModifiers,
@@ -714,7 +712,7 @@ export function createExternalHelpersImportDeclarationIfNeeded(nodeFactory: Node
714712
if (compilerOptions.importHelpers && isEffectiveExternalModule(sourceFile, compilerOptions)) {
715713
let namedBindings: NamedImportBindings | undefined;
716714
const moduleKind = getEmitModuleKind(compilerOptions);
717-
if ((moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) || getImpliedNodeFormatForEmitWorker(sourceFile, compilerOptions) === ModuleKind.ESNext) {
715+
if ((moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) || sourceFile.impliedNodeFormat === ModuleKind.ESNext) {
718716
// use named imports
719717
const helpers = getEmitHelpers(sourceFile);
720718
if (helpers) {
@@ -771,8 +769,10 @@ export function getOrCreateExternalHelpersModuleNameIfNeeded(factory: NodeFactor
771769
return externalHelpersModuleName;
772770
}
773771

772+
const moduleKind = getEmitModuleKind(compilerOptions);
774773
let create = (hasExportStarsToExportValues || (getESModuleInterop(compilerOptions) && hasImportStarOrImportDefault))
775-
&& getEmitModuleFormatOfFileWorker(node, compilerOptions) < ModuleKind.System;
774+
&& moduleKind !== ModuleKind.System
775+
&& (moduleKind < ModuleKind.ES2015 || node.impliedNodeFormat === ModuleKind.CommonJS);
776776
if (!create) {
777777
const helpers = getEmitHelpers(node);
778778
if (helpers) {

src/compiler/moduleSpecifiers.ts

+13-24
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import {
3737
getBaseFileName,
3838
GetCanonicalFileName,
3939
getConditions,
40-
getDefaultResolutionModeForFileWorker,
4140
getDirectoryPath,
4241
getEmitModuleResolutionKind,
42+
getModeForResolutionAtIndex,
4343
getModuleNameStringLiteralAt,
4444
getModuleSpecifierEndingPreference,
4545
getNodeModulePathParts,
@@ -143,13 +143,12 @@ export interface ModuleSpecifierPreferences {
143143
/**
144144
* @param syntaxImpliedNodeFormat Used when the import syntax implies ESM or CJS irrespective of the mode of the file.
145145
*/
146-
getAllowedEndingsInPreferredOrder(syntaxImpliedNodeFormat?: ResolutionMode): ModuleSpecifierEnding[];
146+
getAllowedEndingsInPreferredOrder(syntaxImpliedNodeFormat?: SourceFile["impliedNodeFormat"]): ModuleSpecifierEnding[];
147147
}
148148

149149
/** @internal */
150150
export function getModuleSpecifierPreferences(
151151
{ importModuleSpecifierPreference, importModuleSpecifierEnding }: UserPreferences,
152-
host: Pick<ModuleSpecifierResolutionHost, "getDefaultResolutionModeForFile">,
153152
compilerOptions: CompilerOptions,
154153
importingSourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat">,
155154
oldImportSpecifier?: string,
@@ -164,10 +163,8 @@ export function getModuleSpecifierPreferences(
164163
importModuleSpecifierPreference === "project-relative" ? RelativePreference.ExternalNonRelative :
165164
RelativePreference.Shortest,
166165
getAllowedEndingsInPreferredOrder: syntaxImpliedNodeFormat => {
167-
const impliedNodeFormat = getDefaultResolutionModeForFile(importingSourceFile, host, compilerOptions);
168-
const preferredEnding = syntaxImpliedNodeFormat !== impliedNodeFormat ? getPreferredEnding(syntaxImpliedNodeFormat) : filePreferredEnding;
169-
const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
170-
if ((syntaxImpliedNodeFormat ?? impliedNodeFormat) === ModuleKind.ESNext && ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext) {
166+
const preferredEnding = syntaxImpliedNodeFormat !== importingSourceFile.impliedNodeFormat ? getPreferredEnding(syntaxImpliedNodeFormat) : filePreferredEnding;
167+
if ((syntaxImpliedNodeFormat ?? importingSourceFile.impliedNodeFormat) === ModuleKind.ESNext) {
171168
if (shouldAllowImportingTsExtension(compilerOptions, importingSourceFile.fileName)) {
172169
return [ModuleSpecifierEnding.TsExtension, ModuleSpecifierEnding.JsExtension];
173170
}
@@ -207,7 +204,7 @@ export function getModuleSpecifierPreferences(
207204
}
208205
return getModuleSpecifierEndingPreference(
209206
importModuleSpecifierEnding,
210-
resolutionMode ?? getDefaultResolutionModeForFile(importingSourceFile, host, compilerOptions),
207+
resolutionMode ?? importingSourceFile.impliedNodeFormat,
211208
compilerOptions,
212209
isFullSourceFile(importingSourceFile) ? importingSourceFile : undefined,
213210
);
@@ -228,7 +225,7 @@ export function updateModuleSpecifier(
228225
oldImportSpecifier: string,
229226
options: ModuleSpecifierOptions = {},
230227
): string | undefined {
231-
const res = getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, host, compilerOptions, importingSourceFile, oldImportSpecifier), {}, options);
228+
const res = getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, compilerOptions, importingSourceFile, oldImportSpecifier), {}, options);
232229
if (res === oldImportSpecifier) return undefined;
233230
return res;
234231
}
@@ -248,7 +245,7 @@ export function getModuleSpecifier(
248245
host: ModuleSpecifierResolutionHost,
249246
options: ModuleSpecifierOptions = {},
250247
): string {
251-
return getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, host, compilerOptions, importingSourceFile), {}, options);
248+
return getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, compilerOptions, importingSourceFile), {}, options);
252249
}
253250

254251
/** @internal */
@@ -278,7 +275,7 @@ function getModuleSpecifierWorker(
278275
const info = getInfo(importingSourceFileName, host);
279276
const modulePaths = getAllModulePaths(info, toFileName, host, userPreferences, compilerOptions, options);
280277
return firstDefined(modulePaths, modulePath => tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode)) ||
281-
getLocalModuleSpecifier(toFileName, info, compilerOptions, host, options.overrideImportMode || getDefaultResolutionModeForFile(importingSourceFile, host, compilerOptions), preferences);
278+
getLocalModuleSpecifier(toFileName, info, compilerOptions, host, options.overrideImportMode || importingSourceFile.impliedNodeFormat, preferences);
282279
}
283280

284281
/** @internal */
@@ -406,7 +403,7 @@ export function getLocalModuleSpecifierBetweenFileNames(
406403
compilerOptions,
407404
host,
408405
importMode,
409-
getModuleSpecifierPreferences({}, host, compilerOptions, importingFile),
406+
getModuleSpecifierPreferences({}, compilerOptions, importingFile),
410407
);
411408
}
412409

@@ -420,19 +417,15 @@ function computeModuleSpecifiers(
420417
forAutoImport: boolean,
421418
): ModuleSpecifierResult {
422419
const info = getInfo(importingSourceFile.fileName, host);
423-
const preferences = getModuleSpecifierPreferences(userPreferences, host, compilerOptions, importingSourceFile);
420+
const preferences = getModuleSpecifierPreferences(userPreferences, compilerOptions, importingSourceFile);
424421
const existingSpecifier = isFullSourceFile(importingSourceFile) && forEach(modulePaths, modulePath =>
425422
forEach(
426423
host.getFileIncludeReasons().get(toPath(modulePath.path, host.getCurrentDirectory(), info.getCanonicalFileName)),
427424
reason => {
428425
if (reason.kind !== FileIncludeKind.Import || reason.file !== importingSourceFile.path) return undefined;
429426
// If the candidate import mode doesn't match the mode we're generating for, don't consider it
430427
// TODO: maybe useful to keep around as an alternative option for certain contexts where the mode is overridable
431-
const existingMode = host.getModeForResolutionAtIndex(importingSourceFile, reason.index);
432-
const targetMode = options.overrideImportMode ?? host.getDefaultResolutionModeForFile(importingSourceFile);
433-
if (existingMode !== targetMode && existingMode !== undefined && targetMode !== undefined) {
434-
return undefined;
435-
}
428+
if (importingSourceFile.impliedNodeFormat && importingSourceFile.impliedNodeFormat !== getModeForResolutionAtIndex(importingSourceFile, reason.index, compilerOptions)) return undefined;
436429
const specifier = getModuleNameStringLiteralAt(importingSourceFile, reason.index).text;
437430
// If the preference is for non relative and the module specifier is relative, ignore it
438431
return preferences.relativePreference !== RelativePreference.NonRelative || !pathIsRelative(specifier) ?
@@ -1100,7 +1093,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
11001093

11011094
// Simplify the full file path to something that can be resolved by Node.
11021095

1103-
const preferences = getModuleSpecifierPreferences(userPreferences, host, options, importingSourceFile);
1096+
const preferences = getModuleSpecifierPreferences(userPreferences, options, importingSourceFile);
11041097
const allowedEndings = preferences.getAllowedEndingsInPreferredOrder();
11051098
let moduleSpecifier = path;
11061099
let isPackageRootPath = false;
@@ -1160,7 +1153,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
11601153
const cachedPackageJson = host.getPackageJsonInfoCache?.()?.getPackageJsonInfo(packageJsonPath);
11611154
if (isPackageJsonInfo(cachedPackageJson) || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
11621155
const packageJsonContent: Record<string, any> | undefined = cachedPackageJson?.contents.packageJsonContent || tryParseJson(host.readFile!(packageJsonPath)!);
1163-
const importMode = overrideMode || getDefaultResolutionModeForFile(importingSourceFile, host, options);
1156+
const importMode = overrideMode || importingSourceFile.impliedNodeFormat;
11641157
if (getResolvePackageJsonExports(options)) {
11651158
// The package name that we found in node_modules could be different from the package
11661159
// name in the package.json content via url/filepath dependency specifiers. We need to
@@ -1355,7 +1348,3 @@ function getRelativePathIfInSameVolume(path: string, directoryPath: string, getC
13551348
function isPathRelativeToParent(path: string): boolean {
13561349
return startsWith(path, "..");
13571350
}
1358-
1359-
function getDefaultResolutionModeForFile(file: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, host: Pick<ModuleSpecifierResolutionHost, "getDefaultResolutionModeForFile">, compilerOptions: CompilerOptions) {
1360-
return isFullSourceFile(file) ? host.getDefaultResolutionModeForFile(file) : getDefaultResolutionModeForFileWorker(file, compilerOptions);
1361-
}

0 commit comments

Comments
 (0)