Skip to content

Commit

Permalink
feat: #535 - Add getTypeExpression() to JSDocReturnTag and JSDocTypeTag
Browse files Browse the repository at this point in the history
  • Loading branch information
Pineapples authored and dsherret committed Feb 16, 2019
1 parent 1d3c2bb commit 61b71b3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4995,6 +4995,10 @@ export declare class JSDocPropertyTag extends JSDocPropertyTagBase<ts.JSDocPrope
* JS doc return tag node.
*/
export declare class JSDocReturnTag extends JSDocTag<ts.JSDocReturnTag> {
/**
* Gets the type expression node of the JS doc property like tag.
*/
getTypeExpression(): JSDocTypeExpression | undefined;
}

/**
Expand Down Expand Up @@ -5042,6 +5046,10 @@ export declare class JSDocTypedefTag extends JSDocTag<ts.JSDocTypedefTag> {
* JS doc type tag node.
*/
export declare class JSDocTypeTag extends JSDocTag<ts.JSDocTypeTag> {
/**
* Gets the type expression node of the JS doc property like tag.
*/
getTypeExpression(): JSDocTypeExpression | undefined;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/ast/doc/JSDocReturnTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ import { JSDocTag } from "./JSDocTag";
* JS doc return tag node.
*/
export class JSDocReturnTag extends JSDocTag<ts.JSDocReturnTag> {
// todo: helper methods
/**
* Gets the type expression node of the JS doc property return tag.
*/
getTypeExpression() {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.typeExpression);
}
}
12 changes: 11 additions & 1 deletion src/compiler/ast/doc/JSDocTypeTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ import { JSDocTag } from "./JSDocTag";
* JS doc type tag node.
*/
export class JSDocTypeTag extends JSDocTag<ts.JSDocTypeTag> {
// todo: helper methods
/**
* Gets the type expression node of the JS doc property type tag.
*/
getTypeExpression() {
// for some reason the compiler will still return a node when it doesn't exist
const node = this.compilerNode.typeExpression;
if (node != null && node.pos === node.end)
return undefined;

return this._getNodeFromCompilerNodeIfExists(this.compilerNode.typeExpression);
}
}
22 changes: 22 additions & 0 deletions src/tests/compiler/ast/doc/jsDocReturnTagTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expect} from "chai";
import {JSDocReturnTag, TypeGuards} from "../../../../main";
import {getInfoFromText} from "../../testHelpers";

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

describe(nameof<JSDocReturnTag>(d => d.getTypeExpression), () => {
it("should get undefined when there is no type given", () => {
const { descendant } = getInfo("/** @returns t - String */\nfunction test() {}");
expect(descendant.getTypeExpression()).to.be.undefined;
});

it("should get when type is given", () => {
const { descendant } = getInfo("/** @returns {boolean} t - String */\nfunction test() {}");
expect(descendant.getTypeExpression()!.getTypeNode().getText()).to.equal("boolean");
});
});
});
22 changes: 22 additions & 0 deletions src/tests/compiler/ast/doc/jsDocTypeTagTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expect} from "chai";
import {TypeGuards, JSDocTypeTag} from "../../../../main";
import {getInfoFromText} from "../../testHelpers";

describe(nameof(JSDocTypeTag), () => {
function getInfo(text: string) {
const info = getInfoFromText(text);
return { descendant: info.sourceFile.getFirstDescendantOrThrow(TypeGuards.isJSDocTypeTag), ...info };
}

describe(nameof<JSDocTypeTag>(d => d.getTypeExpression), () => {
it("returns undefined when typeExpression is undefined", () => {
const { descendant } = getInfo("/** @type */\nvar bar = 1;");
expect(descendant.getTypeExpression()).to.be.undefined;
});

it("should get when type is given", () => {
const { descendant } = getInfo("/** @type {boolean} */\nvar bar = 1");
expect(descendant.getTypeExpression()!.getTypeNode().getText()).to.equal("boolean");
});
});
});
4 changes: 2 additions & 2 deletions wrapped-nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* [JSDocParameterTag](src/compiler/ast/doc/JSDocParameterTag.ts)
* [JSDocPropertyTag](src/compiler/ast/doc/JSDocPropertyTag.ts)
* [JSDocReturnTag](src/compiler/ast/doc/JSDocReturnTag.ts)
* :x: typeExpression
* :heavy_check_mark: typeExpression
* [JSDocTag](src/compiler/ast/doc/JSDocTag.ts)
* :heavy_check_mark: tagName
* :heavy_check_mark: comment
* [JSDocTypeExpression](src/compiler/ast/doc/JSDocTypeExpression.ts)
* :heavy_check_mark: type
* [JSDocTypeTag](src/compiler/ast/doc/JSDocTypeTag.ts)
* :x: typeExpression
* :heavy_check_mark: typeExpression
* [JSDocTypedefTag](src/compiler/ast/doc/JSDocTypedefTag.ts)
* :x: fullName
* :heavy_check_mark: name
Expand Down

0 comments on commit 61b71b3

Please sign in to comment.