Skip to content

Commit b8082ca

Browse files
committed
Clean up jsdoc in utilities
Fix functions that were unused (getJsDocComments), redundant (append) or badly named (getJSDocCommentsFromText)
1 parent 0e879c0 commit b8082ca

3 files changed

Lines changed: 15 additions & 35 deletions

File tree

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ namespace ts {
371371

372372
function writeJsDocComments(declaration: Node) {
373373
if (declaration) {
374-
const jsDocComments = getJsDocCommentsFromText(declaration, currentText);
374+
const jsDocComments = getJSDocCommentRanges(declaration, currentText);
375375
emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments);
376376
// jsDoc comments are emitted at /*leading comment1 */space/*leading comment*/space
377377
emitComments(currentText, currentLineMap, writer, jsDocComments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeCommentRange);

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ namespace ts {
676676

677677

678678
function addJSDocComment<T extends Node>(node: T): T {
679-
const comments = getJsDocCommentsFromText(node, sourceFile.text);
679+
const comments = getJSDocCommentRanges(node, sourceFile.text);
680680
if (comments) {
681681
for (const comment of comments) {
682682
const jsDoc = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos);

src/compiler/utilities.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -638,25 +638,18 @@ namespace ts {
638638
return getLeadingCommentRanges(text, node.pos);
639639
}
640640

641-
export function getJsDocComments(node: Node, sourceFileOfNode: SourceFile) {
642-
return getJsDocCommentsFromText(node, sourceFileOfNode.text);
643-
}
644-
645-
export function getJsDocCommentsFromText(node: Node, text: string) {
641+
export function getJSDocCommentRanges(node: Node, text: string) {
646642
const commentRanges = (node.kind === SyntaxKind.Parameter ||
647643
node.kind === SyntaxKind.TypeParameter ||
648644
node.kind === SyntaxKind.FunctionExpression ||
649645
node.kind === SyntaxKind.ArrowFunction) ?
650646
concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) :
651647
getLeadingCommentRangesOfNodeFromText(node, text);
652-
return filter(commentRanges, isJsDocComment);
653-
654-
function isJsDocComment(comment: CommentRange) {
655-
// True if the comment starts with '/**' but not if it is '/**/'
656-
return text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
657-
text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk &&
658-
text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash;
659-
}
648+
// True if the comment starts with '/**' but not if it is '/**/'
649+
return filter(commentRanges, comment =>
650+
text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
651+
text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk &&
652+
text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash);
660653
}
661654

662655
export let fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
@@ -1453,18 +1446,6 @@ namespace ts {
14531446
}
14541447
}
14551448

1456-
function append<T>(previous: T[] | undefined, additional: T[] | undefined): T[] | undefined {
1457-
if (additional) {
1458-
if (!previous) {
1459-
previous = [];
1460-
}
1461-
for (const x of additional) {
1462-
previous.push(x);
1463-
}
1464-
}
1465-
return previous;
1466-
}
1467-
14681449
export function getJSDocComments(node: Node, checkParentVariableStatement: boolean): string[] {
14691450
return getJSDocs(node, checkParentVariableStatement, docs => map(docs, doc => doc.comment), tags => map(tags, tag => tag.comment));
14701451
}
@@ -1482,7 +1463,6 @@ namespace ts {
14821463
}
14831464

14841465
function getJSDocs<T>(node: Node, checkParentVariableStatement: boolean, getDocs: (docs: JSDoc[]) => T[], getTags: (tags: JSDocTag[]) => T[]): T[] {
1485-
// TODO: Get rid of getJsDocComments and friends (note the lowercase 's' in Js)
14861466
// TODO: A lot of this work should be cached, maybe. I guess it's only used in services right now...
14871467
let result: T[] = undefined;
14881468
// prepend documentation from parent sources
@@ -1505,11 +1485,11 @@ namespace ts {
15051485
isVariableOfVariableDeclarationStatement ? node.parent.parent :
15061486
undefined;
15071487
if (variableStatementNode) {
1508-
result = append(result, getJSDocs(variableStatementNode, checkParentVariableStatement, getDocs, getTags));
1488+
result = concatenate(result, getJSDocs(variableStatementNode, checkParentVariableStatement, getDocs, getTags));
15091489
}
15101490
if (node.kind === SyntaxKind.ModuleDeclaration &&
15111491
node.parent && node.parent.kind === SyntaxKind.ModuleDeclaration) {
1512-
result = append(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags));
1492+
result = concatenate(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags));
15131493
}
15141494

15151495
// Also recognize when the node is the RHS of an assignment expression
@@ -1520,30 +1500,30 @@ namespace ts {
15201500
(parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken &&
15211501
parent.parent.kind === SyntaxKind.ExpressionStatement;
15221502
if (isSourceOfAssignmentExpressionStatement) {
1523-
result = append(result, getJSDocs(parent.parent, checkParentVariableStatement, getDocs, getTags));
1503+
result = concatenate(result, getJSDocs(parent.parent, checkParentVariableStatement, getDocs, getTags));
15241504
}
15251505

15261506
const isPropertyAssignmentExpression = parent && parent.kind === SyntaxKind.PropertyAssignment;
15271507
if (isPropertyAssignmentExpression) {
1528-
result = append(result, getJSDocs(parent, checkParentVariableStatement, getDocs, getTags));
1508+
result = concatenate(result, getJSDocs(parent, checkParentVariableStatement, getDocs, getTags));
15291509
}
15301510

15311511
// Pull parameter comments from declaring function as well
15321512
if (node.kind === SyntaxKind.Parameter) {
15331513
const paramTags = getJSDocParameterTag(node as ParameterDeclaration, checkParentVariableStatement);
15341514
if (paramTags) {
1535-
result = append(result, getTags(paramTags));
1515+
result = concatenate(result, getTags(paramTags));
15361516
}
15371517
}
15381518
}
15391519

15401520
if (isVariableLike(node) && node.initializer) {
1541-
result = append(result, getJSDocs(node.initializer, /*checkParentVariableStatement*/ false, getDocs, getTags));
1521+
result = concatenate(result, getJSDocs(node.initializer, /*checkParentVariableStatement*/ false, getDocs, getTags));
15421522
}
15431523

15441524
if (node.jsDocComments) {
15451525
if (result) {
1546-
result = append(result, getDocs(node.jsDocComments));
1526+
result = concatenate(result, getDocs(node.jsDocComments));
15471527
}
15481528
else {
15491529
return getDocs(node.jsDocComments);

0 commit comments

Comments
 (0)