Skip to content

Commit 4c4c803

Browse files
TypeScript BotDanielRosenwasser
TypeScript Bot
andauthored
🤖 Pick PR #49360 (Expose import mode calculation func...) into release-4.7 (#49370)
* Cherry-pick PR #49360 into release-4.7 Component commits: 5eb6425 Expose import mode calculation functions 1f907ae Make `SourceFileImportsList` internal again. 4e40185 Accepted API baselines. * Fix lints. Co-authored-by: Daniel Rosenwasser <[email protected]> Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent 3ce08c6 commit 4c4c803

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

‎src/compiler/program.ts

+35-8
Original file line numberDiff line numberDiff line change
@@ -533,20 +533,39 @@ namespace ts {
533533
return resolutions;
534534
}
535535

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

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

548-
/* @internal */
549-
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number) {
557+
/**
558+
* Calculates the final resolution mode for an import at some index within a file's imports list. This is generally the explicitly
559+
* 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).
560+
* If you have an actual import node, prefer using getModeForUsageLocation on the reference string node.
561+
* @param file File to fetch the resolution mode within
562+
* @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
563+
*/
564+
export function getModeForResolutionAtIndex(file: SourceFile, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
565+
/** @internal */
566+
// eslint-disable-next-line @typescript-eslint/unified-signatures
567+
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
568+
export function getModeForResolutionAtIndex(file: SourceFileImportsList, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined {
550569
if (file.impliedNodeFormat === undefined) return undefined;
551570
// we ensure all elements of file.imports and file.moduleAugmentations have the relevant parent pointers set during program setup,
552571
// so it's safe to use them even pre-bind
@@ -564,7 +583,15 @@ namespace ts {
564583
return false;
565584
}
566585

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

‎tests/baselines/reference/api/tsserverlibrary.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -5099,6 +5099,31 @@ declare namespace ts {
50995099
export function formatDiagnostic(diagnostic: Diagnostic, host: FormatDiagnosticsHost): string;
51005100
export function formatDiagnosticsWithColorAndContext(diagnostics: readonly Diagnostic[], host: FormatDiagnosticsHost): string;
51015101
export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageChain | undefined, newLine: string, indent?: number): string;
5102+
/**
5103+
* Calculates the resulting resolution mode for some reference in some file - this is generally the explicitly
5104+
* provided resolution mode in the reference, unless one is not present, in which case it is the mode of the containing file.
5105+
*/
5106+
export function getModeForFileReference(ref: FileReference | string, containingFileMode: SourceFile["impliedNodeFormat"]): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5107+
/**
5108+
* Calculates the final resolution mode for an import at some index within a file's imports list. This is generally the explicitly
5109+
* 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).
5110+
* If you have an actual import node, prefer using getModeForUsageLocation on the reference string node.
5111+
* @param file File to fetch the resolution mode within
5112+
* @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
5113+
*/
5114+
export function getModeForResolutionAtIndex(file: SourceFile, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5115+
/**
5116+
* Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if
5117+
* one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm).
5118+
* Notably, this function always returns `undefined` if the containing file has an `undefined` `impliedNodeFormat` - this field is only set when
5119+
* `moduleResolution` is `node16`+.
5120+
* @param file The file the import or import-like reference is contained within
5121+
* @param usage The module reference string
5122+
* @returns The final resolution mode of the import
5123+
*/
5124+
export function getModeForUsageLocation(file: {
5125+
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
5126+
}, usage: StringLiteralLike): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
51025127
export function getConfigFileParsingDiagnostics(configFileParseResult: ParsedCommandLine): readonly Diagnostic[];
51035128
/**
51045129
* A function for determining if a given file is esm or cjs format, assuming modern node module resolution rules, as configured by the

‎tests/baselines/reference/api/typescript.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -5099,6 +5099,31 @@ declare namespace ts {
50995099
export function formatDiagnostic(diagnostic: Diagnostic, host: FormatDiagnosticsHost): string;
51005100
export function formatDiagnosticsWithColorAndContext(diagnostics: readonly Diagnostic[], host: FormatDiagnosticsHost): string;
51015101
export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageChain | undefined, newLine: string, indent?: number): string;
5102+
/**
5103+
* Calculates the resulting resolution mode for some reference in some file - this is generally the explicitly
5104+
* provided resolution mode in the reference, unless one is not present, in which case it is the mode of the containing file.
5105+
*/
5106+
export function getModeForFileReference(ref: FileReference | string, containingFileMode: SourceFile["impliedNodeFormat"]): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5107+
/**
5108+
* Calculates the final resolution mode for an import at some index within a file's imports list. This is generally the explicitly
5109+
* 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).
5110+
* If you have an actual import node, prefer using getModeForUsageLocation on the reference string node.
5111+
* @param file File to fetch the resolution mode within
5112+
* @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
5113+
*/
5114+
export function getModeForResolutionAtIndex(file: SourceFile, index: number): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
5115+
/**
5116+
* Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if
5117+
* one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm).
5118+
* Notably, this function always returns `undefined` if the containing file has an `undefined` `impliedNodeFormat` - this field is only set when
5119+
* `moduleResolution` is `node16`+.
5120+
* @param file The file the import or import-like reference is contained within
5121+
* @param usage The module reference string
5122+
* @returns The final resolution mode of the import
5123+
*/
5124+
export function getModeForUsageLocation(file: {
5125+
impliedNodeFormat?: SourceFile["impliedNodeFormat"];
5126+
}, usage: StringLiteralLike): ModuleKind.CommonJS | ModuleKind.ESNext | undefined;
51025127
export function getConfigFileParsingDiagnostics(configFileParseResult: ParsedCommandLine): readonly Diagnostic[];
51035128
/**
51045129
* 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)