Skip to content

Fix implied formats, file watching, new source file creating during edits #50098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,22 @@
"category": "Message",
"code": 1457
},
"File is ECMAScript module because '{0}' has field \"type\" with value \"module\"": {
"category": "Message",
"code": 1458
},
"File is CommonJS module because '{0}' has field \"type\" whose value is not \"module\"": {
"category": "Message",
"code": 1459
},
"File is CommonJS module because '{0}' does not have field \"type\"": {
"category": "Message",
"code": 1460
},
"File is CommonJS module because 'package.json' was not found": {
"category": "Message",
"code": 1461
},

"The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.": {
"category": "Error",
Expand Down
34 changes: 25 additions & 9 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ namespace ts {
}

export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
/*@internal*/ clearAllExceptPackageJsonInfoCache(): void;
}

export interface ModeAwareCache<T> {
Expand Down Expand Up @@ -570,6 +571,7 @@ namespace ts {

export interface ModuleResolutionCache extends PerDirectoryResolutionCache<ResolvedModuleWithFailedLookupLocations>, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
getPackageJsonInfoCache(): PackageJsonInfoCache;
/*@internal*/ clearAllExceptPackageJsonInfoCache(): void;
}

/**
Expand All @@ -584,6 +586,7 @@ namespace ts {
/*@internal*/ getPackageJsonInfo(packageJsonPath: string): PackageJsonInfo | boolean | undefined;
/*@internal*/ setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfo | boolean): void;
/*@internal*/ entries(): [Path, PackageJsonInfo | boolean][];
/*@internal*/ getInternalMap(): ESMap<Path, PackageJsonInfo | boolean> | undefined;
clear(): void;
}

Expand Down Expand Up @@ -649,7 +652,7 @@ namespace ts {

function createPackageJsonInfoCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): PackageJsonInfoCache {
let cache: ESMap<Path, PackageJsonInfo | boolean> | undefined;
return { getPackageJsonInfo, setPackageJsonInfo, clear, entries };
return { getPackageJsonInfo, setPackageJsonInfo, clear, entries, getInternalMap };
function getPackageJsonInfo(packageJsonPath: string) {
return cache?.get(toPath(packageJsonPath, currentDirectory, getCanonicalFileName));
}
Expand All @@ -663,6 +666,9 @@ namespace ts {
const iter = cache?.entries();
return iter ? arrayFrom(iter) : [];
}
function getInternalMap() {
return cache;
}
}

function getOrCreateCache<T>(cacheWithRedirects: CacheWithRedirects<T>, redirectedReference: ResolvedProjectReference | undefined, key: string, create: () => T): T {
Expand Down Expand Up @@ -797,25 +803,30 @@ namespace ts {
directoryToModuleNameMap?: CacheWithRedirects<ModeAwareCache<ResolvedModuleWithFailedLookupLocations>>,
moduleNameToDirectoryMap?: CacheWithRedirects<PerModuleNameCache>,
): ModuleResolutionCache {
const preDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
const perDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
moduleNameToDirectoryMap ||= createCacheWithRedirects(options);
const packageJsonInfoCache = createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);

return {
...packageJsonInfoCache,
...preDirectoryResolutionCache,
...perDirectoryResolutionCache,
getOrCreateCacheForModuleName,
clear,
update,
getPackageJsonInfoCache: () => packageJsonInfoCache,
clearAllExceptPackageJsonInfoCache,
};

function clear() {
preDirectoryResolutionCache.clear();
moduleNameToDirectoryMap!.clear();
clearAllExceptPackageJsonInfoCache();
packageJsonInfoCache.clear();
}

function clearAllExceptPackageJsonInfoCache() {
perDirectoryResolutionCache.clear();
moduleNameToDirectoryMap!.clear();
}

function update(options: CompilerOptions) {
updateRedirectsMap(options, directoryToModuleNameMap!, moduleNameToDirectoryMap);
}
Expand Down Expand Up @@ -919,19 +930,24 @@ namespace ts {
packageJsonInfoCache?: PackageJsonInfoCache | undefined,
directoryToModuleNameMap?: CacheWithRedirects<ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>>,
): TypeReferenceDirectiveResolutionCache {
const preDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
const perDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
packageJsonInfoCache ||= createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);

return {
...packageJsonInfoCache,
...preDirectoryResolutionCache,
...perDirectoryResolutionCache,
clear,
clearAllExceptPackageJsonInfoCache,
};

function clear() {
preDirectoryResolutionCache.clear();
clearAllExceptPackageJsonInfoCache();
packageJsonInfoCache!.clear();
}

function clearAllExceptPackageJsonInfoCache() {
perDirectoryResolutionCache.clear();
}
}

export function resolveModuleNameFromCache(moduleName: string, containingFile: string, cache: ModuleResolutionCache, mode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations | undefined {
Expand Down Expand Up @@ -1786,7 +1802,7 @@ namespace ts {
}

/*@internal*/
interface PackageJsonInfo {
export interface PackageJsonInfo {
packageDirectory: string;
packageJsonContent: PackageJsonPathFields;
versionPaths: VersionPaths | undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ namespace ts {
* check specified by `isFileProbablyExternalModule` will be used to set the field.
*/
setExternalModuleIndicator?: (file: SourceFile) => void;
/*@internal*/ packageJsonLocations?: readonly string[];
/*@internal*/ packageJsonScope?: PackageJsonInfo;
}

function setExternalModuleIndicator(sourceFile: SourceFile) {
Expand Down
Loading