From b8082caa8eb5c16020f805ff70ef61454e07175c Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 11 Nov 2016 15:58:52 -0800 Subject: [PATCH] Clean up jsdoc in utilities Fix functions that were unused (getJsDocComments), redundant (append) or badly named (getJSDocCommentsFromText) --- src/compiler/declarationEmitter.ts | 2 +- src/compiler/parser.ts | 2 +- src/compiler/utilities.ts | 46 +++++++++--------------------- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 0bba375a2cb..3b48f453478 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -371,7 +371,7 @@ namespace ts { function writeJsDocComments(declaration: Node) { if (declaration) { - const jsDocComments = getJsDocCommentsFromText(declaration, currentText); + const jsDocComments = getJSDocCommentRanges(declaration, currentText); emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments); // jsDoc comments are emitted at /*leading comment1 */space/*leading comment*/space emitComments(currentText, currentLineMap, writer, jsDocComments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeCommentRange); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 943a677ef5d..682c22b4fba 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -676,7 +676,7 @@ namespace ts { function addJSDocComment(node: T): T { - const comments = getJsDocCommentsFromText(node, sourceFile.text); + const comments = getJSDocCommentRanges(node, sourceFile.text); if (comments) { for (const comment of comments) { const jsDoc = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index a9f2429d376..9aa0e4267cf 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -638,25 +638,18 @@ namespace ts { return getLeadingCommentRanges(text, node.pos); } - export function getJsDocComments(node: Node, sourceFileOfNode: SourceFile) { - return getJsDocCommentsFromText(node, sourceFileOfNode.text); - } - - export function getJsDocCommentsFromText(node: Node, text: string) { + export function getJSDocCommentRanges(node: Node, text: string) { const commentRanges = (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter || node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction) ? concatenate(getTrailingCommentRanges(text, node.pos), getLeadingCommentRanges(text, node.pos)) : getLeadingCommentRangesOfNodeFromText(node, text); - return filter(commentRanges, isJsDocComment); - - function isJsDocComment(comment: CommentRange) { - // True if the comment starts with '/**' but not if it is '/**/' - return text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk && - text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk && - text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash; - } + // True if the comment starts with '/**' but not if it is '/**/' + return filter(commentRanges, comment => + text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk && + text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk && + text.charCodeAt(comment.pos + 3) !== CharacterCodes.slash); } export let fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; @@ -1453,18 +1446,6 @@ namespace ts { } } - function append(previous: T[] | undefined, additional: T[] | undefined): T[] | undefined { - if (additional) { - if (!previous) { - previous = []; - } - for (const x of additional) { - previous.push(x); - } - } - return previous; - } - export function getJSDocComments(node: Node, checkParentVariableStatement: boolean): string[] { return getJSDocs(node, checkParentVariableStatement, docs => map(docs, doc => doc.comment), tags => map(tags, tag => tag.comment)); } @@ -1482,7 +1463,6 @@ namespace ts { } function getJSDocs(node: Node, checkParentVariableStatement: boolean, getDocs: (docs: JSDoc[]) => T[], getTags: (tags: JSDocTag[]) => T[]): T[] { - // TODO: Get rid of getJsDocComments and friends (note the lowercase 's' in Js) // TODO: A lot of this work should be cached, maybe. I guess it's only used in services right now... let result: T[] = undefined; // prepend documentation from parent sources @@ -1505,11 +1485,11 @@ namespace ts { isVariableOfVariableDeclarationStatement ? node.parent.parent : undefined; if (variableStatementNode) { - result = append(result, getJSDocs(variableStatementNode, checkParentVariableStatement, getDocs, getTags)); + result = concatenate(result, getJSDocs(variableStatementNode, checkParentVariableStatement, getDocs, getTags)); } if (node.kind === SyntaxKind.ModuleDeclaration && node.parent && node.parent.kind === SyntaxKind.ModuleDeclaration) { - result = append(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags)); + result = concatenate(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags)); } // Also recognize when the node is the RHS of an assignment expression @@ -1520,30 +1500,30 @@ namespace ts { (parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken && parent.parent.kind === SyntaxKind.ExpressionStatement; if (isSourceOfAssignmentExpressionStatement) { - result = append(result, getJSDocs(parent.parent, checkParentVariableStatement, getDocs, getTags)); + result = concatenate(result, getJSDocs(parent.parent, checkParentVariableStatement, getDocs, getTags)); } const isPropertyAssignmentExpression = parent && parent.kind === SyntaxKind.PropertyAssignment; if (isPropertyAssignmentExpression) { - result = append(result, getJSDocs(parent, checkParentVariableStatement, getDocs, getTags)); + result = concatenate(result, getJSDocs(parent, checkParentVariableStatement, getDocs, getTags)); } // Pull parameter comments from declaring function as well if (node.kind === SyntaxKind.Parameter) { const paramTags = getJSDocParameterTag(node as ParameterDeclaration, checkParentVariableStatement); if (paramTags) { - result = append(result, getTags(paramTags)); + result = concatenate(result, getTags(paramTags)); } } } if (isVariableLike(node) && node.initializer) { - result = append(result, getJSDocs(node.initializer, /*checkParentVariableStatement*/ false, getDocs, getTags)); + result = concatenate(result, getJSDocs(node.initializer, /*checkParentVariableStatement*/ false, getDocs, getTags)); } if (node.jsDocComments) { if (result) { - result = append(result, getDocs(node.jsDocComments)); + result = concatenate(result, getDocs(node.jsDocComments)); } else { return getDocs(node.jsDocComments);