Skip to content

Commit fbcdb8c

Browse files
emmatownAndaristandrewbranch
authored
Add auto-import for the package.json imports field (#55015)
Co-authored-by: Mateusz Burzyński <[email protected]> Co-authored-by: Andrew Branch <[email protected]>
1 parent 93e6b9d commit fbcdb8c

35 files changed

+2298
-69
lines changed

Diff for: src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50764,7 +50764,7 @@ export function signatureHasLiteralTypes(s: Signature) {
5076450764
return !!(s.flags & SignatureFlags.HasLiteralTypes);
5076550765
}
5076650766

50767-
function createBasicNodeBuilderModuleSpecifierResolutionHost(host: TypeCheckerHost): ModuleSpecifierResolutionHost & { getCommonSourceDirectory(): string; } {
50767+
function createBasicNodeBuilderModuleSpecifierResolutionHost(host: TypeCheckerHost): ModuleSpecifierResolutionHost {
5076850768
return {
5076950769
getCommonSourceDirectory: !!(host as Program).getCommonSourceDirectory ? () => (host as Program).getCommonSourceDirectory() : () => "",
5077050770
getCurrentDirectory: () => host.getCurrentDirectory(),

Diff for: src/compiler/core.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -2400,9 +2400,13 @@ function levenshteinWithMax(s1: string, s2: string, max: number): number | undef
24002400
}
24012401

24022402
/** @internal */
2403-
export function endsWith(str: string, suffix: string): boolean {
2403+
export function endsWith(str: string, suffix: string, ignoreCase?: boolean): boolean {
24042404
const expectedPos = str.length - suffix.length;
2405-
return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos;
2405+
return expectedPos >= 0 && (
2406+
ignoreCase
2407+
? equateStringsCaseInsensitive(str.slice(expectedPos), suffix)
2408+
: str.indexOf(suffix, expectedPos) === expectedPos
2409+
);
24062410
}
24072411

24082412
/** @internal */
@@ -2579,8 +2583,10 @@ export function findBestPatternMatch<T>(values: readonly T[], getPattern: (value
25792583
}
25802584

25812585
/** @internal */
2582-
export function startsWith(str: string, prefix: string): boolean {
2583-
return str.lastIndexOf(prefix, 0) === 0;
2586+
export function startsWith(str: string, prefix: string, ignoreCase?: boolean): boolean {
2587+
return ignoreCase
2588+
? equateStringsCaseInsensitive(str.slice(0, prefix.length), prefix)
2589+
: str.lastIndexOf(prefix, 0) === 0;
25842590
}
25852591

25862592
/** @internal */

Diff for: src/compiler/emitter.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -583,35 +583,50 @@ export function getOutputExtension(fileName: string, options: CompilerOptions):
583583
Extension.Js;
584584
}
585585

586-
function getOutputPathWithoutChangingExt(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean, outputDir: string | undefined, getCommonSourceDirectory?: () => string) {
586+
function getOutputPathWithoutChangingExt(
587+
inputFileName: string,
588+
ignoreCase: boolean,
589+
outputDir: string | undefined,
590+
getCommonSourceDirectory: () => string,
591+
): string {
587592
return outputDir ?
588593
resolvePath(
589594
outputDir,
590-
getRelativePathFromDirectory(getCommonSourceDirectory ? getCommonSourceDirectory() : getCommonSourceDirectoryOfConfig(configFile, ignoreCase), inputFileName, ignoreCase),
595+
getRelativePathFromDirectory(getCommonSourceDirectory(), inputFileName, ignoreCase),
591596
) :
592597
inputFileName;
593598
}
594599

595600
/** @internal */
596-
export function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean, getCommonSourceDirectory?: () => string) {
601+
export function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean, getCommonSourceDirectory = () => getCommonSourceDirectoryOfConfig(configFile, ignoreCase)) {
602+
return getOutputDeclarationFileNameWorker(inputFileName, configFile.options, ignoreCase, getCommonSourceDirectory);
603+
}
604+
605+
/** @internal */
606+
export function getOutputDeclarationFileNameWorker(inputFileName: string, options: CompilerOptions, ignoreCase: boolean, getCommonSourceDirectory: () => string) {
597607
return changeExtension(
598-
getOutputPathWithoutChangingExt(inputFileName, configFile, ignoreCase, configFile.options.declarationDir || configFile.options.outDir, getCommonSourceDirectory),
608+
getOutputPathWithoutChangingExt(inputFileName, ignoreCase, options.declarationDir || options.outDir, getCommonSourceDirectory),
599609
getDeclarationEmitExtensionForPath(inputFileName),
600610
);
601611
}
602612

603-
function getOutputJSFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean, getCommonSourceDirectory?: () => string) {
613+
function getOutputJSFileName(inputFileName: string, configFile: ParsedCommandLine, ignoreCase: boolean, getCommonSourceDirectory = () => getCommonSourceDirectoryOfConfig(configFile, ignoreCase)) {
604614
if (configFile.options.emitDeclarationOnly) return undefined;
605615
const isJsonFile = fileExtensionIs(inputFileName, Extension.Json);
606-
const outputFileName = changeExtension(
607-
getOutputPathWithoutChangingExt(inputFileName, configFile, ignoreCase, configFile.options.outDir, getCommonSourceDirectory),
608-
getOutputExtension(inputFileName, configFile.options),
609-
);
616+
const outputFileName = getOutputJSFileNameWorker(inputFileName, configFile.options, ignoreCase, getCommonSourceDirectory);
610617
return !isJsonFile || comparePaths(inputFileName, outputFileName, Debug.checkDefined(configFile.options.configFilePath), ignoreCase) !== Comparison.EqualTo ?
611618
outputFileName :
612619
undefined;
613620
}
614621

622+
/** @internal */
623+
export function getOutputJSFileNameWorker(inputFileName: string, options: CompilerOptions, ignoreCase: boolean, getCommonSourceDirectory: () => string): string {
624+
return changeExtension(
625+
getOutputPathWithoutChangingExt(inputFileName, ignoreCase, options.outDir, getCommonSourceDirectory),
626+
getOutputExtension(inputFileName, options),
627+
);
628+
}
629+
615630
function createAddOutput() {
616631
let outputs: string[] | undefined;
617632
return { addOutput, getOutputs };

Diff for: src/compiler/moduleNameResolver.ts

+5
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,11 @@ export function isPackageJsonInfo(entry: PackageJsonInfoCacheEntry | undefined):
915915
return !!(entry as PackageJsonInfo | undefined)?.contents;
916916
}
917917

918+
/** @internal */
919+
export function isMissingPackageJsonInfo(entry: PackageJsonInfoCacheEntry | undefined): entry is MissingPackageJsonInfo {
920+
return !!entry && !(entry as PackageJsonInfo).contents;
921+
}
922+
918923
export interface PackageJsonInfoCache {
919924
/** @internal */ getPackageJsonInfo(packageJsonPath: string): PackageJsonInfoCacheEntry | undefined;
920925
/** @internal */ setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfoCacheEntry): void;

0 commit comments

Comments
 (0)