Skip to content

Commit ffe16a4

Browse files
committed
Move FileReference mode into helper
1 parent a8519fe commit ffe16a4

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

src/compiler/emitter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3993,8 +3993,10 @@ namespace ts {
39933993
}
39943994
for (const directive of types) {
39953995
const pos = writer.getTextPos();
3996-
// Should we elide `resolution-mode` if it matches the mode the currentSourceFile defaults to?
3997-
writeComment(`/// <reference types="${directive.fileName}" ${directive.resolutionMode && directive.resolutionMode !== currentSourceFile?.impliedNodeFormat ? `resolution-mode="${directive.resolutionMode === ModuleKind.ESNext ? "import" : "require"}"` : ""}/>`);
3996+
const resolutionMode = directive.resolutionMode && directive.resolutionMode !== currentSourceFile?.impliedNodeFormat
3997+
? `resolution-mode="${directive.resolutionMode === ModuleKind.ESNext ? "import" : "require"}"`
3998+
: "";
3999+
writeComment(`/// <reference types="${directive.fileName}" ${resolutionMode}/>`);
39984000
if (bundleFileInfo) bundleFileInfo.sections.push({ pos, end: writer.getTextPos(), kind: !directive.resolutionMode ? BundleFileSectionKind.Type : directive.resolutionMode === ModuleKind.ESNext ? BundleFileSectionKind.TypeResolutionModeImport : BundleFileSectionKind.TypeResolutionModeRequire, data: directive.fileName });
39994001
writeLine();
40004002
}

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ namespace ts {
347347
// are present in a non-modal project; while in theory we'd like to either ignore the mode or provide faithful modern resolution, depending on what we feel is best,
348348
// in practice, not every cache has the options available to intelligently make the choice to ignore the mode request, and it's unclear how modern "faithful modern
349349
// resolution" should be (`node12`? `nodenext`?). As such, witnessing a mode-overriding triple-slash reference in a non-modal module resolution
350-
// context should _probably_ be an error - and that should likely be handled by the `Program`.
350+
// context should _probably_ be an error - and that should likely be handled by the `Program` (which is what we do).
351351
if (resolutionMode === ModuleKind.ESNext && (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node12 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext)) {
352352
features |= NodeResolutionFeatures.EsmMode;
353353
}

src/compiler/program.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ namespace ts {
518518
const cache = new Map<string, T>();
519519
for (const name of names) {
520520
let result: T;
521-
const mode = (!isString(name) ? name.resolutionMode : undefined) || containingFileMode;
521+
const mode = getModeForFileReference(name, containingFileMode);
522522
// We lower-case all type references because npm automatically lowercases all packages. See GH#9824.
523523
const strName = isString(name) ? name : name.fileName.toLowerCase();
524524
const cacheKey = mode !== undefined ? `${mode}|${strName}` : strName;
@@ -540,6 +540,11 @@ namespace ts {
540540
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
541541
};
542542

543+
/* @internal */
544+
export function getModeForFileReference(ref: FileReference | string, containingFileMode: SourceFile["impliedNodeFormat"]) {
545+
return (isString(ref) ? containingFileMode : ref.resolutionMode) || containingFileMode;
546+
}
547+
543548
/* @internal */
544549
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number) {
545550
if (file.impliedNodeFormat === undefined) return undefined;

src/compiler/resolutionCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ namespace ts {
400400
// import's syntax may override the file's default mode.
401401
// Type references instead supply a `containingSourceFileMode` and a non-string entry which contains
402402
// a default file mode override if applicable.
403-
const mode = !isString(entry) ? entry.resolutionMode || containingSourceFileMode :
403+
const mode = !isString(entry) ? getModeForFileReference(entry, containingSourceFileMode) :
404404
containingSourceFile ? getModeForResolutionAtIndex(containingSourceFile, i) : undefined;
405405
i++;
406406
let resolution = resolutionsInFile.get(name, mode);

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ namespace ts {
219219
const entry = names[i];
220220
// We lower-case all type references because npm automatically lowercases all packages. See GH#9824.
221221
const name = !isString(entry) ? entry.fileName.toLowerCase() : entry;
222-
const mode = !isString(entry) ? entry.resolutionMode || oldSourceFile?.impliedNodeFormat : oldSourceFile && getModeForResolutionAtIndex(oldSourceFile, i);
222+
const mode = !isString(entry) ? getModeForFileReference(entry, oldSourceFile?.impliedNodeFormat) : oldSourceFile && getModeForResolutionAtIndex(oldSourceFile, i);
223223
const oldResolution = oldResolutions && oldResolutions.get(name, mode);
224224
const changed =
225225
oldResolution

0 commit comments

Comments
 (0)