Skip to content

Commit

Permalink
#27 - Setting JSDoc description.
Browse files Browse the repository at this point in the history
- Getting JSDoc tags.

Need to update the documentation for this before releasing. Still a lot of JSDoc stuff not implemented.
  • Loading branch information
dsherret committed Nov 6, 2017
1 parent fd3a64e commit fc81509
Show file tree
Hide file tree
Showing 31 changed files with 489 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/compiler/base/BodiedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface BodiedNode {
getBody(): Node;
/**
* Sets the body text.
* @param writerFunction - Function for using a writer to write out the body text.
* @param writerFunction - Write the text using the provided writer.
*/
setBodyText(writerFunction: (writer: CodeBlockWriter) => void): this;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/base/BodyableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface BodyableNode {
getBody(): Node | undefined;
/**
* Sets the body text. A body is required to do this operation.
* @param writerFunction - Function for using a writer to write out the body text.
* @param writerFunction - Write the text using the provided writer.
*/
setBodyText(writerFunction: (writer: CodeBlockWriter) => void): this;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/base/DocumentationableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function DocumentationableNode<T extends Constructor<DocumentationableNod
if (docCommentNodes.length === 0)
return undefined;

const texts = docCommentNodes.map(n => (n.compilerNode.comment || "").trim());
const texts = docCommentNodes.map(n => (n.getComment() || "").trim());
return texts.filter(t => t.length > 0).join(this.global.manipulationSettings.getNewLineKind());
}

Expand Down
8 changes: 8 additions & 0 deletions src/compiler/base/name/DeclarationNamedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface DeclarationNamedNode {
* Gets the name.
*/
getName(): string | undefined;
/**
* Gets the name or throws if it doens't exist.
*/
getNameOrThrow(): string;
/**
* Renames the name.
* @param text - Text to set as the name.
Expand Down Expand Up @@ -52,6 +56,10 @@ export function DeclarationNamedNode<T extends Constructor<DeclarationNamedNodeE
}
}

getNameOrThrow() {
return errors.throwIfNullOrUndefined(this.getName(), "Expected to find a name.");
}

getName() {
const nameNode = this.getNameIdentifier();
return nameNode == null ? undefined : nameNode.getText();
Expand Down
8 changes: 3 additions & 5 deletions src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,11 @@ export class Node<NodeType extends ts.Node = ts.Node> implements Disposable {
}

/**
* Gets the position of the start of the line that this node is on.
* Gets the position of the start of the line that this node starts on.
*/
getStartLinePos() {
const sourceFileText = this.sourceFile.getFullText();
const startPos = this.getStart();

return getPreviousMatchingPos(sourceFileText, startPos, char => char === "\n");
return getPreviousMatchingPos(sourceFileText, this.getStart(), char => char === "\n");
}

/**
Expand All @@ -705,7 +703,7 @@ export class Node<NodeType extends ts.Node = ts.Node> implements Disposable {
* Replaces the text of the current node with new text.
*
* This will dispose the current node and return a new node that can be asserted or type guarded to the correct type.
* @param writerFunction - Writer function to replace the text with.
* @param writerFunction - Write the text using the provided writer.
* @returns The new node.
*/
replaceWithText(writerFunction: (writer: CodeBlockWriter) => void): Node;
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/doc.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export * from "./doc/JSDoc";
export * from "./doc/JSDocTag";
export * from "./doc/JSDocUnknownTag";
export * from "./doc/JSDocAugmentsTag";
export * from "./doc/JSDocClassTag";
export * from "./doc/JSDocReturnTag";
export * from "./doc/JSDocTypeTag";
export * from "./doc/JSDocTypedefTag";
export * from "./doc/JSDocPropertyTag";
export * from "./doc/JSDocParameterTag";
53 changes: 51 additions & 2 deletions src/compiler/doc/JSDoc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
import * as ts from "typescript";
import {removeChildren} from "./../../manipulation";
import CodeBlockWriter from "code-block-writer";
import {removeChildren, replaceTextPossiblyCreatingChildNodes} from "./../../manipulation";
import {getPreviousMatchingPos} from "./../../manipulation/textSeek";
import {getTextFromStringOrWriter} from "./../../utils";
import {Node} from "./../common";

/**
* A js doc node.
* JS doc node.
*/
export class JSDoc extends Node<ts.JSDoc> {
/**
* Gets the tags of the JSDoc.
*/
getTags(): Node[] {
const tags = this.compilerNode.tags;
if (tags == null)
return [];
return tags.map(t => this.global.compilerFactory.getNodeFromCompilerNode(t, this.sourceFile)) as Node[];
}

/**
* Gets the comment.
*/
getComment() {
return this.compilerNode.comment;
}

/**
* Sets the comment.
* @param writerFunction - Write the text using the provided writer.
*/
setComment(writerFunction: (writer: CodeBlockWriter) => void): this;
/**
* Sets the comment.
* @param text - Text of the comment.
*/
setComment(text: string): this;
setComment(textOrWriterFunction: string | ((writer: CodeBlockWriter) => void)) {
const tags = this.getTags();
const startEditPos = this.getStart() + 3;
const endEditPos = tags.length > 0 ? getPreviousMatchingPos(this.sourceFile.getFullText(), tags[0].getStart(), c => c === "*") - 1 : this.getEnd() - 2;
const indentationText = this.getIndentationText();
const newLineKind = this.global.manipulationSettings.getNewLineKind();
const text = getTextFromStringOrWriter(this.global.manipulationSettings, textOrWriterFunction);
const newText = newLineKind + text.split(/\r?\n/).map(l => `${indentationText} * ${l}`).join(newLineKind) + newLineKind + indentationText + " ";

replaceTextPossiblyCreatingChildNodes({
parent: this,
newText,
replacePos: startEditPos,
replacingLength: endEditPos - startEditPos
});

return this;
}

/**
* Removes this JSDoc.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/doc/JSDocAugmentsTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";

/**
* JS doc augments tag node.
*/
export class JSDocAugmentsTag extends JSDocTag<ts.JSDocAugmentsTag> {
// todo: helper methods
}
8 changes: 8 additions & 0 deletions src/compiler/doc/JSDocClassTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";

/**
* JS doc class tag node.
*/
export class JSDocClassTag extends JSDocTag<ts.JSDocClassTag> {
}
10 changes: 10 additions & 0 deletions src/compiler/doc/JSDocParameterTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";
import {JSDocPropertyLikeTag} from "./base";

export const JSDocParameterTagBase = JSDocPropertyLikeTag(JSDocTag);
/**
* JS doc parameter tag node.
*/
export class JSDocParameterTag extends JSDocParameterTagBase<ts.JSDocParameterTag> {
}
10 changes: 10 additions & 0 deletions src/compiler/doc/JSDocPropertyTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";
import {JSDocPropertyLikeTag} from "./base";

export const JSDocPropertyTagBase = JSDocPropertyLikeTag(JSDocTag);
/**
* JS doc property tag node.
*/
export class JSDocPropertyTag extends JSDocPropertyTagBase<ts.JSDocPropertyTag> {
}
9 changes: 9 additions & 0 deletions src/compiler/doc/JSDocReturnTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";

/**
* JS doc return tag node.
*/
export class JSDocReturnTag extends JSDocTag<ts.JSDocReturnTag> {
// todo: helper methods
}
28 changes: 28 additions & 0 deletions src/compiler/doc/JSDocTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as ts from "typescript";
import {Node, Identifier} from "./../common";

/**
* JS doc tag node.
*/
export class JSDocTag<NodeType extends ts.JSDocTag = ts.JSDocTag> extends Node<NodeType> {
/**
* Gets the at token.
*/
getAtToken() {
return this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.atToken, this.sourceFile) as Node;
}

/**
* Gets the tag name identifier.
*/
getTagNameIdentifier() {
return this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.tagName, this.sourceFile) as Identifier;
}

/**
* Gets the tag's comment.
*/
getComment() {
return this.compilerNode.comment;
}
}
9 changes: 9 additions & 0 deletions src/compiler/doc/JSDocTypeTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";

/**
* JS doc type tag node.
*/
export class JSDocTypeTag extends JSDocTag<ts.JSDocTypeTag> {
// todo: helper methods
}
9 changes: 9 additions & 0 deletions src/compiler/doc/JSDocTypedefTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";

/**
* JS doc type def tag node.
*/
export class JSDocTypedefTag extends JSDocTag<ts.JSDocTypedefTag> {
// todo: helper methods
}
8 changes: 8 additions & 0 deletions src/compiler/doc/JSDocUnknownTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as ts from "typescript";
import {JSDocTag} from "./JSDocTag";

/**
* JS doc unknown tag node.
*/
export class JSDocUnknownTag extends JSDocTag<ts.JSDocUnknownTag> {
}
1 change: 1 addition & 0 deletions src/compiler/doc/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./base/JSDocPropertyLikeTag";
15 changes: 15 additions & 0 deletions src/compiler/doc/base/JSDocPropertyLikeTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from "typescript";
import {Constructor} from "./../../../Constructor";
import {Node} from "./../../common";

export type JSDocPropertyLikeTagExtensionType = Node /*& ModifierableNode*/;

export interface JSDocPropertyLikeTag {
// todo: methods
}

export function JSDocPropertyLikeTag<T extends Constructor<JSDocPropertyLikeTagExtensionType>>(Base: T): Constructor<JSDocPropertyLikeTag> & T {
return class extends Base implements JSDocPropertyLikeTag {
// todo: methods
};
}
9 changes: 8 additions & 1 deletion src/factories/nodeToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,12 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[ts.SyntaxKind.VariableStatement]: compiler.VariableStatement,
[ts.SyntaxKind.JSDocComment]: compiler.JSDoc,
[ts.SyntaxKind.FirstTypeNode]: compiler.TypeNode,
[ts.SyntaxKind.LastTypeNode]: compiler.TypeNode
[ts.SyntaxKind.LastTypeNode]: compiler.TypeNode,
[ts.SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag,
[ts.SyntaxKind.JSDocAugmentsTag]: compiler.JSDocAugmentsTag,
[ts.SyntaxKind.JSDocClassTag]: compiler.JSDocClassTag,
[ts.SyntaxKind.JSDocReturnTag]: compiler.JSDocReturnTag,
[ts.SyntaxKind.JSDocTypeTag]: compiler.JSDocTypeTag,
[ts.SyntaxKind.JSDocTypedefTag]: compiler.JSDocTypedefTag,
[ts.SyntaxKind.JSDocPropertyTag]: compiler.JSDocPropertyTag
};
2 changes: 1 addition & 1 deletion src/manipulation/code/getIndentedText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface GetIndentedTextOptions {
export function getIndentedText(opts: GetIndentedTextOptions) {
const {textOrWriterFunction, manipulationSettings, indentationText} = opts;
const newLineKind = manipulationSettings.getNewLineKind();
const originalText = getTextFromStringOrWriter(manipulationSettings, textOrWriterFunction) || "";
const originalText = getTextFromStringOrWriter(manipulationSettings, textOrWriterFunction);
if (originalText.length > 0)
return originalText.split(/\r?\n/).map(t => t.length > 0 ? indentationText + t : t).join(newLineKind);
return originalText;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Node, SourceFile} from "./../../compiler";
import {Node, SourceFile} from "./../compiler";

export interface GetNewReplacementSourceFileOptions {
insertPos: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Node} from "./../../compiler";
import {replaceTreeCreatingSyntaxList, replaceTreeWithChildIndex} from "./../tree";
import {getNewReplacementSourceFile} from "./getNewReplacementSourceFile";
import {getNewReplacementSourceFile} from "./../getNewReplacementSourceFile";
import {insertSyntaxList} from "./insertSyntaxList";
import {insertIntoParent} from "./insertIntoParent";

Expand Down
2 changes: 1 addition & 1 deletion src/manipulation/insertion/insertIntoParent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Node} from "./../../compiler";
import {replaceTreeWithChildIndex} from "./../tree";
import {getNewReplacementSourceFile} from "./getNewReplacementSourceFile";
import {getNewReplacementSourceFile} from "./../getNewReplacementSourceFile";

export interface InsertIntoParentOptions {
insertPos: number;
Expand Down
2 changes: 1 addition & 1 deletion src/manipulation/insertion/insertIntoParentTextRange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Node} from "./../../compiler";
import {replaceTreeWithRange} from "./../tree";
import {getNewReplacementSourceFile} from "./getNewReplacementSourceFile";
import {getNewReplacementSourceFile} from "./../getNewReplacementSourceFile";

export interface InsertIntoParentTextRangeOptions {
insertPos: number;
Expand Down
2 changes: 1 addition & 1 deletion src/manipulation/insertion/insertSyntaxList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Node} from "./../../compiler";
import {replaceTreeCreatingSyntaxList} from "./../tree";
import {getNewReplacementSourceFile} from "./getNewReplacementSourceFile";
import {getNewReplacementSourceFile} from "./../getNewReplacementSourceFile";

export interface InsertSyntaxListOptions {
insertPos: number;
Expand Down
2 changes: 1 addition & 1 deletion src/manipulation/removal/unwrapNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {Node} from "./../../compiler";
import {isStringNode} from "./../../utils";
import {replaceTreeUnwrappingNode} from "./../tree";
import {getNewReplacementSourceFile} from "./../insertion/getNewReplacementSourceFile";
import {getNewReplacementSourceFile} from "./../getNewReplacementSourceFile";

export function unwrapNode(node: Node) {
const tempSourceFile = getNewReplacementSourceFile({
Expand Down
1 change: 1 addition & 0 deletions src/manipulation/replaction.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./replaction/replaceNodeText";
export * from "./replaction/replaceSourceFileTextForFormatting";
export * from "./replaction/replaceTextPossiblyCreatingChildNodes";
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Node} from "./../../compiler";
import {replaceTreeWithRange} from "./../tree";
import {getNewReplacementSourceFile} from "./../getNewReplacementSourceFile";

export interface ReplaceTextPossiblyCreatingChildNodesOptions {
replacePos: number;
replacingLength: number;
newText: string;
parent: Node;
}

/**
* Replaces a node text while possibly creating new child nodes.
*/
export function replaceTextPossiblyCreatingChildNodes(opts: ReplaceTextPossiblyCreatingChildNodesOptions) {
const {replacePos, replacingLength, newText, parent} = opts;
const tempSourceFile = getNewReplacementSourceFile({
sourceFile: parent.getSourceFile(),
insertPos: replacePos,
replacingLength,
newText
});

replaceTreeWithRange({
parent,
replacementSourceFile: tempSourceFile,
start: replacePos,
end: replacePos + newText.length
});
}
Loading

0 comments on commit fc81509

Please sign in to comment.