Skip to content

Commit 2a2866c

Browse files
author
Orta
authored
Merge pull request microsoft#32563 from orta/fix_31298
Handle namepaths inside JSDoc type expressions a bit better
2 parents 98b6db8 + 5294e92 commit 2a2866c

File tree

9 files changed

+142
-60
lines changed

9 files changed

+142
-60
lines changed

src/compiler/parser.ts

+19
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,25 @@ namespace ts {
24352435

24362436
function parseJSDocType(): TypeNode {
24372437
scanner.setInJSDocType(true);
2438+
const moduleSpecifier = parseOptionalToken(SyntaxKind.ModuleKeyword);
2439+
if (moduleSpecifier) {
2440+
const moduleTag = createNode(SyntaxKind.JSDocNamepathType, moduleSpecifier.pos) as JSDocNamepathType;
2441+
terminate: while (true) {
2442+
switch (token()) {
2443+
case SyntaxKind.CloseBraceToken:
2444+
case SyntaxKind.EndOfFileToken:
2445+
case SyntaxKind.CommaToken:
2446+
case SyntaxKind.WhitespaceTrivia:
2447+
break terminate;
2448+
default:
2449+
nextTokenJSDoc();
2450+
}
2451+
}
2452+
2453+
scanner.setInJSDocType(false);
2454+
return finishNode(moduleTag);
2455+
}
2456+
24382457
const dotdotdot = parseOptionalToken(SyntaxKind.DotDotDotToken);
24392458
let type = parseTypeOrTypePredicate();
24402459
scanner.setInJSDocType(false);

src/compiler/scanner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ namespace ts {
20252025
// First non-whitespace character on this line.
20262026
let firstNonWhitespace = 0;
20272027
// These initial values are special because the first line is:
2028-
// firstNonWhitespace = 0 to indicate that we want leading whitspace,
2028+
// firstNonWhitespace = 0 to indicate that we want leading whitespace,
20292029

20302030
while (pos < end) {
20312031
char = text.charCodeAt(pos);

src/compiler/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ namespace ts {
455455
JSDocOptionalType,
456456
JSDocFunctionType,
457457
JSDocVariadicType,
458+
// https://jsdoc.app/about-namepaths.html
459+
JSDocNamepathType,
458460
JSDocComment,
459461
JSDocTypeLiteral,
460462
JSDocSignature,
@@ -2430,6 +2432,11 @@ namespace ts {
24302432
type: TypeNode;
24312433
}
24322434

2435+
export interface JSDocNamepathType extends JSDocType {
2436+
kind: SyntaxKind.JSDocNamepathType;
2437+
type: TypeNode;
2438+
}
2439+
24332440
export type JSDocTypeReferencingNode = JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
24342441

24352442
export interface JSDoc extends Node {

src/harness/fourslash.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,10 @@ namespace FourSlash {
12691269

12701270
private verifySignatureHelpWorker(options: FourSlashInterface.VerifySignatureHelpOptions) {
12711271
const help = this.getSignatureHelp({ triggerReason: options.triggerReason })!;
1272+
if (!help) {
1273+
this.raiseError("Could not get a help signature");
1274+
}
1275+
12721276
const selectedItem = help.items[help.selectedItemIndex];
12731277
// Argument index may exceed number of parameters
12741278
const currentParameter = selectedItem.parameters[help.argumentIndex] as ts.SignatureHelpParameter | undefined;

src/services/classifier.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace ts {
2+
/** The classifier is used for syntactic highlighting in editors via the TSServer */
23
export function createClassifier(): Classifier {
34
const scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ false);
45

tests/baselines/reference/api/tsserverlibrary.d.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -374,29 +374,30 @@ declare namespace ts {
374374
JSDocOptionalType = 294,
375375
JSDocFunctionType = 295,
376376
JSDocVariadicType = 296,
377-
JSDocComment = 297,
378-
JSDocTypeLiteral = 298,
379-
JSDocSignature = 299,
380-
JSDocTag = 300,
381-
JSDocAugmentsTag = 301,
382-
JSDocAuthorTag = 302,
383-
JSDocClassTag = 303,
384-
JSDocCallbackTag = 304,
385-
JSDocEnumTag = 305,
386-
JSDocParameterTag = 306,
387-
JSDocReturnTag = 307,
388-
JSDocThisTag = 308,
389-
JSDocTypeTag = 309,
390-
JSDocTemplateTag = 310,
391-
JSDocTypedefTag = 311,
392-
JSDocPropertyTag = 312,
393-
SyntaxList = 313,
394-
NotEmittedStatement = 314,
395-
PartiallyEmittedExpression = 315,
396-
CommaListExpression = 316,
397-
MergeDeclarationMarker = 317,
398-
EndOfDeclarationMarker = 318,
399-
Count = 319,
377+
JSDocNamepathType = 297,
378+
JSDocComment = 298,
379+
JSDocTypeLiteral = 299,
380+
JSDocSignature = 300,
381+
JSDocTag = 301,
382+
JSDocAugmentsTag = 302,
383+
JSDocAuthorTag = 303,
384+
JSDocClassTag = 304,
385+
JSDocCallbackTag = 305,
386+
JSDocEnumTag = 306,
387+
JSDocParameterTag = 307,
388+
JSDocReturnTag = 308,
389+
JSDocThisTag = 309,
390+
JSDocTypeTag = 310,
391+
JSDocTemplateTag = 311,
392+
JSDocTypedefTag = 312,
393+
JSDocPropertyTag = 313,
394+
SyntaxList = 314,
395+
NotEmittedStatement = 315,
396+
PartiallyEmittedExpression = 316,
397+
CommaListExpression = 317,
398+
MergeDeclarationMarker = 318,
399+
EndOfDeclarationMarker = 319,
400+
Count = 320,
400401
FirstAssignment = 60,
401402
LastAssignment = 72,
402403
FirstCompoundAssignment = 61,
@@ -423,9 +424,9 @@ declare namespace ts {
423424
LastBinaryOperator = 72,
424425
FirstNode = 149,
425426
FirstJSDocNode = 289,
426-
LastJSDocNode = 312,
427-
FirstJSDocTagNode = 300,
428-
LastJSDocTagNode = 312,
427+
LastJSDocNode = 313,
428+
FirstJSDocTagNode = 301,
429+
LastJSDocTagNode = 313,
429430
}
430431
export enum NodeFlags {
431432
None = 0,
@@ -1558,6 +1559,10 @@ declare namespace ts {
15581559
kind: SyntaxKind.JSDocVariadicType;
15591560
type: TypeNode;
15601561
}
1562+
export interface JSDocNamepathType extends JSDocType {
1563+
kind: SyntaxKind.JSDocNamepathType;
1564+
type: TypeNode;
1565+
}
15611566
export type JSDocTypeReferencingNode = JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
15621567
export interface JSDoc extends Node {
15631568
kind: SyntaxKind.JSDocComment;
@@ -5671,6 +5676,7 @@ declare namespace ts {
56715676
}
56725677
}
56735678
declare namespace ts {
5679+
/** The classifier is used for syntactic highlighting in editors via the TSServer */
56745680
function createClassifier(): Classifier;
56755681
}
56765682
declare namespace ts {

tests/baselines/reference/api/typescript.d.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -374,29 +374,30 @@ declare namespace ts {
374374
JSDocOptionalType = 294,
375375
JSDocFunctionType = 295,
376376
JSDocVariadicType = 296,
377-
JSDocComment = 297,
378-
JSDocTypeLiteral = 298,
379-
JSDocSignature = 299,
380-
JSDocTag = 300,
381-
JSDocAugmentsTag = 301,
382-
JSDocAuthorTag = 302,
383-
JSDocClassTag = 303,
384-
JSDocCallbackTag = 304,
385-
JSDocEnumTag = 305,
386-
JSDocParameterTag = 306,
387-
JSDocReturnTag = 307,
388-
JSDocThisTag = 308,
389-
JSDocTypeTag = 309,
390-
JSDocTemplateTag = 310,
391-
JSDocTypedefTag = 311,
392-
JSDocPropertyTag = 312,
393-
SyntaxList = 313,
394-
NotEmittedStatement = 314,
395-
PartiallyEmittedExpression = 315,
396-
CommaListExpression = 316,
397-
MergeDeclarationMarker = 317,
398-
EndOfDeclarationMarker = 318,
399-
Count = 319,
377+
JSDocNamepathType = 297,
378+
JSDocComment = 298,
379+
JSDocTypeLiteral = 299,
380+
JSDocSignature = 300,
381+
JSDocTag = 301,
382+
JSDocAugmentsTag = 302,
383+
JSDocAuthorTag = 303,
384+
JSDocClassTag = 304,
385+
JSDocCallbackTag = 305,
386+
JSDocEnumTag = 306,
387+
JSDocParameterTag = 307,
388+
JSDocReturnTag = 308,
389+
JSDocThisTag = 309,
390+
JSDocTypeTag = 310,
391+
JSDocTemplateTag = 311,
392+
JSDocTypedefTag = 312,
393+
JSDocPropertyTag = 313,
394+
SyntaxList = 314,
395+
NotEmittedStatement = 315,
396+
PartiallyEmittedExpression = 316,
397+
CommaListExpression = 317,
398+
MergeDeclarationMarker = 318,
399+
EndOfDeclarationMarker = 319,
400+
Count = 320,
400401
FirstAssignment = 60,
401402
LastAssignment = 72,
402403
FirstCompoundAssignment = 61,
@@ -423,9 +424,9 @@ declare namespace ts {
423424
LastBinaryOperator = 72,
424425
FirstNode = 149,
425426
FirstJSDocNode = 289,
426-
LastJSDocNode = 312,
427-
FirstJSDocTagNode = 300,
428-
LastJSDocTagNode = 312,
427+
LastJSDocNode = 313,
428+
FirstJSDocTagNode = 301,
429+
LastJSDocTagNode = 313,
429430
}
430431
export enum NodeFlags {
431432
None = 0,
@@ -1558,6 +1559,10 @@ declare namespace ts {
15581559
kind: SyntaxKind.JSDocVariadicType;
15591560
type: TypeNode;
15601561
}
1562+
export interface JSDocNamepathType extends JSDocType {
1563+
kind: SyntaxKind.JSDocNamepathType;
1564+
type: TypeNode;
1565+
}
15611566
export type JSDocTypeReferencingNode = JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
15621567
export interface JSDoc extends Node {
15631568
kind: SyntaxKind.JSDocComment;
@@ -5671,6 +5676,7 @@ declare namespace ts {
56715676
}
56725677
}
56735678
declare namespace ts {
5679+
/** The classifier is used for syntactic highlighting in editors via the TSServer */
56745680
function createClassifier(): Classifier;
56755681
}
56765682
declare namespace ts {

tests/baselines/reference/noAssertForUnparseableTypedefs.errors.txt

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
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`.
2-
tests/cases/conformance/jsdoc/bug26693.js(1,21): error TS1005: '}' expected.
31
tests/cases/conformance/jsdoc/bug26693.js(2,22): error TS2307: Cannot find module 'nope'.
42

53

6-
==== tests/cases/conformance/jsdoc/bug26693.js (3 errors) ====
4+
==== tests/cases/conformance/jsdoc/bug26693.js (1 errors) ====
75
/** @typedef {module:locale} hi */
8-
~~~~~~
9-
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i @types/node`.
10-
~
11-
!!! error TS1005: '}' expected.
126
import { nope } from 'nope';
137
~~~~~~
148
!!! error TS2307: Cannot find module 'nope'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
///<reference path="fourslash.ts" />
2+
// @allowJs: true
3+
// @Filename: 31298.js
4+
/////**
5+
//// * @returns {module:@nodefuel/web~Webserver~wsServer#hello} Websocket server object
6+
//// */
7+
////function foo() { }
8+
////foo(''/*foo*/);
9+
////
10+
/////**
11+
//// * @type {module:xxxxx} */
12+
//// */
13+
////function bar() { }
14+
////bar(''/*bar*/);
15+
////
16+
/////** @type {function(module:xxxx, module:xxxx): module:xxxxx} */
17+
////function zee() { }
18+
////zee(''/*zee*/);
19+
20+
21+
verify.signatureHelp({
22+
marker: "foo",
23+
text: "foo(): any",
24+
docComment: "",
25+
tags: [
26+
{ name: "returns", text: "Websocket server object" },
27+
],
28+
});
29+
30+
verify.signatureHelp({
31+
marker: "bar",
32+
text: "bar(): void",
33+
docComment: "",
34+
tags: [],
35+
});
36+
37+
38+
verify.signatureHelp({
39+
marker: "zee",
40+
text: "zee(): any",
41+
docComment: "",
42+
tags: [
43+
{ name: "type", text: "{function(module:xxxx, module:xxxx): module:xxxxx}" },
44+
],
45+
});

0 commit comments

Comments
 (0)