Skip to content

Commit 5eb6425

Browse files
committed
Expose import mode calculation functions
1 parent ce9b4c1 commit 5eb6425

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

Diff for: src/compiler/program.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -536,19 +536,32 @@ namespace ts {
536536
return resolutions;
537537
}
538538

539-
/* @internal */
540-
interface SourceFileImportsList {
541-
imports: SourceFile["imports"];
542-
moduleAugmentations: SourceFile["moduleAugmentations"];
539+
/**
540+
* Subset of a SourceFile used to calculate index-based resolutions
541+
* This includes some internal fields, so unless you have very good reason,
542+
* (and are willing to use some less stable internals) you should probably just pass a SourceFile.
543+
*/
544+
export interface SourceFileImportsList {
545+
/* @internal */ imports: SourceFile["imports"];
546+
/* @internal */ moduleAugmentations: SourceFile["moduleAugmentations"];
543547
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
544548
};
545549

546-
/* @internal */
550+
/**
551+
* Calculates the resulting resolution mode for some reference in some file - this is generally the explicitly
552+
* provided resolution mode in the reference, unless one is not present, in which case it is the mode of the containing file.
553+
*/
547554
export function getModeForFileReference(ref: FileReference | string, containingFileMode: SourceFile["impliedNodeFormat"]) {
548555
return (isString(ref) ? containingFileMode : ref.resolutionMode) || containingFileMode;
549556
}
550557

551-
/* @internal */
558+
/**
559+
* Calculates the final resolution mode for an import at some index within a file's imports list. This is generally the explicitly
560+
* defined mode of the import if provided, or, if not, the mode of the containing file (with some exceptions: import=require is always commonjs, dynamic import is always esm).
561+
* If you have an actual import node, prefer using getModeForUsageLocation on the reference string node.
562+
* @param file File to fetch the resolution mode within
563+
* @param index Index into the file's complete resolution list to get the resolution of - this is a concatenation of the file's imports and module augmentations
564+
*/
552565
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number) {
553566
if (file.impliedNodeFormat === undefined) return undefined;
554567
// we ensure all elements of file.imports and file.moduleAugmentations have the relevant parent pointers set during program setup,
@@ -567,7 +580,15 @@ namespace ts {
567580
return false;
568581
}
569582

570-
/* @internal */
583+
/**
584+
* Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if
585+
* one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm).
586+
* Notably, this function always returns `undefined` if the containing file has an `undefined` `impliedNodeFormat` - this field is only set when
587+
* `moduleResolution` is `node16`+.
588+
* @param file The file the import or import-like reference is contained within
589+
* @param usage The module reference string
590+
* @returns The final resolution mode of the import
591+
*/
571592
export function getModeForUsageLocation(file: {impliedNodeFormat?: SourceFile["impliedNodeFormat"]}, usage: StringLiteralLike) {
572593
if (file.impliedNodeFormat === undefined) return undefined;
573594
if ((isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent))) {

Diff for: tests/baselines/reference/api/tsserverlibrary.d.ts

+33
Original file line numberDiff line numberDiff line change
@@ -5102,6 +5102,39 @@ declare namespace ts {
51025102
export function formatDiagnostic(diagnostic: Diagnostic, host: FormatDiagnosticsHost): string;
51035103
export function formatDiagnosticsWithColorAndContext(diagnostics: readonly Diagnostic[], host: FormatDiagnosticsHost): string;
51045104
export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageChain | undefined, newLine: string, indent?: number): string;
5105+
/**
5106+
* Subset of a SourceFile used to calculate index-based resolutions
5107+
* This includes some internal fields, so unless you have very good reason,
5108+
* (and are willing to use some less stable internals) you should probably just pass a SourceFile.
5109+
*/
5110+
export interface SourceFileImportsList {
5111+
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
5112+
}
5113+
/**
5114+
* Calculates the resulting resolution mode for some reference in some file - this is generally the explicitly
5115+
* provided resolution mode in the reference, unless one is not present, in which case it is the mode of the containing file.
5116+
*/
5117+
export function getModeForFileReference(ref: FileReference | string, containingFileMode: SourceFile["impliedNodeFormat"]): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5118+
/**
5119+
* Calculates the final resolution mode for an import at some index within a file's imports list. This is generally the explicitly
5120+
* defined mode of the import if provided, or, if not, the mode of the containing file (with some exceptions: import=require is always commonjs, dynamic import is always esm).
5121+
* If you have an actual import node, prefer using getModeForUsageLocation on the reference string node.
5122+
* @param file File to fetch the resolution mode within
5123+
* @param index Index into the file's complete resolution list to get the resolution of - this is a concatenation of the file's imports and module augmentations
5124+
*/
5125+
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5126+
/**
5127+
* Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if
5128+
* one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm).
5129+
* Notably, this function always returns `undefined` if the containing file has an `undefined` `impliedNodeFormat` - this field is only set when
5130+
* `moduleResolution` is `node16`+.
5131+
* @param file The file the import or import-like reference is contained within
5132+
* @param usage The module reference string
5133+
* @returns The final resolution mode of the import
5134+
*/
5135+
export function getModeForUsageLocation(file: {
5136+
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
5137+
}, usage: StringLiteralLike): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
51055138
export function getConfigFileParsingDiagnostics(configFileParseResult: ParsedCommandLine): readonly Diagnostic[];
51065139
/**
51075140
* A function for determining if a given file is esm or cjs format, assuming modern node module resolution rules, as configured by the

Diff for: tests/baselines/reference/api/typescript.d.ts

+33
Original file line numberDiff line numberDiff line change
@@ -5102,6 +5102,39 @@ declare namespace ts {
51025102
export function formatDiagnostic(diagnostic: Diagnostic, host: FormatDiagnosticsHost): string;
51035103
export function formatDiagnosticsWithColorAndContext(diagnostics: readonly Diagnostic[], host: FormatDiagnosticsHost): string;
51045104
export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageChain | undefined, newLine: string, indent?: number): string;
5105+
/**
5106+
* Subset of a SourceFile used to calculate index-based resolutions
5107+
* This includes some internal fields, so unless you have very good reason,
5108+
* (and are willing to use some less stable internals) you should probably just pass a SourceFile.
5109+
*/
5110+
export interface SourceFileImportsList {
5111+
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
5112+
}
5113+
/**
5114+
* Calculates the resulting resolution mode for some reference in some file - this is generally the explicitly
5115+
* provided resolution mode in the reference, unless one is not present, in which case it is the mode of the containing file.
5116+
*/
5117+
export function getModeForFileReference(ref: FileReference | string, containingFileMode: SourceFile["impliedNodeFormat"]): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5118+
/**
5119+
* Calculates the final resolution mode for an import at some index within a file's imports list. This is generally the explicitly
5120+
* defined mode of the import if provided, or, if not, the mode of the containing file (with some exceptions: import=require is always commonjs, dynamic import is always esm).
5121+
* If you have an actual import node, prefer using getModeForUsageLocation on the reference string node.
5122+
* @param file File to fetch the resolution mode within
5123+
* @param index Index into the file's complete resolution list to get the resolution of - this is a concatenation of the file's imports and module augmentations
5124+
*/
5125+
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5126+
/**
5127+
* Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if
5128+
* one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm).
5129+
* Notably, this function always returns `undefined` if the containing file has an `undefined` `impliedNodeFormat` - this field is only set when
5130+
* `moduleResolution` is `node16`+.
5131+
* @param file The file the import or import-like reference is contained within
5132+
* @param usage The module reference string
5133+
* @returns The final resolution mode of the import
5134+
*/
5135+
export function getModeForUsageLocation(file: {
5136+
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
5137+
}, usage: StringLiteralLike): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
51055138
export function getConfigFileParsingDiagnostics(configFileParseResult: ParsedCommandLine): readonly Diagnostic[];
51065139
/**
51075140
* A function for determining if a given file is esm or cjs format, assuming modern node module resolution rules, as configured by the

0 commit comments

Comments
 (0)