Skip to content

Commit

Permalink
feat: Wrap JSDocFunctionType.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Feb 21, 2019
1 parent 16e82a0 commit 8965da3
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,11 @@ export declare class TypeGuards {
* @param node - Node to check.
*/
static isJSDocClassTag(node: Node): node is JSDocClassTag;
/**
* Gets if the node is a JSDocFunctionType.
* @param node - Node to check.
*/
static isJSDocFunctionType(node: Node): node is JSDocFunctionType;
/**
* Gets if the node is a JSDocParameterTag.
* @param node - Node to check.
Expand Down Expand Up @@ -1880,12 +1885,16 @@ export declare type BindingName = Identifier | BindingPattern;

export declare type BindingPattern = ObjectBindingPattern | ArrayBindingPattern;

export declare type CallLikeExpression = CallExpression | NewExpression | TaggedTemplateExpression | Decorator | JsxOpeningLikeElement;

export declare type EntityName = Identifier | QualifiedName;

export declare type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement | JsxFragment;

export declare type JsxAttributeLike = JsxAttribute | JsxSpreadAttribute;

export declare type JsxOpeningLikeElement = JsxSelfClosingElement | JsxOpeningElement;

export declare type JsxTagNameExpression = Identifier | ThisExpression | JsxTagNamePropertyAccess;

export interface JsxTagNamePropertyAccess extends PropertyAccessExpression {
Expand Down Expand Up @@ -4998,6 +5007,14 @@ export declare class JSDocAugmentsTag extends JSDocTag<ts.JSDocAugmentsTag> {
export declare class JSDocClassTag extends JSDocTag<ts.JSDocClassTag> {
}

declare const JSDocFunctionTypeBase: Constructor<SignaturedDeclaration> & typeof JSDocType;

/**
* JS doc function type.
*/
export declare class JSDocFunctionType extends JSDocFunctionTypeBase<ts.JSDocFunctionType> {
}

declare const JSDocParameterTagBase: Constructor<JSDocPropertyLikeTag> & typeof JSDocTag;

/**
Expand Down Expand Up @@ -6463,6 +6480,7 @@ export interface ImplementedKindToNodeMappings {
[SyntaxKind.IntersectionType]: IntersectionTypeNode;
[SyntaxKind.JSDocAugmentsTag]: JSDocAugmentsTag;
[SyntaxKind.JSDocClassTag]: JSDocClassTag;
[SyntaxKind.JSDocFunctionType]: JSDocFunctionType;
[SyntaxKind.JSDocReturnTag]: JSDocReturnTag;
[SyntaxKind.JSDocSignature]: JSDocSignature;
[SyntaxKind.JSDocTag]: JSDocUnknownTag;
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/ast/doc/JSDocFunctionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ts } from "../../../typescript";
import { SignaturedDeclaration } from "../base";
import { JSDocType } from "./JSDocType";

export const JSDocFunctionTypeBase = SignaturedDeclaration(JSDocType);

/**
* JS doc function type.
*/
export class JSDocFunctionType extends JSDocFunctionTypeBase<ts.JSDocFunctionType> {
}
1 change: 1 addition & 0 deletions src/compiler/ast/doc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./base";
export * from "./JSDoc";
export * from "./JSDocAugmentsTag";
export * from "./JSDocClassTag";
export * from "./JSDocFunctionType";
export * from "./JSDocParameterTag";
export * from "./JSDocPropertyTag";
export * from "./JSDocReturnTag";
Expand Down
1 change: 1 addition & 0 deletions src/compiler/ast/kindToNodeMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface ImplementedKindToNodeMappings {
[SyntaxKind.IntersectionType]: compiler.IntersectionTypeNode;
[SyntaxKind.JSDocAugmentsTag]: compiler.JSDocAugmentsTag;
[SyntaxKind.JSDocClassTag]: compiler.JSDocClassTag;
[SyntaxKind.JSDocFunctionType]: compiler.JSDocFunctionType;
[SyntaxKind.JSDocReturnTag]: compiler.JSDocReturnTag;
[SyntaxKind.JSDocSignature]: compiler.JSDocSignature;
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag;
Expand Down
1 change: 1 addition & 0 deletions src/factories/kindToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const kindToWrapperMappings: { [key: number]: unknown } = {
[SyntaxKind.IntersectionType]: compiler.IntersectionTypeNode,
[SyntaxKind.JSDocAugmentsTag]: compiler.JSDocAugmentsTag,
[SyntaxKind.JSDocClassTag]: compiler.JSDocClassTag,
[SyntaxKind.JSDocFunctionType]: compiler.JSDocFunctionType,
[SyntaxKind.JSDocReturnTag]: compiler.JSDocReturnTag,
[SyntaxKind.JSDocSignature]: compiler.JSDocSignature,
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag,
Expand Down
19 changes: 19 additions & 0 deletions src/tests/compiler/ast/doc/jsDocFunctionTypeTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from "chai";
import { JSDocFunctionType } from "../../../../compiler";
import { SyntaxKind } from "../../../../typescript";
import { getInfoFromTextWithDescendant } from "../../testHelpers";

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

describe("general", () => {
it("should get the js doc function type", () => {
const { descendant } = getInfo("/** @type {function (): void} */let x;");
expect(descendant).to.be.instanceOf(JSDocFunctionType);
// the rest of these are tested in SignaturedDeclaration
expect(descendant.getReturnType().getText()).to.equal("void");
});
});
});
20 changes: 19 additions & 1 deletion src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,14 @@ export class TypeGuards {
return node.getKind() === SyntaxKind.JSDocClassTag;
}

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

/**
* Gets if the node is a JSDocParameterTag.
* @param node - Node to check.
Expand Down Expand Up @@ -1136,7 +1144,13 @@ export class TypeGuards {
* @param node - Node to check.
*/
static isJSDocType(node: compiler.Node): node is compiler.JSDocType {
return node.getKind() === SyntaxKind.JSDocSignature;
switch (node.getKind()) {
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocSignature:
return true;
default:
return false;
}
}

/**
Expand Down Expand Up @@ -1761,6 +1775,7 @@ export class TypeGuards {
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
Expand Down Expand Up @@ -2046,6 +2061,7 @@ export class TypeGuards {
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
Expand Down Expand Up @@ -2120,6 +2136,7 @@ export class TypeGuards {
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
Expand Down Expand Up @@ -2484,6 +2501,7 @@ export class TypeGuards {
static isTypeNode(node: compiler.Node): node is compiler.TypeNode {
switch (node.getKind()) {
case SyntaxKind.TypePredicate:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocSignature:
case SyntaxKind.ArrayType:
case SyntaxKind.ConstructorType:
Expand Down
6 changes: 3 additions & 3 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:** 170
**Total:** 171

* [ArrayBindingPattern](src/compiler/ast/binding/ArrayBindingPattern.ts)
* :heavy_check_mark: elements
Expand Down Expand Up @@ -180,6 +180,7 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* [JSDocAugmentsTag](src/compiler/ast/doc/JSDocAugmentsTag.ts)
* :x: class
* [JSDocClassTag](src/compiler/ast/doc/JSDocClassTag.ts)
* [JSDocFunctionType](src/compiler/ast/doc/JSDocFunctionType.ts)
* [JSDocParameterTag](src/compiler/ast/doc/JSDocParameterTag.ts)
* [JSDocPropertyTag](src/compiler/ast/doc/JSDocPropertyTag.ts)
* [JSDocReturnTag](src/compiler/ast/doc/JSDocReturnTag.ts)
Expand Down Expand Up @@ -429,7 +430,7 @@ The disadvantage to a node not being wrapped is that it won't have helper method

## Not Exist

**Total:** 53
**Total:** 52

* BigIntLiteral
* Bundle
Expand All @@ -444,7 +445,6 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* JSDocAllType
* JSDocCallbackTag
* JSDocEnumTag
* JSDocFunctionType
* JSDocNamespaceDeclaration
* JSDocNonNullableType
* JSDocNullableType
Expand Down

0 comments on commit 8965da3

Please sign in to comment.