Skip to content

Commit d5779c7

Browse files
authored
replace whole path if directory separator appears for import completion. (#41412)
* replace whole path if directory separator appears. * Fix. * revert change. * Fix test case. * add test. * fix as suggested. * revert useless change * adapt to code change. * fix baseline for test file.
1 parent 242e020 commit d5779c7

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

src/compiler/path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ts {
66
* we expect the host to correctly handle paths in our specified format.
77
*/
88
export const directorySeparator = "/";
9-
const altDirectorySeparator = "\\";
9+
export const altDirectorySeparator = "\\";
1010
const urlSchemeSeparator = "://";
1111
const backslashRegExp = /\\/g;
1212

src/services/stringCompletions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ namespace ts.Completions.StringCompletions {
279279

280280
function addReplacementSpans(text: string, textStart: number, names: readonly NameAndKind[]): readonly PathCompletion[] {
281281
const span = getDirectoryFragmentTextSpan(text, textStart);
282-
return names.map(({ name, kind, extension }): PathCompletion => ({ name, kind, extension, span }));
282+
const wholeSpan = text.length === 0 ? undefined : createTextSpan(textStart, text.length);
283+
return names.map(({ name, kind, extension }): PathCompletion =>
284+
Math.max(name.indexOf(directorySeparator), name.indexOf(altDirectorySeparator)) !== -1 ? { name, kind, extension, span: wholeSpan } : { name, kind, extension, span });
283285
}
284286

285287
function getStringLiteralCompletionsFromModuleNames(sourceFile: SourceFile, node: LiteralExpression, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): readonly PathCompletion[] {
@@ -683,7 +685,7 @@ namespace ts.Completions.StringCompletions {
683685

684686
// Replace everything after the last directory separator that appears
685687
function getDirectoryFragmentTextSpan(text: string, textStart: number): TextSpan | undefined {
686-
const index = Math.max(text.lastIndexOf(directorySeparator), text.lastIndexOf("\\"));
688+
const index = Math.max(text.lastIndexOf(directorySeparator), text.lastIndexOf(altDirectorySeparator));
687689
const offset = index !== -1 ? index + 1 : 0;
688690
// If the range is an identifier, span is unnecessary.
689691
const length = text.length - offset;

tests/cases/fourslash/completionForStringLiteralNonrelativeImport14.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,69 @@
2525
// @Filename: some/other/path.ts
2626
//// export var y = 10;
2727

28-
verify.completions({ marker: test.markerNames(), exact: ["lib", "tests", "/module1", "/module2"], isNewIdentifierLocation: true });
28+
verify.completions({
29+
marker: ["import_as0",],
30+
exact: ["lib", "tests",
31+
{
32+
name: "/module1",
33+
replacementSpan: {
34+
fileName: "foo",
35+
pos: 23,
36+
end: 24
37+
}
38+
},
39+
{
40+
name: "/module2",
41+
replacementSpan: {
42+
fileName: "foo",
43+
pos: 23,
44+
end: 24
45+
}
46+
}],
47+
isNewIdentifierLocation: true
48+
});
49+
50+
51+
verify.completions({
52+
marker: ["import_equals0",],
53+
exact: ["lib", "tests",
54+
{
55+
name: "/module1",
56+
replacementSpan: {
57+
fileName: "foo",
58+
pos: 48,
59+
end: 49
60+
}
61+
},
62+
{
63+
name: "/module2",
64+
replacementSpan: {
65+
fileName: "foo",
66+
pos: 48,
67+
end: 49
68+
}
69+
}],
70+
isNewIdentifierLocation: true
71+
});
72+
73+
verify.completions({
74+
marker: ["require0",],
75+
exact: ["lib", "tests",
76+
{
77+
name: "/module1",
78+
replacementSpan: {
79+
fileName: "foo",
80+
pos: 70,
81+
end: 71
82+
}
83+
},
84+
{
85+
name: "/module2",
86+
replacementSpan: {
87+
fileName: "foo",
88+
pos: 70,
89+
end: 71
90+
}
91+
}],
92+
isNewIdentifierLocation: true
93+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Should give completions for modules referenced via baseUrl and paths compiler options with explicit name mappings
4+
5+
// @Filename: tsconfig.json
6+
//// {
7+
//// "compilerOptions": {
8+
//// "baseUrl": "./",
9+
//// "paths": {
10+
//// "module1/path1": ["some/path/whatever.ts"],
11+
//// }
12+
//// }
13+
//// }
14+
15+
// @Filename: test0.ts
16+
//// import * as foo1 from "m/*first*/
17+
//// import * as foo1 from "module1/pa/*second*/
18+
19+
// @Filename: some/path/whatever.ts
20+
//// export var x = 9;
21+
22+
verify.completions({ marker: ["first"], exact: ["test0", "some", {
23+
name: "module1/path1",
24+
replacementSpan: {
25+
fileName: "foo",
26+
pos: 23,
27+
end: 24
28+
}
29+
}], isNewIdentifierLocation: true });
30+
31+
verify.completions({
32+
marker: ["second"], exact: [{
33+
name: "module1/path1",
34+
replacementSpan: {
35+
fileName: "foo",
36+
pos: 48,
37+
end: 58
38+
}
39+
}], isNewIdentifierLocation: true
40+
});

0 commit comments

Comments
 (0)