mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Use type acquisition as a heuristic for whether a JS project is using node core
This commit is contained in:
4 files changed
+44
-9
No files matched your search
@@ -527,6 +527,17 @@ namespace ts.server {
|
||||
return this.projectService.getSourceFileLike(fileName, this);
|
||||
}
|
||||
|
||||
/*@internal*/
|
||||
getUnresolvedImports(): SortedReadonlyArray<string> | undefined {
|
||||
return this.typingsCache.getUnresolvedImports(this);
|
||||
}
|
||||
|
||||
/*@internal*/
|
||||
hasTypeAcquisitionAcquiredNodeTypings?(): boolean {
|
||||
const globalCache = this.getGlobalCache();
|
||||
return !!globalCache && some(this.typingFiles, fileName => startsWithDirectory(fileName, combinePaths(globalCache, "node_modules", "@types", "node"), this.getCanonicalFileName));
|
||||
}
|
||||
|
||||
private shouldEmitFile(scriptInfo: ScriptInfo) {
|
||||
return scriptInfo && !scriptInfo.isDynamicOrHasMixedContent();
|
||||
}
|
||||
|
||||
@@ -138,5 +138,12 @@ namespace ts.server {
|
||||
this.perProjectCache.delete(project.getProjectName());
|
||||
this.installer.onProjectClosed(project);
|
||||
}
|
||||
|
||||
getUnresolvedImports(project: Project): SortedReadonlyArray<string> | undefined {
|
||||
const entry = this.perProjectCache.get(project.getProjectName());
|
||||
if (entry) {
|
||||
return entry.unresolvedImports;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -570,25 +570,40 @@ namespace ts.codefix {
|
||||
export function forEachExternalModuleToImportFrom(checker: TypeChecker, host: LanguageServiceHost, from: SourceFile, allSourceFiles: ReadonlyArray<SourceFile>, cb: (module: Symbol) => void) {
|
||||
const packageJsons = findPackageJsons(getDirectoryPath(from.fileName), host);
|
||||
const dependencyIterator = readPackageJsonDependencies();
|
||||
const isJS = isSourceFileJS(from);
|
||||
forEachExternalModule(checker, allSourceFiles, (module, sourceFile) => {
|
||||
if (sourceFile === undefined) {
|
||||
const maybeSourceFile = module.valueDeclaration.getSourceFile();
|
||||
if (!maybeSourceFile || packageJsonAllowsImporting(maybeSourceFile.fileName)) {
|
||||
if (sourceFile === undefined && packageJsonAllowsImporting(stripQuotes(module.getName()))) {
|
||||
cb(module);
|
||||
}
|
||||
else if (sourceFile && sourceFile !== from && isImportablePath(from.fileName, sourceFile.fileName)) {
|
||||
const moduleSpecifier = getModuleSpecifierFromFileName(sourceFile.fileName);
|
||||
if (!moduleSpecifier || packageJsonAllowsImporting(moduleSpecifier)) {
|
||||
cb(module);
|
||||
}
|
||||
}
|
||||
else if (sourceFile && sourceFile !== from && isImportablePath(from.fileName, sourceFile.fileName) && packageJsonAllowsImporting(sourceFile.fileName)) {
|
||||
cb(module);
|
||||
}
|
||||
});
|
||||
|
||||
let seenDeps: Map<true> | undefined;
|
||||
function packageJsonAllowsImporting(fileName: string): boolean {
|
||||
const moduleSpecifier = getModuleSpecifierFromFileName(fileName);
|
||||
if (!moduleSpecifier || !packageJsons.length || seenDeps && seenDeps.has(moduleSpecifier)) {
|
||||
let hasNodeTypingsViaTypeAcquisition: boolean | undefined;
|
||||
function packageJsonAllowsImporting(moduleSpecifier: string): boolean {
|
||||
if (!packageJsons.length || seenDeps && seenDeps.has(moduleSpecifier)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If we’re in JavaScript, it can be difficult to tell whether the user wants to import
|
||||
// from Node core modules or not. We can start by seeing if type acquisition has already
|
||||
// downloaded @types/node, because if it has, that indicates that the user has already
|
||||
// been using some Node core modules directly, as opposed to simply having @types/node
|
||||
// accidentally as a dependency of a dependency.
|
||||
if (isJS && JsTyping.nodeCoreModules.has(moduleSpecifier)) {
|
||||
if (hasNodeTypingsViaTypeAcquisition === undefined) {
|
||||
hasNodeTypingsViaTypeAcquisition = host.hasTypeAcquisitionAcquiredNodeTypings && host.hasTypeAcquisitionAcquiredNodeTypings();
|
||||
}
|
||||
if (hasNodeTypingsViaTypeAcquisition) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
let packageName: string;
|
||||
while (packageName = dependencyIterator.next().value) {
|
||||
const nameWithoutTypes = getPackageNameFromTypesPackageName(packageName);
|
||||
|
||||
@@ -236,6 +236,8 @@ namespace ts {
|
||||
getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined;
|
||||
/* @internal */
|
||||
getSourceFileLike?(fileName: string): SourceFileLike | undefined;
|
||||
/* @internal */
|
||||
hasTypeAcquisitionAcquiredNodeTypings?(): boolean;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
|
||||
Reference in new issue
Block a user