Spaces:
Sleeping
Sleeping
File size: 15,736 Bytes
31248b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { DocNodeKind } from '../nodes/DocNode';
import { DocNodeTransforms } from '../transforms/DocNodeTransforms';
import { StandardTags } from '../details/StandardTags';
var LineState;
(function (LineState) {
LineState[LineState["Closed"] = 0] = "Closed";
LineState[LineState["StartOfLine"] = 1] = "StartOfLine";
LineState[LineState["MiddleOfLine"] = 2] = "MiddleOfLine";
})(LineState || (LineState = {}));
/**
* Renders a DocNode tree as a code comment.
*/
export class TSDocEmitter {
constructor() {
this.eol = '\n';
// Whether to emit the /** */ framing
this._emitCommentFraming = true;
// This state machine is used by the writer functions to generate the /** */ framing around the emitted lines
this._lineState = LineState.Closed;
// State for _ensureLineSkipped()
this._previousLineHadContent = false;
// Normally a paragraph is precede by a blank line (unless it's the first thing written).
// But sometimes we want the paragraph to be attached to the previous element, e.g. when it's part of
// an "@param" block. Setting _hangingParagraph=true accomplishes that.
this._hangingParagraph = false;
}
renderComment(output, docComment) {
this._emitCommentFraming = true;
this._renderCompleteObject(output, docComment);
}
renderHtmlTag(output, htmlTag) {
this._emitCommentFraming = false;
this._renderCompleteObject(output, htmlTag);
}
renderDeclarationReference(output, declarationReference) {
this._emitCommentFraming = false;
this._renderCompleteObject(output, declarationReference);
}
_renderCompleteObject(output, docNode) {
this._output = output;
this._lineState = LineState.Closed;
this._previousLineHadContent = false;
this._hangingParagraph = false;
this._renderNode(docNode);
this._writeEnd();
}
_renderNode(docNode) {
if (docNode === undefined) {
return;
}
switch (docNode.kind) {
case DocNodeKind.Block:
const docBlock = docNode;
this._ensureLineSkipped();
this._renderNode(docBlock.blockTag);
if (docBlock.blockTag.tagNameWithUpperCase === StandardTags.returns.tagNameWithUpperCase ||
docBlock.blockTag.tagNameWithUpperCase === StandardTags.defaultValue.tagNameWithUpperCase) {
this._writeContent(' ');
this._hangingParagraph = true;
}
this._renderNode(docBlock.content);
break;
case DocNodeKind.BlockTag:
const docBlockTag = docNode;
if (this._lineState === LineState.MiddleOfLine) {
this._writeContent(' ');
}
this._writeContent(docBlockTag.tagName);
break;
case DocNodeKind.CodeSpan:
const docCodeSpan = docNode;
this._writeContent('`');
this._writeContent(docCodeSpan.code);
this._writeContent('`');
break;
case DocNodeKind.Comment:
const docComment = docNode;
this._renderNodes([
docComment.summarySection,
docComment.remarksBlock,
docComment.privateRemarks,
docComment.deprecatedBlock,
docComment.params,
docComment.typeParams,
docComment.returnsBlock,
...docComment.customBlocks,
...docComment.seeBlocks,
docComment.inheritDocTag
]);
if (docComment.modifierTagSet.nodes.length > 0) {
this._ensureLineSkipped();
this._renderNodes(docComment.modifierTagSet.nodes);
}
break;
case DocNodeKind.DeclarationReference:
const docDeclarationReference = docNode;
this._writeContent(docDeclarationReference.packageName);
this._writeContent(docDeclarationReference.importPath);
if (docDeclarationReference.packageName !== undefined ||
docDeclarationReference.importPath !== undefined) {
this._writeContent('#');
}
this._renderNodes(docDeclarationReference.memberReferences);
break;
case DocNodeKind.ErrorText:
const docErrorText = docNode;
this._writeContent(docErrorText.text);
break;
case DocNodeKind.EscapedText:
const docEscapedText = docNode;
this._writeContent(docEscapedText.encodedText);
break;
case DocNodeKind.FencedCode:
const docFencedCode = docNode;
this._ensureAtStartOfLine();
this._writeContent('```');
this._writeContent(docFencedCode.language);
this._writeNewline();
this._writeContent(docFencedCode.code);
this._writeContent('```');
this._writeNewline();
this._writeNewline();
break;
case DocNodeKind.HtmlAttribute:
const docHtmlAttribute = docNode;
this._writeContent(docHtmlAttribute.name);
this._writeContent(docHtmlAttribute.spacingAfterName);
this._writeContent('=');
this._writeContent(docHtmlAttribute.spacingAfterEquals);
this._writeContent(docHtmlAttribute.value);
this._writeContent(docHtmlAttribute.spacingAfterValue);
break;
case DocNodeKind.HtmlEndTag:
const docHtmlEndTag = docNode;
this._writeContent('</');
this._writeContent(docHtmlEndTag.name);
this._writeContent('>');
break;
case DocNodeKind.HtmlStartTag:
const docHtmlStartTag = docNode;
this._writeContent('<');
this._writeContent(docHtmlStartTag.name);
this._writeContent(docHtmlStartTag.spacingAfterName);
let needsSpace = docHtmlStartTag.spacingAfterName === undefined || docHtmlStartTag.spacingAfterName.length === 0;
for (const attribute of docHtmlStartTag.htmlAttributes) {
if (needsSpace) {
this._writeContent(' ');
}
this._renderNode(attribute);
needsSpace = attribute.spacingAfterValue === undefined || attribute.spacingAfterValue.length === 0;
}
this._writeContent(docHtmlStartTag.selfClosingTag ? '/>' : '>');
break;
case DocNodeKind.InheritDocTag:
const docInheritDocTag = docNode;
this._renderInlineTag(docInheritDocTag, () => {
if (docInheritDocTag.declarationReference) {
this._writeContent(' ');
this._renderNode(docInheritDocTag.declarationReference);
}
});
break;
case DocNodeKind.InlineTag:
const docInlineTag = docNode;
this._renderInlineTag(docInlineTag, () => {
if (docInlineTag.tagContent.length > 0) {
this._writeContent(' ');
this._writeContent(docInlineTag.tagContent);
}
});
break;
case DocNodeKind.LinkTag:
const docLinkTag = docNode;
this._renderInlineTag(docLinkTag, () => {
if (docLinkTag.urlDestination !== undefined || docLinkTag.codeDestination !== undefined) {
if (docLinkTag.urlDestination !== undefined) {
this._writeContent(' ');
this._writeContent(docLinkTag.urlDestination);
}
else if (docLinkTag.codeDestination !== undefined) {
this._writeContent(' ');
this._renderNode(docLinkTag.codeDestination);
}
}
if (docLinkTag.linkText !== undefined) {
this._writeContent(' ');
this._writeContent('|');
this._writeContent(' ');
this._writeContent(docLinkTag.linkText);
}
});
break;
case DocNodeKind.MemberIdentifier:
const docMemberIdentifier = docNode;
if (docMemberIdentifier.hasQuotes) {
this._writeContent('"');
this._writeContent(docMemberIdentifier.identifier); // todo: encoding
this._writeContent('"');
}
else {
this._writeContent(docMemberIdentifier.identifier);
}
break;
case DocNodeKind.MemberReference:
const docMemberReference = docNode;
if (docMemberReference.hasDot) {
this._writeContent('.');
}
if (docMemberReference.selector) {
this._writeContent('(');
}
if (docMemberReference.memberSymbol) {
this._renderNode(docMemberReference.memberSymbol);
}
else {
this._renderNode(docMemberReference.memberIdentifier);
}
if (docMemberReference.selector) {
this._writeContent(':');
this._renderNode(docMemberReference.selector);
this._writeContent(')');
}
break;
case DocNodeKind.MemberSelector:
const docMemberSelector = docNode;
this._writeContent(docMemberSelector.selector);
break;
case DocNodeKind.MemberSymbol:
const docMemberSymbol = docNode;
this._writeContent('[');
this._renderNode(docMemberSymbol.symbolReference);
this._writeContent(']');
break;
case DocNodeKind.Section:
const docSection = docNode;
this._renderNodes(docSection.nodes);
break;
case DocNodeKind.Paragraph:
const trimmedParagraph = DocNodeTransforms.trimSpacesInParagraph(docNode);
if (trimmedParagraph.nodes.length > 0) {
if (this._hangingParagraph) {
// If it's a hanging paragraph, then don't skip a line
this._hangingParagraph = false;
}
else {
this._ensureLineSkipped();
}
this._renderNodes(trimmedParagraph.nodes);
this._writeNewline();
}
break;
case DocNodeKind.ParamBlock:
const docParamBlock = docNode;
this._ensureLineSkipped();
this._renderNode(docParamBlock.blockTag);
this._writeContent(' ');
this._writeContent(docParamBlock.parameterName);
this._writeContent(' - ');
this._hangingParagraph = true;
this._renderNode(docParamBlock.content);
this._hangingParagraph = false;
break;
case DocNodeKind.ParamCollection:
const docParamCollection = docNode;
this._renderNodes(docParamCollection.blocks);
break;
case DocNodeKind.PlainText:
const docPlainText = docNode;
this._writeContent(docPlainText.text);
break;
}
}
_renderInlineTag(docInlineTagBase, writeInlineTagContent) {
this._writeContent('{');
this._writeContent(docInlineTagBase.tagName);
writeInlineTagContent();
this._writeContent('}');
}
_renderNodes(docNodes) {
for (const docNode of docNodes) {
this._renderNode(docNode);
}
}
// Calls _writeNewline() only if we're not already at the start of a new line
_ensureAtStartOfLine() {
if (this._lineState === LineState.MiddleOfLine) {
this._writeNewline();
}
}
// Calls _writeNewline() if needed to ensure that we have skipped at least one line
_ensureLineSkipped() {
this._ensureAtStartOfLine();
if (this._previousLineHadContent) {
this._writeNewline();
}
}
// Writes literal text content. If it contains newlines, they will automatically be converted to
// _writeNewline() calls, to ensure that "*" is written at the start of each line.
_writeContent(content) {
if (content === undefined || content.length === 0) {
return;
}
const splitLines = content.split(/\r?\n/g);
if (splitLines.length > 1) {
let firstLine = true;
for (const line of splitLines) {
if (firstLine) {
firstLine = false;
}
else {
this._writeNewline();
}
this._writeContent(line);
}
return;
}
if (this._lineState === LineState.Closed) {
if (this._emitCommentFraming) {
this._output.append('/**' + this.eol + ' *');
}
this._lineState = LineState.StartOfLine;
}
if (this._lineState === LineState.StartOfLine) {
if (this._emitCommentFraming) {
this._output.append(' ');
}
}
this._output.append(content);
this._lineState = LineState.MiddleOfLine;
this._previousLineHadContent = true;
}
// Starts a new line, and inserts "/**" or "*" as appropriate.
_writeNewline() {
if (this._lineState === LineState.Closed) {
if (this._emitCommentFraming) {
this._output.append('/**' + this.eol + ' *');
}
this._lineState = LineState.StartOfLine;
}
this._previousLineHadContent = this._lineState === LineState.MiddleOfLine;
if (this._emitCommentFraming) {
this._output.append(this.eol + ' *');
}
else {
this._output.append(this.eol);
}
this._lineState = LineState.StartOfLine;
this._hangingParagraph = false;
}
// Closes the comment, adding the final "*/" delimiter
_writeEnd() {
if (this._lineState === LineState.MiddleOfLine) {
if (this._emitCommentFraming) {
this._writeNewline();
}
}
if (this._lineState !== LineState.Closed) {
if (this._emitCommentFraming) {
this._output.append('/' + this.eol);
}
this._lineState = LineState.Closed;
}
}
}
//# sourceMappingURL=TSDocEmitter.js.map |