Skip to content

Commit d342492

Browse files
committed
path completions adjustments
1 parent 84f7d14 commit d342492

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
forEach,
3333
forEachAncestorDirectory,
3434
GetCanonicalFileName,
35+
getBaseFileName,
3536
getDirectoryPath,
3637
getEmitModuleResolutionKind,
3738
getImpliedNodeFormatForFile,
@@ -84,6 +85,7 @@ import {
8485
pathIsBareSpecifier,
8586
pathIsRelative,
8687
PropertyAccessExpression,
88+
removeExtension,
8789
removeFileExtension,
8890
removeSuffix,
8991
ResolutionMode,
@@ -1043,8 +1045,7 @@ function removeExtensionAndIndexPostFix(fileName: string, ending: Ending, option
10431045
if (fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Dcts, Extension.Cts])) return noExtension + getJSExtensionForFile(fileName, options);
10441046
else if (!fileExtensionIsOneOf(fileName, [Extension.Dts]) && fileExtensionIsOneOf(fileName, [Extension.Ts]) && stringContains(fileName, ".d.")) {
10451047
// `foo.d.json.ts` and the like - remap back to `foo.json`
1046-
const ext = noExtension.substring(noExtension.lastIndexOf("."));
1047-
return noExtension.substring(0, noExtension.indexOf(".d.")) + ext;
1048+
return tryGetRealFileNameForNonJsDeclarationFileName(fileName)!;
10481049
}
10491050
switch (ending) {
10501051
case Ending.Minimal:
@@ -1068,6 +1069,16 @@ function getJSExtensionForFile(fileName: string, options: CompilerOptions): Exte
10681069
return tryGetJSExtensionForFile(fileName, options) ?? Debug.fail(`Extension ${extensionFromPath(fileName)} is unsupported:: FileName:: ${fileName}`);
10691070
}
10701071

1072+
1073+
/** @internal */
1074+
export function tryGetRealFileNameForNonJsDeclarationFileName(fileName: string) {
1075+
const baseName = getBaseFileName(fileName);
1076+
if (!endsWith(fileName, Extension.Ts) || !stringContains(baseName, ".d.") || fileExtensionIsOneOf(baseName, [Extension.Dts])) return undefined;
1077+
const noExtension = removeExtension(fileName, Extension.Ts);
1078+
const ext = noExtension.substring(noExtension.lastIndexOf("."));
1079+
return noExtension.substring(0, noExtension.indexOf(".d.")) + ext;
1080+
}
1081+
10711082
/** @internal */
10721083
export function tryGetJSExtensionForFile(fileName: string, options: CompilerOptions): Extension | undefined {
10731084
const ext = tryGetExtensionFromPath(fileName);

src/services/stringCompletions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ function getCompletionEntriesForDirectoryFragment(
690690
}
691691

692692
function getFilenameWithExtensionOption(name: string, compilerOptions: CompilerOptions, includeExtensionsOption: IncludeExtensionsOption): { name: string, extension: Extension | undefined } {
693+
const nonJsResult = moduleSpecifiers.tryGetRealFileNameForNonJsDeclarationFileName(name);
694+
if (nonJsResult) {
695+
return { name: nonJsResult, extension: undefined };
696+
}
693697
const outputExtension = moduleSpecifiers.tryGetJSExtensionForFile(name, compilerOptions);
694698
if (includeExtensionsOption === IncludeExtensionsOption.Exclude && !fileExtensionIsOneOf(name, [Extension.Json, Extension.Mts, Extension.Cts, Extension.Dmts, Extension.Dcts, Extension.Mjs, Extension.Cjs])) {
695699
return { name: removeFileExtension(name), extension: tryGetExtensionFromPath(name) };
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[
2+
{
3+
"marker": {
4+
"fileName": "/project/usage.ts",
5+
"position": 35,
6+
"name": "1"
7+
},
8+
"completionList": {
9+
"isGlobalCompletion": false,
10+
"isMemberCompletion": false,
11+
"isNewIdentifierLocation": true,
12+
"entries": [
13+
{
14+
"name": "mod.html",
15+
"kind": "script",
16+
"kindModifiers": "",
17+
"sortText": "11",
18+
"displayParts": [
19+
{
20+
"text": "mod.html",
21+
"kind": "text"
22+
}
23+
],
24+
"tags": []
25+
},
26+
{
27+
"name": "node_modules",
28+
"kind": "directory",
29+
"kindModifiers": "",
30+
"sortText": "11",
31+
"displayParts": [
32+
{
33+
"text": "node_modules",
34+
"kind": "text"
35+
}
36+
],
37+
"tags": []
38+
}
39+
]
40+
}
41+
},
42+
{
43+
"marker": {
44+
"fileName": "/project/usage.ts",
45+
"position": 86,
46+
"name": "2"
47+
},
48+
"completionList": {
49+
"isGlobalCompletion": false,
50+
"isMemberCompletion": false,
51+
"isNewIdentifierLocation": true,
52+
"entries": [
53+
{
54+
"name": "mod.html",
55+
"kind": "script",
56+
"kindModifiers": "",
57+
"sortText": "11",
58+
"displayParts": [
59+
{
60+
"text": "mod.html",
61+
"kind": "text"
62+
}
63+
],
64+
"tags": []
65+
}
66+
]
67+
}
68+
}
69+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @allowNonJsExtensions: true
4+
// @Filename: /project/mod.d.html.ts
5+
////export declare class HtmlModuleThing {}
6+
7+
// @Filename: /project/node_modules/package/mod.d.html.ts
8+
////export declare class PackageHtmlModuleThing {}
9+
10+
// @Filename: /project/usage.ts
11+
////import { HtmlModuleThing } from ".//*1*/";
12+
////import { PackageHtmlModuleThing } from "package//*2*/";
13+
14+
verify.baselineCompletions();

0 commit comments

Comments
 (0)