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C,eAAe,CAAC;YACnC,KAAK,EAAE,CAAC,UAAU,CAAC,gBAAgB,CAAC;YACpC,KAAK,EAAE,CAAC,UAAU,CAAC,eAAe,CAAC;YACnC,KAAK,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC;YACrC,KAAK,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC;YACrC,KAAK,EAAE,CAAC,UAAU,CAAC,eAAe,CAAC;YACnC,KAAK,EAAE,CAAC,UAAU,CAAC,mBAAmB,CAAC;YACvC,KAAK,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC;YACrC,KAAK,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC;YAC/B,KAAK,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC;YAC/B,KAAK,EAAE,CAAC,UAAU,CAAC,oBAAoB,CAAC;YACxC,KAAK,EAAE,CAAC,UAAU,CAAC,oBAAoB,CAAC;YACxC,KAAK,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC;YACjC,KAAK,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC;YAC9B,KAAK,EAAE,CAAC,UAAU,CAAC,cAAc;gBAC/B,OAAO,IAAI,CAAC;YACd;gBACE,OAAO,KAAK,CAAC;SAChB;IACH,CAAC;CACF;AAvRD,wCAuRC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as ts from 'typescript';\nimport type { DeclarationReference } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';\nimport {\n ExcerptTokenKind,\n type IExcerptToken,\n type IExcerptTokenRange\n} from '@microsoft/api-extractor-model';\n\nimport { Span } from '../analyzer/Span';\nimport type { DeclarationReferenceGenerator } from './DeclarationReferenceGenerator';\nimport type { AstDeclaration } from '../analyzer/AstDeclaration';\n\n/**\n * Used to provide ExcerptBuilder with a list of nodes whose token range we want to capture.\n */\nexport interface IExcerptBuilderNodeToCapture {\n /**\n * The node to capture\n */\n node: ts.Node | undefined;\n /**\n * The token range whose startIndex/endIndex will be overwritten with the indexes for the\n * tokens corresponding to IExcerptBuilderNodeToCapture.node\n */\n tokenRange: IExcerptTokenRange;\n}\n\n/**\n * Internal state for ExcerptBuilder\n */\ninterface IBuildSpanState {\n referenceGenerator: DeclarationReferenceGenerator;\n\n /**\n * The AST node that we will traverse to extract tokens\n */\n startingNode: ts.Node;\n\n /**\n * Normally, the excerpt will include all child nodes for `startingNode`; whereas if `childKindToStopBefore`\n * is specified, then the node traversal will stop before (i.e. excluding) the first immediate child\n * of `startingNode` with the specified syntax kind.\n *\n * @remarks\n * For example, suppose the signature is `interface X: Y { z: string }`. The token `{` has syntax kind\n * `ts.SyntaxKind.FirstPunctuation`, so we can specify that to truncate the excerpt to `interface X: Y`.\n */\n stopBeforeChildKind: ts.SyntaxKind | undefined;\n\n tokenRangesByNode: Map<ts.Node, IExcerptTokenRange>;\n\n /**\n * Tracks whether the last appended token was a separator. If so, and we're in the middle of\n * capturing a token range, then omit the separator from the range.\n */\n lastAppendedTokenIsSeparator: boolean;\n}\n\nexport class ExcerptBuilder {\n /**\n * Appends a blank line to the `excerptTokens` list.\n * @param excerptTokens - The target token list to append to\n */\n public static addBlankLine(excerptTokens: IExcerptToken[]): void {\n let newlines: string = '\\n\\n';\n // If the existing text already ended with a newline, then only append one newline\n if (excerptTokens.length > 0) {\n const previousText: string = excerptTokens[excerptTokens.length - 1].text;\n if (/\\n$/.test(previousText)) {\n newlines = '\\n';\n }\n }\n excerptTokens.push({ kind: ExcerptTokenKind.Content, text: newlines });\n }\n\n /**\n * Appends the signature for the specified `AstDeclaration` to the `excerptTokens` list.\n * @param excerptTokens - The target token list to append to\n * @param nodesToCapture - A list of child nodes whose token ranges we want to capture\n */\n public static addDeclaration(\n excerptTokens: IExcerptToken[],\n astDeclaration: AstDeclaration,\n nodesToCapture: IExcerptBuilderNodeToCapture[],\n referenceGenerator: DeclarationReferenceGenerator\n ): void {\n let stopBeforeChildKind: ts.SyntaxKind | undefined = undefined;\n\n switch (astDeclaration.declaration.kind) {\n case ts.SyntaxKind.ClassDeclaration:\n case ts.SyntaxKind.EnumDeclaration:\n case ts.SyntaxKind.InterfaceDeclaration:\n // FirstPunctuation = \"{\"\n stopBeforeChildKind = ts.SyntaxKind.FirstPunctuation;\n break;\n case ts.SyntaxKind.ModuleDeclaration:\n // ModuleBlock = the \"{ ... }\" block\n stopBeforeChildKind = ts.SyntaxKind.ModuleBlock;\n break;\n }\n\n const span: Span = new Span(astDeclaration.declaration);\n\n const tokenRangesByNode: Map<ts.Node, IExcerptTokenRange> = new Map<ts.Node, IExcerptTokenRange>();\n for (const excerpt of nodesToCapture || []) {\n if (excerpt.node) {\n tokenRangesByNode.set(excerpt.node, excerpt.tokenRange);\n }\n }\n\n ExcerptBuilder._buildSpan(excerptTokens, span, {\n referenceGenerator: referenceGenerator,\n startingNode: span.node,\n stopBeforeChildKind,\n tokenRangesByNode,\n lastAppendedTokenIsSeparator: false\n });\n ExcerptBuilder._condenseTokens(excerptTokens, [...tokenRangesByNode.values()]);\n }\n\n public static createEmptyTokenRange(): IExcerptTokenRange {\n return { startIndex: 0, endIndex: 0 };\n }\n\n private static _buildSpan(excerptTokens: IExcerptToken[], span: Span, state: IBuildSpanState): boolean {\n if (span.kind === ts.SyntaxKind.JSDocComment) {\n // Discard any comments\n return true;\n }\n\n // Can this node start a excerpt?\n const capturedTokenRange: IExcerptTokenRange | undefined = state.tokenRangesByNode.get(span.node);\n let excerptStartIndex: number = 0;\n\n if (capturedTokenRange) {\n // We will assign capturedTokenRange.startIndex to be the index of the next token to be appended\n excerptStartIndex = excerptTokens.length;\n }\n\n if (span.prefix) {\n let canonicalReference: DeclarationReference | undefined = undefined;\n\n if (span.kind === ts.SyntaxKind.Identifier) {\n const name: ts.Identifier = span.node as ts.Identifier;\n if (!ExcerptBuilder._isDeclarationName(name)) {\n canonicalReference = state.referenceGenerator.getDeclarationReferenceForIdentifier(name);\n }\n }\n\n if (canonicalReference) {\n ExcerptBuilder._appendToken(\n excerptTokens,\n ExcerptTokenKind.Reference,\n span.prefix,\n canonicalReference\n );\n } else {\n ExcerptBuilder._appendToken(excerptTokens, ExcerptTokenKind.Content, span.prefix);\n }\n state.lastAppendedTokenIsSeparator = false;\n }\n\n for (const child of span.children) {\n if (span.node === state.startingNode) {\n if (state.stopBeforeChildKind && child.kind === state.stopBeforeChildKind) {\n // We reached a child whose kind is stopBeforeChildKind, so stop traversing\n return false;\n }\n }\n\n if (!this._buildSpan(excerptTokens, child, state)) {\n return false;\n }\n }\n\n if (span.suffix) {\n ExcerptBuilder._appendToken(excerptTokens, ExcerptTokenKind.Content, span.suffix);\n state.lastAppendedTokenIsSeparator = false;\n }\n if (span.separator) {\n ExcerptBuilder._appendToken(excerptTokens, ExcerptTokenKind.Content, span.separator);\n state.lastAppendedTokenIsSeparator = true;\n }\n\n // Are we building a excerpt? If so, set its range\n if (capturedTokenRange) {\n capturedTokenRange.startIndex = excerptStartIndex;\n\n // We will assign capturedTokenRange.startIndex to be the index after the last token\n // that was appended so far. However, if the last appended token was a separator, omit\n // it from the range.\n let excerptEndIndex: number = excerptTokens.length;\n if (state.lastAppendedTokenIsSeparator) {\n excerptEndIndex--;\n }\n\n capturedTokenRange.endIndex = excerptEndIndex;\n }\n\n return true;\n }\n\n private static _appendToken(\n excerptTokens: IExcerptToken[],\n excerptTokenKind: ExcerptTokenKind,\n text: string,\n canonicalReference?: DeclarationReference\n ): void {\n if (text.length === 0) {\n return;\n }\n\n const excerptToken: IExcerptToken = { kind: excerptTokenKind, text: text };\n if (canonicalReference !== undefined) {\n excerptToken.canonicalReference = canonicalReference.toString();\n }\n excerptTokens.push(excerptToken);\n }\n\n /**\n * Condenses the provided excerpt tokens by merging tokens where possible. Updates the provided token ranges to\n * remain accurate after token merging.\n *\n * @remarks\n * For example, suppose we have excerpt tokens [\"A\", \"B\", \"C\"] and a token range [0, 2]. If the excerpt tokens\n * are condensed to [\"AB\", \"C\"], then the token range would be updated to [0, 1]. Note that merges are only\n * performed if they are compatible with the provided token ranges. In the example above, if our token range was\n * originally [0, 1], we would not be able to merge tokens \"A\" and \"B\".\n */\n private static _condenseTokens(excerptTokens: IExcerptToken[], tokenRanges: IExcerptTokenRange[]): void {\n // This set is used to quickly lookup a start or end index.\n const startOrEndIndices: Set<number> = new Set();\n for (const tokenRange of tokenRanges) {\n startOrEndIndices.add(tokenRange.startIndex);\n startOrEndIndices.add(tokenRange.endIndex);\n }\n\n for (let currentIndex: number = 1; currentIndex < excerptTokens.length; ++currentIndex) {\n while (currentIndex < excerptTokens.length) {\n const prevPrevToken: IExcerptToken = excerptTokens[currentIndex - 2]; // May be undefined\n const prevToken: IExcerptToken = excerptTokens[currentIndex - 1];\n const currentToken: IExcerptToken = excerptTokens[currentIndex];\n\n // The number of excerpt tokens that are merged in this iteration. We need this to determine\n // how to update the start and end indices of our token ranges.\n let mergeCount: number;\n\n // There are two types of merges that can occur. We only perform these merges if they are\n // compatible with all of our token ranges.\n if (\n prevPrevToken &&\n prevPrevToken.kind === ExcerptTokenKind.Reference &&\n prevToken.kind === ExcerptTokenKind.Content &&\n prevToken.text.trim() === '.' &&\n currentToken.kind === ExcerptTokenKind.Reference &&\n !startOrEndIndices.has(currentIndex) &&\n !startOrEndIndices.has(currentIndex - 1)\n ) {\n // If the current token is a reference token, the previous token is a \".\", and the previous-\n // previous token is a reference token, then merge all three tokens into a reference token.\n //\n // For example: Given [\"MyNamespace\" (R), \".\", \"MyClass\" (R)], tokens \".\" and \"MyClass\" might\n // be merged into \"MyNamespace\". The condensed token would be [\"MyNamespace.MyClass\" (R)].\n prevPrevToken.text += prevToken.text + currentToken.text;\n prevPrevToken.canonicalReference = currentToken.canonicalReference;\n mergeCount = 2;\n currentIndex--;\n } else if (\n // If the current and previous tokens are both content tokens, then merge the tokens into a\n // single content token. For example: Given [\"export \", \"declare class\"], these tokens\n // might be merged into \"export declare class\".\n prevToken.kind === ExcerptTokenKind.Content &&\n prevToken.kind === currentToken.kind &&\n !startOrEndIndices.has(currentIndex)\n ) {\n prevToken.text += currentToken.text;\n mergeCount = 1;\n } else {\n // Otherwise, no merging can occur here. Continue to the next index.\n break;\n }\n\n // Remove the now redundant excerpt token(s), as they were merged into a previous token.\n excerptTokens.splice(currentIndex, mergeCount);\n\n // Update the start and end indices for all token ranges based upon how many excerpt\n // tokens were merged and in what positions.\n for (const tokenRange of tokenRanges) {\n if (tokenRange.startIndex > currentIndex) {\n tokenRange.startIndex -= mergeCount;\n }\n\n if (tokenRange.endIndex > currentIndex) {\n tokenRange.endIndex -= mergeCount;\n }\n }\n\n // Clear and repopulate our set with the updated indices.\n startOrEndIndices.clear();\n for (const tokenRange of tokenRanges) {\n startOrEndIndices.add(tokenRange.startIndex);\n startOrEndIndices.add(tokenRange.endIndex);\n }\n }\n }\n }\n\n private static _isDeclarationName(name: ts.Identifier): boolean {\n return ExcerptBuilder._isDeclaration(name.parent) && name.parent.name === name;\n }\n\n private static _isDeclaration(node: ts.Node): node is ts.NamedDeclaration {\n switch (node.kind) {\n case ts.SyntaxKind.FunctionDeclaration:\n case ts.SyntaxKind.FunctionExpression:\n case ts.SyntaxKind.VariableDeclaration:\n case ts.SyntaxKind.Parameter:\n case ts.SyntaxKind.EnumDeclaration:\n case ts.SyntaxKind.ClassDeclaration:\n case ts.SyntaxKind.ClassExpression:\n case ts.SyntaxKind.ModuleDeclaration:\n case ts.SyntaxKind.MethodDeclaration:\n case ts.SyntaxKind.MethodSignature:\n case ts.SyntaxKind.PropertyDeclaration:\n case ts.SyntaxKind.PropertySignature:\n case ts.SyntaxKind.GetAccessor:\n case ts.SyntaxKind.SetAccessor:\n case ts.SyntaxKind.InterfaceDeclaration:\n case ts.SyntaxKind.TypeAliasDeclaration:\n case ts.SyntaxKind.TypeParameter:\n case ts.SyntaxKind.EnumMember:\n case ts.SyntaxKind.BindingElement:\n return true;\n default:\n return false;\n }\n }\n}\n"]} |