Skip to content

Commit

Permalink
feat: Add JSDocPropertyLikeTag.getName() and .getNameNode()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 26, 2018
1 parent c362510 commit 9804627
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4836,9 +4836,13 @@ export declare class Decorator extends DecoratorBase<ts.Decorator> {
export declare function JSDocPropertyLikeTag<T extends Constructor<JSDocPropertyLikeTagExtensionType>>(Base: T): Constructor<JSDocPropertyLikeTag> & T;

export interface JSDocPropertyLikeTag {
/** Gets the name of the JS doc property like tag. */
getName(): string;
/** Gets the name node of the JS doc property like tag. */
getNameNode(): EntityName;
}

declare type JSDocPropertyLikeTagExtensionType = Node;
declare type JSDocPropertyLikeTagExtensionType = Node<ts.JSDocPropertyLikeTag> & JSDocTag;

declare const JSDocBase: typeof Node;

Expand Down
19 changes: 16 additions & 3 deletions src/compiler/ast/doc/base/JSDocPropertyLikeTag.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { Constructor } from "../../../../types";
import { ts } from "../../../../typescript";
import { EntityName } from "../../aliases";
import { Node } from "../../common";
import { JSDocTag } from "../JSDocTag";

export type JSDocPropertyLikeTagExtensionType = Node /*& ModifierableNode*/;
export type JSDocPropertyLikeTagExtensionType = Node<ts.JSDocPropertyLikeTag> & JSDocTag;

export interface JSDocPropertyLikeTag {
// todo: methods
/** Gets the name of the JS doc property like tag. */
getName(): string;
/** Gets the name node of the JS doc property like tag. */
getNameNode(): EntityName;
}

export function JSDocPropertyLikeTag<T extends Constructor<JSDocPropertyLikeTagExtensionType>>(Base: T): Constructor<JSDocPropertyLikeTag> & T {
return class extends Base implements JSDocPropertyLikeTag {
// todo: methods
// todo: more methods
getName() {
return this.getNameNode().getText();
}

getNameNode() {
return this.getNodeFromCompilerNode(this.compilerNode.name);
}
};
}
41 changes: 41 additions & 0 deletions src/tests/compiler/doc/base/jsDocPropertyLikeTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from "chai";
import { JSDocPropertyLikeTag } from "../../../../compiler";
import { TypeGuards } from "../../../../utils";
import { getInfoFromText } from "../../testHelpers";

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

describe(nameof<JSDocPropertyLikeTag>(d => d.getName), () => {
function doTest(text: string, expected: string) {
const { descendant } = getInfo(text);
expect(descendant.getName()).to.equal(expected);
}

it("should get when identifier", () => {
doTest("/** @param t - String */\nfunction test() {}", "t");
});

it("should get when fully qualified name", () => {
doTest("/** @param t.t.t - String */\nfunction test() {}", "t.t.t");
});
});

describe(nameof<JSDocPropertyLikeTag>(d => d.getNameNode), () => {
function doTest(text: string, expected: string) {
const { descendant } = getInfo(text);
expect(descendant.getNameNode().getText()).to.equal(expected);
}

it("should get when identifier", () => {
doTest("/** @param t - String */\nfunction test() {}", "t");
});

it("should get when fully qualified name", () => {
doTest("/** @param t.t.t - String */\nfunction test() {}", "t.t.t");
});
});
});

0 comments on commit 9804627

Please sign in to comment.