Skip to content

Handle namepaths inside JSDoc type expressions a bit better #32563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2431,6 +2431,25 @@ namespace ts {

function parseJSDocType(): TypeNode {
scanner.setInJSDocType(true);
const moduleSpecifier = parseOptionalToken(SyntaxKind.ModuleKeyword);
if (moduleSpecifier) {
const moduleTag = createNode(SyntaxKind.JSDocNamepathType, moduleSpecifier.pos) as JSDocNamepathType;
terminate: while (true) {
switch (token()) {
case SyntaxKind.CloseBraceToken:
case SyntaxKind.EndOfFileToken:
case SyntaxKind.CommaToken:
case SyntaxKind.WhitespaceTrivia:
break terminate;
default:
nextTokenJSDoc();
}
}

scanner.setInJSDocType(false);
return finishNode(moduleTag);
}

const dotdotdot = parseOptionalToken(SyntaxKind.DotDotDotToken);
let type = parseTypeOrTypePredicate();
scanner.setInJSDocType(false);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ namespace ts {
// First non-whitespace character on this line.
let firstNonWhitespace = 0;
// These initial values are special because the first line is:
// firstNonWhitespace = 0 to indicate that we want leading whitspace,
// firstNonWhitespace = 0 to indicate that we want leading whitespace,

while (pos < end) {
char = text.charCodeAt(pos);
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ namespace ts {
JSDocOptionalType,
JSDocFunctionType,
JSDocVariadicType,
// https://jsdoc.app/about-namepaths.html
JSDocNamepathType,
JSDocComment,
JSDocTypeLiteral,
JSDocSignature,
Expand Down Expand Up @@ -2430,6 +2432,11 @@ namespace ts {
type: TypeNode;
}

export interface JSDocNamepathType extends JSDocType {
kind: SyntaxKind.JSDocNamepathType;
type: TypeNode;
}

export type JSDocTypeReferencingNode = JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;

export interface JSDoc extends Node {
Expand Down
4 changes: 4 additions & 0 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,10 @@ namespace FourSlash {

private verifySignatureHelpWorker(options: FourSlashInterface.VerifySignatureHelpOptions) {
const help = this.getSignatureHelp({ triggerReason: options.triggerReason })!;
if (!help) {
this.raiseError("Could not get a help signature");
}

const selectedItem = help.items[help.selectedItemIndex];
// Argument index may exceed number of parameters
const currentParameter = selectedItem.parameters[help.argumentIndex] as ts.SignatureHelpParameter | undefined;
Expand Down
1 change: 1 addition & 0 deletions src/services/classifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace ts {
/** The classifier is used for syntactic highlighting in editors via the TSServer */
export function createClassifier(): Classifier {
const scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ false);

Expand Down
58 changes: 32 additions & 26 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,29 +374,30 @@ declare namespace ts {
JSDocOptionalType = 294,
JSDocFunctionType = 295,
JSDocVariadicType = 296,
JSDocComment = 297,
JSDocTypeLiteral = 298,
JSDocSignature = 299,
JSDocTag = 300,
JSDocAugmentsTag = 301,
JSDocAuthorTag = 302,
JSDocClassTag = 303,
JSDocCallbackTag = 304,
JSDocEnumTag = 305,
JSDocParameterTag = 306,
JSDocReturnTag = 307,
JSDocThisTag = 308,
JSDocTypeTag = 309,
JSDocTemplateTag = 310,
JSDocTypedefTag = 311,
JSDocPropertyTag = 312,
SyntaxList = 313,
NotEmittedStatement = 314,
PartiallyEmittedExpression = 315,
CommaListExpression = 316,
MergeDeclarationMarker = 317,
EndOfDeclarationMarker = 318,
Count = 319,
JSDocNamepathType = 297,
JSDocComment = 298,
JSDocTypeLiteral = 299,
JSDocSignature = 300,
JSDocTag = 301,
JSDocAugmentsTag = 302,
JSDocAuthorTag = 303,
JSDocClassTag = 304,
JSDocCallbackTag = 305,
JSDocEnumTag = 306,
JSDocParameterTag = 307,
JSDocReturnTag = 308,
JSDocThisTag = 309,
JSDocTypeTag = 310,
JSDocTemplateTag = 311,
JSDocTypedefTag = 312,
JSDocPropertyTag = 313,
SyntaxList = 314,
NotEmittedStatement = 315,
PartiallyEmittedExpression = 316,
CommaListExpression = 317,
MergeDeclarationMarker = 318,
EndOfDeclarationMarker = 319,
Count = 320,
FirstAssignment = 60,
LastAssignment = 72,
FirstCompoundAssignment = 61,
Expand All @@ -423,9 +424,9 @@ declare namespace ts {
LastBinaryOperator = 72,
FirstNode = 149,
FirstJSDocNode = 289,
LastJSDocNode = 312,
FirstJSDocTagNode = 300,
LastJSDocTagNode = 312,
LastJSDocNode = 313,
FirstJSDocTagNode = 301,
LastJSDocTagNode = 313,
}
export enum NodeFlags {
None = 0,
Expand Down Expand Up @@ -1558,6 +1559,10 @@ declare namespace ts {
kind: SyntaxKind.JSDocVariadicType;
type: TypeNode;
}
export interface JSDocNamepathType extends JSDocType {
kind: SyntaxKind.JSDocNamepathType;
type: TypeNode;
}
export type JSDocTypeReferencingNode = JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
export interface JSDoc extends Node {
kind: SyntaxKind.JSDocComment;
Expand Down Expand Up @@ -5671,6 +5676,7 @@ declare namespace ts {
}
}
declare namespace ts {
/** The classifier is used for syntactic highlighting in editors via the TSServer */
function createClassifier(): Classifier;
}
declare namespace ts {
Expand Down
58 changes: 32 additions & 26 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,29 +374,30 @@ declare namespace ts {
JSDocOptionalType = 294,
JSDocFunctionType = 295,
JSDocVariadicType = 296,
JSDocComment = 297,
JSDocTypeLiteral = 298,
JSDocSignature = 299,
JSDocTag = 300,
JSDocAugmentsTag = 301,
JSDocAuthorTag = 302,
JSDocClassTag = 303,
JSDocCallbackTag = 304,
JSDocEnumTag = 305,
JSDocParameterTag = 306,
JSDocReturnTag = 307,
JSDocThisTag = 308,
JSDocTypeTag = 309,
JSDocTemplateTag = 310,
JSDocTypedefTag = 311,
JSDocPropertyTag = 312,
SyntaxList = 313,
NotEmittedStatement = 314,
PartiallyEmittedExpression = 315,
CommaListExpression = 316,
MergeDeclarationMarker = 317,
EndOfDeclarationMarker = 318,
Count = 319,
JSDocNamepathType = 297,
JSDocComment = 298,
JSDocTypeLiteral = 299,
JSDocSignature = 300,
JSDocTag = 301,
JSDocAugmentsTag = 302,
JSDocAuthorTag = 303,
JSDocClassTag = 304,
JSDocCallbackTag = 305,
JSDocEnumTag = 306,
JSDocParameterTag = 307,
JSDocReturnTag = 308,
JSDocThisTag = 309,
JSDocTypeTag = 310,
JSDocTemplateTag = 311,
JSDocTypedefTag = 312,
JSDocPropertyTag = 313,
SyntaxList = 314,
NotEmittedStatement = 315,
PartiallyEmittedExpression = 316,
CommaListExpression = 317,
MergeDeclarationMarker = 318,
EndOfDeclarationMarker = 319,
Count = 320,
FirstAssignment = 60,
LastAssignment = 72,
FirstCompoundAssignment = 61,
Expand All @@ -423,9 +424,9 @@ declare namespace ts {
LastBinaryOperator = 72,
FirstNode = 149,
FirstJSDocNode = 289,
LastJSDocNode = 312,
FirstJSDocTagNode = 300,
LastJSDocTagNode = 312,
LastJSDocNode = 313,
FirstJSDocTagNode = 301,
LastJSDocTagNode = 313,
}
export enum NodeFlags {
None = 0,
Expand Down Expand Up @@ -1558,6 +1559,10 @@ declare namespace ts {
kind: SyntaxKind.JSDocVariadicType;
type: TypeNode;
}
export interface JSDocNamepathType extends JSDocType {
kind: SyntaxKind.JSDocNamepathType;
type: TypeNode;
}
export type JSDocTypeReferencingNode = JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
export interface JSDoc extends Node {
kind: SyntaxKind.JSDocComment;
Expand Down Expand Up @@ -5671,6 +5676,7 @@ declare namespace ts {
}
}
declare namespace ts {
/** The classifier is used for syntactic highlighting in editors via the TSServer */
function createClassifier(): Classifier;
}
declare namespace ts {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
tests/cases/conformance/jsdoc/bug26693.js(1,15): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
tests/cases/conformance/jsdoc/bug26693.js(1,21): error TS1005: '}' expected.
tests/cases/conformance/jsdoc/bug26693.js(2,22): error TS2307: Cannot find module 'nope'.


==== tests/cases/conformance/jsdoc/bug26693.js (3 errors) ====
==== tests/cases/conformance/jsdoc/bug26693.js (1 errors) ====
/** @typedef {module:locale} hi */
~~~~~~
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
~
!!! error TS1005: '}' expected.
import { nope } from 'nope';
~~~~~~
!!! error TS2307: Cannot find module 'nope'.
Expand Down
45 changes: 45 additions & 0 deletions tests/cases/fourslash/jsDocDontBreakWithNamespaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
///<reference path="fourslash.ts" />
// @allowJs: true
// @Filename: 31298.js
/////**
//// * @returns {module:@nodefuel/web~Webserver~wsServer#hello} Websocket server object
//// */
////function foo() { }
////foo(''/*foo*/);
////
/////**
//// * @type {module:xxxxx} */
//// */
////function bar() { }
////bar(''/*bar*/);
////
/////** @type {function(module:xxxx, module:xxxx): module:xxxxx} */
////function zee() { }
////zee(''/*zee*/);


verify.signatureHelp({
marker: "foo",
text: "foo(): any",
docComment: "",
tags: [
{ name: "returns", text: "Websocket server object" },
],
});

verify.signatureHelp({
marker: "bar",
text: "bar(): void",
docComment: "",
tags: [],
});


verify.signatureHelp({
marker: "zee",
text: "zee(): any",
docComment: "",
tags: [
{ name: "type", text: "{function(module:xxxx, module:xxxx): module:xxxxx}" },
],
});