Skip to content

Commit

Permalink
feat: #547 - Wrap JSDocSignature and JSDocType.
Browse files Browse the repository at this point in the history
Still need to add more methods on JSDocSignature, but not going to bother for the time being.
  • Loading branch information
dsherret committed Feb 18, 2019
1 parent 2fd7cac commit 9e1b0b2
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 14 deletions.
31 changes: 29 additions & 2 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,11 +1195,21 @@ export declare class TypeGuards {
* @param node - Node to check.
*/
static isJSDocReturnTag(node: Node): node is JSDocReturnTag;
/**
* Gets if the node is a JSDocSignature.
* @param node - Node to check.
*/
static isJSDocSignature(node: Node): node is JSDocSignature;
/**
* Gets if the node is a JSDocTag.
* @param node - Node to check.
*/
static isJSDocTag(node: Node): node is JSDocTag;
/**
* Gets if the node is a JSDocType.
* @param node - Node to check.
*/
static isJSDocType(node: Node): node is JSDocType;
/**
* Gets if the node is a JSDocTypeExpression.
* @param node - Node to check.
Expand Down Expand Up @@ -5014,6 +5024,16 @@ export declare class JSDocReturnTag extends JSDocTag<ts.JSDocReturnTag> {
getTypeExpression(): JSDocTypeExpression | undefined;
}

/**
* JS doc signature node.
*/
export declare class JSDocSignature extends JSDocType<ts.JSDocSignature> {
/**
* Gets the type node of the JS doc signature.
*/
getTypeNode(): JSDocReturnTag | undefined;
}

/**
* JS doc tag node.
*/
Expand Down Expand Up @@ -5049,6 +5069,12 @@ export declare class JSDocTagInfo {
getText(): string | undefined;
}

/**
* JS doc type node.
*/
export declare class JSDocType<T extends ts.JSDocType = ts.JSDocType> extends TypeNode<T> {
}

/**
* JS doc type def tag node.
*/
Expand Down Expand Up @@ -6435,11 +6461,12 @@ export interface ImplementedKindToNodeMappings {
[SyntaxKind.IndexSignature]: IndexSignatureDeclaration;
[SyntaxKind.InterfaceDeclaration]: InterfaceDeclaration;
[SyntaxKind.IntersectionType]: IntersectionTypeNode;
[SyntaxKind.JSDocTag]: JSDocUnknownTag;
[SyntaxKind.FirstJSDocTagNode]: JSDocUnknownTag;
[SyntaxKind.JSDocAugmentsTag]: JSDocAugmentsTag;
[SyntaxKind.JSDocClassTag]: JSDocClassTag;
[SyntaxKind.JSDocReturnTag]: JSDocReturnTag;
[SyntaxKind.JSDocSignature]: JSDocSignature;
[SyntaxKind.JSDocTag]: JSDocUnknownTag;
[SyntaxKind.FirstJSDocTagNode]: JSDocUnknownTag;
[SyntaxKind.JSDocTypeExpression]: JSDocTypeExpression;
[SyntaxKind.FirstJSDocNode]: JSDocTypeExpression;
[SyntaxKind.JSDocTypeTag]: JSDocTypeTag;
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/ast/doc/JSDocSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ts } from "../../../typescript";
import { JSDocType } from "./JSDocType";

/**
* JS doc signature node.
*/
export class JSDocSignature extends JSDocType<ts.JSDocSignature> {
/**
* Gets the type node of the JS doc signature.
*/
getTypeNode() {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.type);
}

// todo: more methods
}
8 changes: 8 additions & 0 deletions src/compiler/ast/doc/JSDocType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ts } from "../../../typescript";
import { TypeNode } from "../type";

/**
* JS doc type node.
*/
export class JSDocType<T extends ts.JSDocType = ts.JSDocType> extends TypeNode<T> {
}
2 changes: 2 additions & 0 deletions src/compiler/ast/doc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export * from "./JSDocClassTag";
export * from "./JSDocParameterTag";
export * from "./JSDocPropertyTag";
export * from "./JSDocReturnTag";
export * from "./JSDocSignature";
export * from "./JSDocTag";
export * from "./JSDocTagInfo";
export * from "./JSDocType";
export * from "./JSDocTypedefTag";
export * from "./JSDocTypeTag";
export * from "./JSDocUnknownTag";
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/ast/kindToNodeMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export interface ImplementedKindToNodeMappings {
[SyntaxKind.IndexSignature]: compiler.IndexSignatureDeclaration;
[SyntaxKind.InterfaceDeclaration]: compiler.InterfaceDeclaration;
[SyntaxKind.IntersectionType]: compiler.IntersectionTypeNode;
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag;
[SyntaxKind.FirstJSDocTagNode]: compiler.JSDocUnknownTag;
[SyntaxKind.JSDocAugmentsTag]: compiler.JSDocAugmentsTag;
[SyntaxKind.JSDocClassTag]: compiler.JSDocClassTag;
[SyntaxKind.JSDocReturnTag]: compiler.JSDocReturnTag;
[SyntaxKind.JSDocSignature]: compiler.JSDocSignature;
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag;
[SyntaxKind.FirstJSDocTagNode]: compiler.JSDocUnknownTag;
[SyntaxKind.JSDocTypeExpression]: compiler.JSDocTypeExpression;
[SyntaxKind.FirstJSDocNode]: compiler.JSDocTypeExpression;
[SyntaxKind.JSDocTypeTag]: compiler.JSDocTypeTag;
Expand Down
3 changes: 2 additions & 1 deletion src/factories/kindToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ export const kindToWrapperMappings: { [key: number]: unknown } = {
[SyntaxKind.IndexSignature]: compiler.IndexSignatureDeclaration,
[SyntaxKind.InterfaceDeclaration]: compiler.InterfaceDeclaration,
[SyntaxKind.IntersectionType]: compiler.IntersectionTypeNode,
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag,
[SyntaxKind.JSDocAugmentsTag]: compiler.JSDocAugmentsTag,
[SyntaxKind.JSDocClassTag]: compiler.JSDocClassTag,
[SyntaxKind.JSDocReturnTag]: compiler.JSDocReturnTag,
[SyntaxKind.JSDocSignature]: compiler.JSDocSignature,
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag,
[SyntaxKind.JSDocTypeExpression]: compiler.JSDocTypeExpression,
[SyntaxKind.JSDocTypeTag]: compiler.JSDocTypeTag,
[SyntaxKind.JSDocTypedefTag]: compiler.JSDocTypedefTag,
Expand Down
10 changes: 5 additions & 5 deletions src/tests/compiler/ast/doc/jsDocReturnTagTests.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {expect} from "chai";
import {JSDocReturnTag, TypeGuards} from "../../../../main";
import {getInfoFromText} from "../../testHelpers";
import { expect } from "chai";
import { JSDocReturnTag } from "../../../../compiler";
import { SyntaxKind } from "../../../../typescript";
import { getInfoFromTextWithDescendant } from "../../testHelpers";

describe(nameof(JSDocReturnTag), () => {
function getInfo(text: string) {
const info = getInfoFromText(text);
return { descendant: info.sourceFile.getFirstDescendantOrThrow(TypeGuards.isJSDocReturnTag), ...info };
return getInfoFromTextWithDescendant<JSDocReturnTag>(text, SyntaxKind.JSDocReturnTag);
}

describe(nameof<JSDocReturnTag>(d => d.getTypeExpression), () => {
Expand Down
28 changes: 28 additions & 0 deletions src/tests/compiler/ast/doc/jsDocSignatureTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from "chai";
import { JSDocSignature } from "../../../../compiler";
import { SyntaxKind } from "../../../../typescript";
import { getInfoFromTextWithDescendant } from "../../testHelpers";

describe(nameof(JSDocSignature), () => {
function getInfo(text: string) {
return getInfoFromTextWithDescendant<JSDocSignature>(text, SyntaxKind.JSDocSignature);
}

describe(nameof<JSDocSignature>(d => d.getTypeNode), () => {
function doTest(text: string, expected: string | undefined) {
const { descendant } = getInfo(text);
if (expected == null)
expect(descendant.getTypeNode()).to.be.undefined;
else
expect(descendant.getTypeNode()!.getText()).to.equal(expected);
}

it("should get when it exists", () => {
doTest("/**\n * @callback cb\n * @return {string}\n */\nlet x;", "@return {string}");
});

it("should be undefined when not exists", () => {
doTest("/**\n * @callback cb\n */\nlet x;", undefined);
});
});
});
17 changes: 17 additions & 0 deletions src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,14 @@ export class TypeGuards {
return node.getKind() === SyntaxKind.JSDocReturnTag;
}

/**
* Gets if the node is a JSDocSignature.
* @param node - Node to check.
*/
static isJSDocSignature(node: compiler.Node): node is compiler.JSDocSignature {
return node.getKind() === SyntaxKind.JSDocSignature;
}

/**
* Gets if the node is a JSDocTag.
* @param node - Node to check.
Expand All @@ -1123,6 +1131,14 @@ export class TypeGuards {
}
}

/**
* Gets if the node is a JSDocType.
* @param node - Node to check.
*/
static isJSDocType(node: compiler.Node): node is compiler.JSDocType {
return node.getKind() === SyntaxKind.JSDocSignature;
}

/**
* Gets if the node is a JSDocTypeExpression.
* @param node - Node to check.
Expand Down Expand Up @@ -2468,6 +2484,7 @@ export class TypeGuards {
static isTypeNode(node: compiler.Node): node is compiler.TypeNode {
switch (node.getKind()) {
case SyntaxKind.TypePredicate:
case SyntaxKind.JSDocSignature:
case SyntaxKind.ArrayType:
case SyntaxKind.ConstructorType:
case SyntaxKind.ExpressionWithTypeArguments:
Expand Down
11 changes: 7 additions & 4 deletions wrapped-nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The disadvantage to a node not being wrapped is that it won't have helper method

## Exist

**Total:** 168
**Total:** 170

* [ArrayBindingPattern](src/compiler/ast/binding/ArrayBindingPattern.ts)
* :heavy_check_mark: elements
Expand Down Expand Up @@ -184,9 +184,14 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* [JSDocPropertyTag](src/compiler/ast/doc/JSDocPropertyTag.ts)
* [JSDocReturnTag](src/compiler/ast/doc/JSDocReturnTag.ts)
* :heavy_check_mark: typeExpression
* [JSDocSignature](src/compiler/ast/doc/JSDocSignature.ts)
* :x: typeParameters
* :x: parameters
* :heavy_check_mark: type
* [JSDocTag](src/compiler/ast/doc/JSDocTag.ts)
* :heavy_check_mark: tagName
* :heavy_check_mark: comment
* [JSDocType](src/compiler/ast/doc/JSDocType.ts)
* [JSDocTypeExpression](src/compiler/ast/doc/JSDocTypeExpression.ts)
* :heavy_check_mark: type
* [JSDocTypeTag](src/compiler/ast/doc/JSDocTypeTag.ts)
Expand Down Expand Up @@ -424,7 +429,7 @@ The disadvantage to a node not being wrapped is that it won't have helper method

## Not Exist

**Total:** 55
**Total:** 53

* BigIntLiteral
* Bundle
Expand All @@ -445,10 +450,8 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* JSDocNullableType
* JSDocOptionalType
* JSDocPropertyLikeTag
* JSDocSignature
* JSDocTemplateTag
* JSDocThisTag
* JSDocType
* JSDocTypeLiteral
* JSDocUnknownType
* JSDocVariadicType
Expand Down

0 comments on commit 9e1b0b2

Please sign in to comment.