Proposal: importModuleSpecifierPreference: project-relative (#40637)

* Add new importModuleSpecifierPreference value

* Add second test

* Update API baselines

* Clean up and add some comments

* Rename option value
This commit is contained in:
Andrew Branch
2020-11-11 11:48:32 -08:00
committed by GitHub
parent de4a57afdc
commit 266d8de64a
13 changed files with 197 additions and 13 deletions

View File

@@ -3878,7 +3878,7 @@ namespace ts.server {
const info = packageJsonCache.getInDirectory(directory);
if (info) result.push(info);
}
if (rootPath && rootPath === this.toPath(directory)) {
if (rootPath && rootPath === directory) {
return true;
}
};
@@ -3887,6 +3887,20 @@ namespace ts.server {
return result;
}
/*@internal*/
getNearestAncestorDirectoryWithPackageJson(fileName: string): string | undefined {
return forEachAncestorDirectory(fileName, directory => {
switch (this.packageJsonCache.directoryHasPackageJson(this.toPath(directory))) {
case Ternary.True: return directory;
case Ternary.False: return undefined;
case Ternary.Maybe:
return this.host.fileExists(combinePaths(directory, "package.json"))
? directory
: undefined;
}
});
}
/*@internal*/
private watchPackageJsonFile(path: Path) {
const watchers = this.packageJsonFilesMap || (this.packageJsonFilesMap = new Map());

View File

@@ -1651,6 +1651,11 @@ namespace ts.server {
return this.projectService.getPackageJsonsVisibleToFile(fileName, rootDir);
}
/*@internal*/
getNearestAncestorDirectoryWithPackageJson(fileName: string): string | undefined {
return this.projectService.getNearestAncestorDirectoryWithPackageJson(fileName);
}
/*@internal*/
getPackageJsonsForAutoImport(rootDir?: string): readonly PackageJsonInfo[] {
const packageJsons = this.getPackageJsonsVisibleToFile(combinePaths(this.currentDirectory, inferredTypesContainingFile), rootDir);
@@ -1994,6 +1999,10 @@ namespace ts.server {
return super.updateGraph();
}
hasRoots() {
return !!this.rootFileNames?.length;
}
markAsDirty() {
this.rootFileNames = undefined;
super.markAsDirty();

View File

@@ -3231,7 +3231,7 @@ namespace ts.server.protocol {
* values, with insertion text to replace preceding `.` tokens with `?.`.
*/
readonly includeAutomaticOptionalChainCompletions?: boolean;
readonly importModuleSpecifierPreference?: "auto" | "relative" | "non-relative";
readonly importModuleSpecifierPreference?: "shortest" | "project-relative" | "relative" | "non-relative";
/** Determines whether we import `foo/index.ts` as "foo", "foo/index", or "foo/index.js" */
readonly importModuleSpecifierEnding?: "auto" | "minimal" | "index" | "js";
readonly allowTextChangesInNewFiles?: boolean;