Skip to content

Commit 65d1079

Browse files
committed
Replace services' jsdoc parser with parsers'
Also modify scanner's definition of leading vs trailing comments, with associated changes to emitter and emit baselines (the last is in a separate commit).
1 parent 38de65a commit 65d1079

File tree

10 files changed

+1049
-828
lines changed

10 files changed

+1049
-828
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ namespace ts {
262262
Debug.assert(node.parent.kind === SyntaxKind.JSDocFunctionType);
263263
let functionType = <JSDocFunctionType>node.parent;
264264
let index = indexOf(functionType.parameters, node);
265-
return "p" + index;
265+
return "arg" + index;
266266
case SyntaxKind.JSDocTypedefTag:
267267
const parentNode = node.parent && node.parent.parent;
268268
let nameFromParentNode: string;
@@ -517,9 +517,7 @@ namespace ts {
517517
// because the scope of JsDocComment should not be affected by whether the current node is a
518518
// container or not.
519519
if (isInJavaScriptFile(node) && node.jsDocComments) {
520-
for (const jsDocComment of node.jsDocComments) {
521-
bind(jsDocComment);
522-
}
520+
forEach(node.jsDocComments, bind);
523521
}
524522
if (checkUnreachable(node)) {
525523
forEachChild(node, bind);

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5629,12 +5629,13 @@ namespace ts {
56295629
case SyntaxKind.JSDocThisType:
56305630
case SyntaxKind.JSDocOptionalType:
56315631
return getTypeFromTypeNode((<ParenthesizedTypeNode | JSDocTypeReferencingNode>node).type);
5632+
case SyntaxKind.JSDocRecordType:
5633+
return getTypeFromTypeNode((node as JSDocRecordType).literal);
56325634
case SyntaxKind.FunctionType:
56335635
case SyntaxKind.ConstructorType:
56345636
case SyntaxKind.TypeLiteral:
56355637
case SyntaxKind.JSDocTypeLiteral:
56365638
case SyntaxKind.JSDocFunctionType:
5637-
case SyntaxKind.JSDocRecordType:
56385639
return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments);
56395640
// This function assumes that an identifier or qualified name is a type expression
56405641
// Callers should first ensure this by calling isTypeNode

src/compiler/emitter.ts

Lines changed: 55 additions & 27 deletions
Large diffs are not rendered by default.

src/compiler/parser.ts

Lines changed: 275 additions & 136 deletions
Large diffs are not rendered by default.

src/compiler/scanner.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,11 @@ namespace ts {
599599
* If false, whitespace is skipped until the first line break and comments between that location
600600
* and the next token are returned.
601601
* If true, comments occurring between the given position and the next line break are returned.
602+
* If there is no line break, then no comments are returned.
602603
*/
603604
function getCommentRanges(text: string, pos: number, trailing: boolean): CommentRange[] {
604605
let result: CommentRange[];
605-
let collecting = trailing || pos === 0;
606+
let needToSkipTrailingComment = pos > 0;
606607
while (pos < text.length) {
607608
const ch = text.charCodeAt(pos);
608609
switch (ch) {
@@ -615,7 +616,11 @@ namespace ts {
615616
if (trailing) {
616617
return result;
617618
}
618-
collecting = true;
619+
else if (needToSkipTrailingComment) {
620+
// skip the first line if not trailing (it'll be part of the trailing comment).
621+
needToSkipTrailingComment = false;
622+
result = undefined;
623+
}
619624
if (result && result.length) {
620625
lastOrUndefined(result).hasTrailingNewLine = true;
621626
}
@@ -651,11 +656,13 @@ namespace ts {
651656
pos++;
652657
}
653658
}
654-
if (collecting) {
659+
660+
// if we are at the end of the file, don't add trailing comments.
661+
// end-of-file comments are added as leading comment of the end-of-file token.
662+
if (pos < text.length || !trailing) {
655663
if (!result) {
656664
result = [];
657665
}
658-
659666
result.push({ pos: startPos, end: pos, hasTrailingNewLine, kind });
660667
}
661668
continue;
@@ -671,10 +678,10 @@ namespace ts {
671678
}
672679
break;
673680
}
674-
return result;
681+
return !trailing ? result : undefined;
675682
}
676683

677-
return result;
684+
return !trailing ? result : undefined;
678685
}
679686

680687
export function getLeadingCommentRanges(text: string, pos: number): CommentRange[] {
@@ -1689,40 +1696,46 @@ namespace ts {
16891696
}
16901697

16911698
startPos = pos;
1692-
1693-
// Eat leading whitespace
1694-
let ch = text.charCodeAt(pos);
1695-
while (pos < end) {
1696-
ch = text.charCodeAt(pos);
1697-
if (isWhiteSpaceSingleLine(ch)) {
1698-
pos++;
1699-
}
1700-
else {
1701-
break;
1702-
}
1703-
}
17041699
tokenPos = pos;
17051700

1701+
const ch = text.charCodeAt(pos);
17061702
switch (ch) {
1703+
case CharacterCodes.tab:
1704+
case CharacterCodes.verticalTab:
1705+
case CharacterCodes.formFeed:
1706+
case CharacterCodes.space:
1707+
while (pos < end && isWhiteSpaceSingleLine(text.charCodeAt(pos))) {
1708+
pos++;
1709+
}
1710+
return token = SyntaxKind.WhitespaceTrivia;
17071711
case CharacterCodes.at:
1708-
return pos += 1, token = SyntaxKind.AtToken;
1712+
pos++;
1713+
return token = SyntaxKind.AtToken;
17091714
case CharacterCodes.lineFeed:
17101715
case CharacterCodes.carriageReturn:
1711-
return pos += 1, token = SyntaxKind.NewLineTrivia;
1716+
pos++;
1717+
return token = SyntaxKind.NewLineTrivia;
17121718
case CharacterCodes.asterisk:
1713-
return pos += 1, token = SyntaxKind.AsteriskToken;
1719+
pos++;
1720+
return token = SyntaxKind.AsteriskToken;
17141721
case CharacterCodes.openBrace:
1715-
return pos += 1, token = SyntaxKind.OpenBraceToken;
1722+
pos++;
1723+
return token = SyntaxKind.OpenBraceToken;
17161724
case CharacterCodes.closeBrace:
1717-
return pos += 1, token = SyntaxKind.CloseBraceToken;
1725+
pos++;
1726+
return token = SyntaxKind.CloseBraceToken;
17181727
case CharacterCodes.openBracket:
1719-
return pos += 1, token = SyntaxKind.OpenBracketToken;
1728+
pos++;
1729+
return token = SyntaxKind.OpenBracketToken;
17201730
case CharacterCodes.closeBracket:
1721-
return pos += 1, token = SyntaxKind.CloseBracketToken;
1731+
pos++;
1732+
return token = SyntaxKind.CloseBracketToken;
17221733
case CharacterCodes.equals:
1723-
return pos += 1, token = SyntaxKind.EqualsToken;
1734+
pos++;
1735+
return token = SyntaxKind.EqualsToken;
17241736
case CharacterCodes.comma:
1725-
return pos += 1, token = SyntaxKind.CommaToken;
1737+
pos++;
1738+
return token = SyntaxKind.CommaToken;
17261739
}
17271740

17281741
if (isIdentifierStart(ch, ScriptTarget.Latest)) {

src/compiler/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ namespace ts {
464464
modifiers?: ModifiersArray; // Array of modifiers
465465
/* @internal */ id?: number; // Unique id (used to look up NodeLinks)
466466
parent?: Node; // Parent node (initialized by binding
467-
/* @internal */ jsDocComments?: JSDocComment[]; // JSDoc for the node, if it has any. Only for .js files.
467+
/* @internal */ jsDocComments?: JSDoc[]; // JSDoc for the node, if it has any. Only for .js files.
468468
/* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding)
469469
/* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding)
470470
/* @internal */ nextContainer?: Node; // Next container in declaration order (initialized by binding)
@@ -1471,7 +1471,7 @@ namespace ts {
14711471

14721472
// @kind(SyntaxKind.JSDocRecordType)
14731473
export interface JSDocRecordType extends JSDocType, TypeLiteralNode {
1474-
members: NodeArray<JSDocRecordMember>;
1474+
literal: TypeLiteralNode;
14751475
}
14761476

14771477
// @kind(SyntaxKind.JSDocTypeReference)
@@ -1519,14 +1519,16 @@ namespace ts {
15191519
}
15201520

15211521
// @kind(SyntaxKind.JSDocComment)
1522-
export interface JSDocComment extends Node {
1522+
export interface JSDoc extends Node {
15231523
tags: NodeArray<JSDocTag>;
1524+
comment: string | undefined;
15241525
}
15251526

15261527
// @kind(SyntaxKind.JSDocTag)
15271528
export interface JSDocTag extends Node {
15281529
atToken: Node;
15291530
tagName: Identifier;
1531+
comment: string | undefined;
15301532
}
15311533

15321534
// @kind(SyntaxKind.JSDocTemplateTag)
@@ -1565,9 +1567,13 @@ namespace ts {
15651567

15661568
// @kind(SyntaxKind.JSDocParameterTag)
15671569
export interface JSDocParameterTag extends JSDocTag {
1570+
/** the parameter name, if provided *before* the type (TypeScript-style) */
15681571
preParameterName?: Identifier;
15691572
typeExpression?: JSDocTypeExpression;
1573+
/** the parameter name, if provided *after* the type (JSDoc-standard) */
15701574
postParameterName?: Identifier;
1575+
/** the parameter name, regardless of the location it was provided */
1576+
parameterName: Identifier;
15711577
isBracketed: boolean;
15721578
}
15731579

0 commit comments

Comments
 (0)