mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Change JS node core module heuristic to same-file utilization
This commit is contained in:
3 files changed
+18
-23
No files matched your search
@@ -532,12 +532,6 @@ namespace ts.server {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -285,7 +285,7 @@ namespace ts.codefix {
|
||||
preferences: UserPreferences,
|
||||
): ReadonlyArray<FixAddNewImport | FixUseImportType> {
|
||||
const isJs = isSourceFileJS(sourceFile);
|
||||
const { allowsImporting } = createLazyPackageJsonDependencyReader(sourceFile.fileName, host);
|
||||
const { allowsImporting } = createLazyPackageJsonDependencyReader(sourceFile, host);
|
||||
const choicesForEachExportingModule = flatMap(moduleSymbols, ({ moduleSymbol, importKind, exportedSymbolIsTypeOnly }) =>
|
||||
moduleSpecifiers.getModuleSpecifiers(moduleSymbol, program.getCompilerOptions(), sourceFile, host, program.getSourceFiles(), preferences, program.redirectTargetsMap)
|
||||
.map((moduleSpecifier): FixAddNewImport | FixUseImportType =>
|
||||
@@ -294,8 +294,8 @@ namespace ts.codefix {
|
||||
|
||||
// Sort by presence in package.json, then shortest paths first
|
||||
return sort(choicesForEachExportingModule, (a, b) => {
|
||||
const allowsImportingA = allowsImporting(a.moduleSpecifier, isJs);
|
||||
const allowsImportingB = allowsImporting(b.moduleSpecifier, isJs);
|
||||
const allowsImportingA = allowsImporting(a.moduleSpecifier);
|
||||
const allowsImportingB = allowsImporting(b.moduleSpecifier);
|
||||
if (allowsImportingA && !allowsImportingB) {
|
||||
return -1;
|
||||
}
|
||||
@@ -580,15 +580,14 @@ namespace ts.codefix {
|
||||
}
|
||||
|
||||
export function forEachExternalModuleToImportFrom(checker: TypeChecker, host: LanguageServiceHost, from: SourceFile, allSourceFiles: ReadonlyArray<SourceFile>, cb: (module: Symbol) => void) {
|
||||
const { allowsImporting } = createLazyPackageJsonDependencyReader(from.fileName, host);
|
||||
const isJS = isSourceFileJS(from);
|
||||
const { allowsImporting } = createLazyPackageJsonDependencyReader(from, host);
|
||||
forEachExternalModule(checker, allSourceFiles, (module, sourceFile) => {
|
||||
if (sourceFile === undefined && allowsImporting(stripQuotes(module.getName()), isJS)) {
|
||||
if (sourceFile === undefined && allowsImporting(stripQuotes(module.getName()))) {
|
||||
cb(module);
|
||||
}
|
||||
else if (sourceFile && sourceFile !== from && isImportablePath(from.fileName, sourceFile.fileName)) {
|
||||
const moduleSpecifier = getModuleSpecifierFromFileName(sourceFile.fileName);
|
||||
if (!moduleSpecifier || allowsImporting(moduleSpecifier, isJS)) {
|
||||
if (!moduleSpecifier || allowsImporting(moduleSpecifier)) {
|
||||
cb(module);
|
||||
}
|
||||
}
|
||||
@@ -665,8 +664,8 @@ namespace ts.codefix {
|
||||
return !isStringANonContextualKeyword(res) ? res || "_" : `_${res}`;
|
||||
}
|
||||
|
||||
function createLazyPackageJsonDependencyReader(fromPath: string, host: LanguageServiceHost) {
|
||||
const packageJsonPaths = findPackageJsons(getDirectoryPath(fromPath), host);
|
||||
function createLazyPackageJsonDependencyReader(fromFile: SourceFile, host: LanguageServiceHost) {
|
||||
const packageJsonPaths = findPackageJsons(getDirectoryPath(fromFile.fileName), host);
|
||||
const dependencyIterator = readPackageJsonDependencies();
|
||||
let seenDeps: Map<true> | undefined;
|
||||
function *readPackageJsonDependencies() {
|
||||
@@ -700,8 +699,8 @@ namespace ts.codefix {
|
||||
return false;
|
||||
}
|
||||
|
||||
let hasNodeTypingsViaTypeAcquisition: boolean | undefined;
|
||||
function allowsImporting(moduleSpecifier: string, fromJSFile: boolean): boolean {
|
||||
let usesNodeCoreModules: boolean | undefined;
|
||||
function allowsImporting(moduleSpecifier: string): boolean {
|
||||
if (!packageJsonPaths.length) {
|
||||
return true;
|
||||
}
|
||||
@@ -711,11 +710,11 @@ namespace ts.codefix {
|
||||
// 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 (fromJSFile && JsTyping.nodeCoreModules.has(moduleSpecifier)) {
|
||||
if (hasNodeTypingsViaTypeAcquisition === undefined) {
|
||||
hasNodeTypingsViaTypeAcquisition = host.hasTypeAcquisitionAcquiredNodeTypings && host.hasTypeAcquisitionAcquiredNodeTypings();
|
||||
if (isSourceFileJS(fromFile) && JsTyping.nodeCoreModules.has(moduleSpecifier)) {
|
||||
if (usesNodeCoreModules === undefined) {
|
||||
usesNodeCoreModules = consumesNodeCoreModules(fromFile);
|
||||
}
|
||||
if (hasNodeTypingsViaTypeAcquisition) {
|
||||
if (usesNodeCoreModules) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -730,4 +729,8 @@ namespace ts.codefix {
|
||||
allowsImporting
|
||||
};
|
||||
}
|
||||
|
||||
function consumesNodeCoreModules(sourceFile: SourceFile): boolean {
|
||||
return some(sourceFile.imports, ({ text }) => JsTyping.nodeCoreModules.has(text));
|
||||
}
|
||||
}
|
||||
@@ -236,8 +236,6 @@ 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