From 1eb3082387d72016b72876a8e64ffaa00c7b5dd7 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 5 Sep 2018 11:34:27 -0700 Subject: [PATCH] Support completions inside JSDoc before EndOfFileToken (#25568) --- src/services/utilities.ts | 12 ++++------ tests/cases/fourslash/completionsPaths.ts | 6 +++-- .../fourslash/completionsPaths_importType.ts | 23 +++++++++++++++++++ .../fourslash/importTypeModuleCompletions.ts | 13 ----------- 4 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 tests/cases/fourslash/completionsPaths_importType.ts delete mode 100644 tests/cases/fourslash/importTypeModuleCompletions.ts diff --git a/src/services/utilities.ts b/src/services/utilities.ts index c3d4cd9a390..b8a235bbeb4 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -755,7 +755,7 @@ namespace ts { return result; function find(n: Node): Node | undefined { - if (isNonWhitespaceToken(n)) { + if (isNonWhitespaceToken(n) && n.kind !== SyntaxKind.EndOfFileToken) { return n; } @@ -786,16 +786,14 @@ namespace ts { } } - Debug.assert(startNode !== undefined || n.kind === SyntaxKind.SourceFile || isJSDocCommentContainingNode(n)); + Debug.assert(startNode !== undefined || n.kind === SyntaxKind.SourceFile || n.kind === SyntaxKind.EndOfFileToken || isJSDocCommentContainingNode(n)); // Here we know that none of child token nodes embrace the position, // the only known case is when position is at the end of the file. // Try to find the rightmost token in the file without filtering. // Namely we are skipping the check: 'position < node.end' - if (children.length) { - const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile); - return candidate && findRightmostToken(candidate, sourceFile); - } + const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile); + return candidate && findRightmostToken(candidate, sourceFile); } } @@ -1048,7 +1046,7 @@ namespace ts { function nodeHasTokens(n: Node, sourceFile: SourceFileLike): boolean { // If we have a token or node that has a non-zero width, it must have tokens. // Note: getWidth() does not take trivia into account. - return n.getWidth(sourceFile) !== 0; + return n.kind === SyntaxKind.EndOfFileToken ? !!(n as EndOfFileToken).jsDoc : n.getWidth(sourceFile) !== 0; } export function getNodeModifiers(node: Node): string { diff --git a/tests/cases/fourslash/completionsPaths.ts b/tests/cases/fourslash/completionsPaths.ts index e120b3dcb14..e875e3e2147 100644 --- a/tests/cases/fourslash/completionsPaths.ts +++ b/tests/cases/fourslash/completionsPaths.ts @@ -23,5 +23,7 @@ // @Filename: /src/folder/4.ts ////const foo = require(`x//*4*/`); -verify.completionsAt("1", ["y", "x"], { isNewIdentifierLocation: true }); -verify.completionsAt(["2", "3", "4"], ["bar", "foo"], { isNewIdentifierLocation: true }); +verify.completions( + { marker: "1", exact: ["y", "x"], isNewIdentifierLocation: true }, + { marker: ["2", "3", "4"], exact: ["bar", "foo"], isNewIdentifierLocation: true }, +); diff --git a/tests/cases/fourslash/completionsPaths_importType.ts b/tests/cases/fourslash/completionsPaths_importType.ts new file mode 100644 index 00000000000..4d351f2494e --- /dev/null +++ b/tests/cases/fourslash/completionsPaths_importType.ts @@ -0,0 +1,23 @@ +/// + +// @allowJs: true +// @moduleResolution: node + +// @Filename: /ns.ts +////file content not read + +// @Filename: /node_modules/package/index.ts +////file content not read + +// @Filename: /usage.ts +////type A = typeof import("p/*1*/"); +////type B = import(".//*2*/"); + +// @Filename: /user.js +/////** @type {import("/*3*/")} */ + +verify.completions( + { marker: "1", exact: "package", isNewIdentifierLocation: true }, + { marker: "2", exact: ["lib", "ns", "user", "node_modules"], isNewIdentifierLocation: true }, + { marker: "3", exact: ["package"], isNewIdentifierLocation: true }, +); diff --git a/tests/cases/fourslash/importTypeModuleCompletions.ts b/tests/cases/fourslash/importTypeModuleCompletions.ts deleted file mode 100644 index 2a2f17818d4..00000000000 --- a/tests/cases/fourslash/importTypeModuleCompletions.ts +++ /dev/null @@ -1,13 +0,0 @@ -/// - -// @moduleResolution: node - -// @Filename: /ns.ts -////file content not read -// @Filename: /node_modules/package/index.ts -////file content not read -// @Filename: /usage.ts -////type A = typeof import("p/*1*/"); -////type B = typeof import(".//*2*/"); -verify.completionsAt("1", ["package"], { isNewIdentifierLocation: true }); -verify.completionsAt("2", ["lib", "ns", "node_modules"], { isNewIdentifierLocation: true });