Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/typescript/src/ast/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import type {
ForInOrOfStatement,
ForStatement,
Identifier,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
JSDocTypeExpression,
JSDocTypeLiteral,
JsxTagNameExpression,
Expand Down Expand Up @@ -309,7 +312,7 @@ function isImportCall(node: Node): node is Node & { readonly expression: MetaPro
&& (node.expression as MetaProperty).keywordToken === SyntaxKind.ImportKeyword;
}

function isJSDocLinkLike(node: Node): boolean {
export function isJSDocLinkLike(node: Node): node is JSDocLink | JSDocLinkCode | JSDocLinkPlain {
return isJSDocLink(node) || isJSDocLinkCode(node) || isJSDocLinkPlain(node);
}

Expand Down
6 changes: 4 additions & 2 deletions packages/typescript/src/ast/jsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ function filterOwnedJSDocTags(hostNode: Node, comments: JSDoc[]): (JSDoc | JSDoc
return result;
}

function getJSDocParameterTags(param: ParameterDeclaration): JSDocParameterTag[] {
/** Gets the JSDoc parameter tags that match a parameter declaration. */
export function getJSDocParameterTags(param: ParameterDeclaration): readonly JSDocParameterTag[] {
const result: JSDocParameterTag[] = [];
const name = param.name;
const parentTags = getJSDocTags(param.parent);
Expand All @@ -152,7 +153,8 @@ function getJSDocParameterTags(param: ParameterDeclaration): JSDocParameterTag[]
return result;
}

function getJSDocTypeParameterTags(typeParam: TypeParameterDeclaration): JSDocTemplateTag[] {
/** Gets the JSDoc template tags that declare a type parameter's name. */
export function getJSDocTypeParameterTags(typeParam: TypeParameterDeclaration): readonly JSDocTemplateTag[] {
const result: JSDocTemplateTag[] = [];
const name = typeParam.name.text;
for (const tag of getJSDocTags(typeParam.parent)) {
Expand Down
82 changes: 82 additions & 0 deletions packages/typescript/test/sync/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,88 @@ test("index signatures allow an omitted type", () => {
assert.strictEqual(declaration.members.length, 2);
});

describe("JSDoc helpers", { concurrency }, () => {
test("isJSDocLinkLike narrows all three link kinds", () => {
const isLink: (node: Node) => node is astModule.JSDocLink | astModule.JSDocLinkCode | astModule.JSDocLinkPlain = astModule.isJSDocLinkLike;
const nodes: Node[] = [
factory.createJSDocLink(createIdentifier("target"), "link text"),
factory.createJSDocLinkCode(createIdentifier("target"), "code text"),
factory.createJSDocLinkPlain(createIdentifier("target"), "plain text"),
factory.createJSDocText("ordinary text"),
factory.createJSDocNameReference(createIdentifier("target")),
createIdentifier("target"),
];
const links = nodes.filter(isLink);
assert.deepStrictEqual(links, nodes.slice(0, 3));
assert.deepStrictEqual(links.map(link => link.text), ["link text", "code text", "plain text"]);
});

test("getJSDocParameterTags matches identifiers by name", () => {
using api = spawnAPI();
const source = api.createSourceFile(
"/parameters.ts",
`/**
* @param second the second parameter
* @param first the first parameter
* @param first another description
* @param first.property a nested property
*/
function f(first: object, second: string, undocumented: number) {}
`,
).sourceFile;
const declaration = source.statements[0];
assert.ok(astModule.isFunctionDeclaration(declaration));
const tags = astModule.getJSDocTags(declaration).filter(astModule.isJSDocParameterTag);
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[0]), [tags[1], tags[2]]);
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[1]), [tags[0]]);
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[2]), []);
});

test("getJSDocTypeParameterTags matches names within template tags", () => {
using api = spawnAPI();
const source = api.createSourceFile(
"/templates.ts",
`/**
* @template U, V shared description
* @template T the first description
* @template T another description
* @template Other an unrelated type parameter
*/
function f<T, U, V, Undocumented>() {}
`,
).sourceFile;
const declaration = source.statements[0];
assert.ok(astModule.isFunctionDeclaration(declaration));
const parameters = declaration.typeParameters;
assert.ok(parameters);
const tags = astModule.getJSDocTags(declaration).filter(astModule.isJSDocTemplateTag);
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[0]), [tags[1], tags[2]]);
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[1]), [tags[0]]);
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[2]), [tags[0]]);
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[3]), []);
});

test("getJSDocParameterTags matches binding patterns by position", () => {
using api = spawnAPI();
const source = api.createSourceFile(
"/bindings.js",
`/**
* @param {string} named a named parameter
* @param {object} options an object parameter
* @param {Array} entries an array parameter
*/
function f(named, { value }, [entry], { undocumented }) {}
`,
).sourceFile;
const declaration = source.statements[0];
assert.ok(astModule.isFunctionDeclaration(declaration));
const tags = astModule.getJSDocTags(declaration).filter(astModule.isJSDocParameterTag);
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[1]), [tags[1]]);
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[2]), [tags[2]]);
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[3]), []);
});
});

describe("AST property contracts", { concurrency }, () => {
test("JSDoc signatures expose tags rather than declaration children", () => {
using api = spawnAPI();
Expand Down
Loading