Skip to content

Commit

Permalink
Fix an off-by-one error in getBaseDirectoriesFromRootDirs (#55233)
Browse files Browse the repository at this point in the history
Co-authored-by: Phoebe Szmucer <[email protected]>
  • Loading branch information
PhoebeSzmucer and Phoebe Szmucer authored Sep 11, 2023
1 parent fdb2531 commit eb2d1f9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,14 @@ function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOpti
*/
function getBaseDirectoriesFromRootDirs(rootDirs: string[], basePath: string, scriptDirectory: string, ignoreCase: boolean): readonly string[] {
// Make all paths absolute/normalized if they are not already
rootDirs = rootDirs.map(rootDirectory => normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory)));
rootDirs = rootDirs.map(rootDirectory => ensureTrailingDirectorySeparator(normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory))));

// 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

// Now find a path for each potential directory that is to be merged with the one containing the script
return deduplicate<string>(
[...rootDirs.map(rootDirectory => combinePaths(rootDirectory, relativeDirectory)), scriptDirectory],
[...rootDirs.map(rootDirectory => combinePaths(rootDirectory, relativeDirectory)), scriptDirectory].map(baseDir => removeTrailingDirectorySeparator(baseDir)),
equateStringsCaseSensitive,
compareStringsCaseSensitive,
);
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,
}
);

0 comments on commit eb2d1f9

Please sign in to comment.