Skip to content
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

Fix an off-by-one error in getBaseDirectoriesFromRootDirs #55233

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
5 changes: 4 additions & 1 deletion src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,10 @@ function getBaseDirectoriesFromRootDirs(rootDirs: string[], basePath: string, sc
rootDirs = rootDirs.map(rootDirectory => normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory)));
PhoebeSzmucer marked this conversation as resolved.
Show resolved Hide resolved

// Determine the path to the directory containing the script relative to the root directory it is contained within
const relativeDirectory = firstDefined(rootDirs, rootDirectory => containsPath(rootDirectory, scriptDirectory, basePath, ignoreCase) ? scriptDirectory.substr(rootDirectory.length) : undefined)!; // TODO: GH#18217
const relativeDirectory = firstDefined(rootDirs, rootDirectory => {
PhoebeSzmucer marked this conversation as resolved.
Show resolved Hide resolved
const rootDirectoryWithSlash = rootDirectory.endsWith("/") ? rootDirectory : rootDirectory + "/";
return containsPath(rootDirectoryWithSlash, scriptDirectory, basePath, ignoreCase) ? scriptDirectory.substr(rootDirectoryWithSlash.length) : undefined;
})!; // TODO: GH#18217

// Now find a path for each potential directory that is to be merged with the one containing the script
return deduplicate<string>(
Expand Down
37 changes: 37 additions & 0 deletions tests/cases/fourslash/completionForStringLiteralRelativeImport5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference path='fourslash.ts' />

// Should give correct completions for directories when invoked from within nested folders

// @rootDirs: /repo/src1,/repo/src2/,/repo/generated1,/repo/generated2/

// @Filename: /dir/secret_file.ts
//// /*secret_file*/

// @Filename: /repo/src1/dir/test1.ts
//// import * as foo1 from ".//*import_as1*/
//// import foo2 = require(".//*import_equals1*/
//// var foo3 = require(".//*require1*/

// @Filename: /repo/src2/dir/test2.ts
//// import * as foo1 from ".//*import_as2*/
//// import foo2 = require(".//*import_equals2*/
//// var foo3 = require(".//*require2*/

// @Filename: /repo/generated1/dir/f1.ts
//// /*f1*/

// @Filename: /repo/generated2/dir/f2.ts
//// /*f2*/

verify.completions(
{
marker: ["import_as1", "import_equals1", "require1"],
unsorted: ["f1", "f2", "test2"],
isNewIdentifierLocation: true,
},
{
marker: ["import_as2", "import_equals2", "require2"],
unsorted: ["f1", "f2", "test1"],
isNewIdentifierLocation: true,
}
);
34 changes: 34 additions & 0 deletions tests/cases/fourslash/completionForStringLiteralRelativeImport6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference path='fourslash.ts' />

// Should give correct completions for rootDirs regardless of the slash at the end

// @rootDirs: /repo/src1,/repo/src2/,/repo/generated1,/repo/generated2/

// @Filename: /repo/src1/test1.ts
//// import * as foo1 from "./dir//*import_as1*/
//// import foo2 = require("./dir//*import_equals1*/
//// var foo3 = require("./dir//*require1*/

// @Filename: /repo/src2/test2.ts
//// import * as foo1 from "./dir//*import_as2*/
//// import foo2 = require("./dir//*import_equals2*/
//// var foo3 = require("./dir//*require2*/

// @Filename: /repo/generated1/dir/f1.ts
//// /*f1*/

// @Filename: /repo/generated2/dir/f2.ts
//// /*f2*/

verify.completions(
{
marker: ["import_as1", "import_equals1", "require1"],
exact: ["f1", "f2"],
isNewIdentifierLocation: true,
},
{
marker: ["import_as2", "import_equals2", "require2"],
exact: ["f1", "f2"],
isNewIdentifierLocation: true,
}
);