Skip to content

Commit 1eb3082

Browse files
author
Andy
authored
Support completions inside JSDoc before EndOfFileToken (#25568)
1 parent 62d8b85 commit 1eb3082

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

src/services/utilities.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ namespace ts {
755755
return result;
756756

757757
function find(n: Node): Node | undefined {
758-
if (isNonWhitespaceToken(n)) {
758+
if (isNonWhitespaceToken(n) && n.kind !== SyntaxKind.EndOfFileToken) {
759759
return n;
760760
}
761761

@@ -786,16 +786,14 @@ namespace ts {
786786
}
787787
}
788788

789-
Debug.assert(startNode !== undefined || n.kind === SyntaxKind.SourceFile || isJSDocCommentContainingNode(n));
789+
Debug.assert(startNode !== undefined || n.kind === SyntaxKind.SourceFile || n.kind === SyntaxKind.EndOfFileToken || isJSDocCommentContainingNode(n));
790790

791791
// Here we know that none of child token nodes embrace the position,
792792
// the only known case is when position is at the end of the file.
793793
// Try to find the rightmost token in the file without filtering.
794794
// Namely we are skipping the check: 'position < node.end'
795-
if (children.length) {
796-
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile);
797-
return candidate && findRightmostToken(candidate, sourceFile);
798-
}
795+
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile);
796+
return candidate && findRightmostToken(candidate, sourceFile);
799797
}
800798
}
801799

@@ -1048,7 +1046,7 @@ namespace ts {
10481046
function nodeHasTokens(n: Node, sourceFile: SourceFileLike): boolean {
10491047
// If we have a token or node that has a non-zero width, it must have tokens.
10501048
// Note: getWidth() does not take trivia into account.
1051-
return n.getWidth(sourceFile) !== 0;
1049+
return n.kind === SyntaxKind.EndOfFileToken ? !!(n as EndOfFileToken).jsDoc : n.getWidth(sourceFile) !== 0;
10521050
}
10531051

10541052
export function getNodeModifiers(node: Node): string {

tests/cases/fourslash/completionsPaths.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@
2323
// @Filename: /src/folder/4.ts
2424
////const foo = require(`x//*4*/`);
2525

26-
verify.completionsAt("1", ["y", "x"], { isNewIdentifierLocation: true });
27-
verify.completionsAt(["2", "3", "4"], ["bar", "foo"], { isNewIdentifierLocation: true });
26+
verify.completions(
27+
{ marker: "1", exact: ["y", "x"], isNewIdentifierLocation: true },
28+
{ marker: ["2", "3", "4"], exact: ["bar", "foo"], isNewIdentifierLocation: true },
29+
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @moduleResolution: node
5+
6+
// @Filename: /ns.ts
7+
////file content not read
8+
9+
// @Filename: /node_modules/package/index.ts
10+
////file content not read
11+
12+
// @Filename: /usage.ts
13+
////type A = typeof import("p/*1*/");
14+
////type B = import(".//*2*/");
15+
16+
// @Filename: /user.js
17+
/////** @type {import("/*3*/")} */
18+
19+
verify.completions(
20+
{ marker: "1", exact: "package", isNewIdentifierLocation: true },
21+
{ marker: "2", exact: ["lib", "ns", "user", "node_modules"], isNewIdentifierLocation: true },
22+
{ marker: "3", exact: ["package"], isNewIdentifierLocation: true },
23+
);

tests/cases/fourslash/importTypeModuleCompletions.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)