Skip to content

Commit c8a00cb

Browse files
[api] export existing JSDoc helpers (#65588)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 89aecc9 commit c8a00cb

3 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎packages/typescript/src/ast/is.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import type {
2020
ForInOrOfStatement,
2121
ForStatement,
2222
Identifier,
23+
JSDocLink,
24+
JSDocLinkCode,
25+
JSDocLinkPlain,
2326
JSDocTypeExpression,
2427
JSDocTypeLiteral,
2528
JsxTagNameExpression,
@@ -308,7 +311,7 @@ function isImportCall(node: Node): node is Node & { readonly expression: MetaPro
308311
&& (node.expression as MetaProperty).keywordToken === SyntaxKind.ImportKeyword;
309312
}
310313

311-
function isJSDocLinkLike(node: Node): boolean {
314+
export function isJSDocLinkLike(node: Node): node is JSDocLink | JSDocLinkCode | JSDocLinkPlain {
312315
return isJSDocLink(node) || isJSDocLinkCode(node) || isJSDocLinkPlain(node);
313316
}
314317

‎packages/typescript/src/ast/jsdoc.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ function filterOwnedJSDocTags(hostNode: Node, comments: JSDoc[]): (JSDoc | JSDoc
127127
return result;
128128
}
129129

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

155-
function getJSDocTypeParameterTags(typeParam: TypeParameterDeclaration): JSDocTemplateTag[] {
156+
/** Gets the JSDoc template tags that declare a type parameter's name. */
157+
export function getJSDocTypeParameterTags(typeParam: TypeParameterDeclaration): readonly JSDocTemplateTag[] {
156158
const result: JSDocTemplateTag[] = [];
157159
const name = typeParam.name.text;
158160
for (const tag of getJSDocTags(typeParam.parent)) {

‎packages/typescript/test/sync/ast.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,88 @@ test("index signatures allow an omitted type", () => {
122122
assert.strictEqual(declaration.members.length, 2);
123123
});
124124

125+
describe("JSDoc helpers", { concurrency }, () => {
126+
test("isJSDocLinkLike narrows all three link kinds", () => {
127+
const isLink: (node: Node) => node is astModule.JSDocLink | astModule.JSDocLinkCode | astModule.JSDocLinkPlain = astModule.isJSDocLinkLike;
128+
const nodes: Node[] = [
129+
factory.createJSDocLink(createIdentifier("target"), "link text"),
130+
factory.createJSDocLinkCode(createIdentifier("target"), "code text"),
131+
factory.createJSDocLinkPlain(createIdentifier("target"), "plain text"),
132+
factory.createJSDocText("ordinary text"),
133+
factory.createJSDocNameReference(createIdentifier("target")),
134+
createIdentifier("target"),
135+
];
136+
const links = nodes.filter(isLink);
137+
assert.deepStrictEqual(links, nodes.slice(0, 3));
138+
assert.deepStrictEqual(links.map(link => link.text), ["link text", "code text", "plain text"]);
139+
});
140+
141+
test("getJSDocParameterTags matches identifiers by name", () => {
142+
using api = spawnAPI();
143+
const source = api.createSourceFile(
144+
"/parameters.ts",
145+
`/**
146+
* @param second the second parameter
147+
* @param first the first parameter
148+
* @param first another description
149+
* @param first.property a nested property
150+
*/
151+
function f(first: object, second: string, undocumented: number) {}
152+
`,
153+
).sourceFile;
154+
const declaration = source.statements[0];
155+
assert.ok(astModule.isFunctionDeclaration(declaration));
156+
const tags = astModule.getJSDocTags(declaration).filter(astModule.isJSDocParameterTag);
157+
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[0]), [tags[1], tags[2]]);
158+
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[1]), [tags[0]]);
159+
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[2]), []);
160+
});
161+
162+
test("getJSDocTypeParameterTags matches names within template tags", () => {
163+
using api = spawnAPI();
164+
const source = api.createSourceFile(
165+
"/templates.ts",
166+
`/**
167+
* @template U, V shared description
168+
* @template T the first description
169+
* @template T another description
170+
* @template Other an unrelated type parameter
171+
*/
172+
function f<T, U, V, Undocumented>() {}
173+
`,
174+
).sourceFile;
175+
const declaration = source.statements[0];
176+
assert.ok(astModule.isFunctionDeclaration(declaration));
177+
const parameters = declaration.typeParameters;
178+
assert.ok(parameters);
179+
const tags = astModule.getJSDocTags(declaration).filter(astModule.isJSDocTemplateTag);
180+
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[0]), [tags[1], tags[2]]);
181+
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[1]), [tags[0]]);
182+
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[2]), [tags[0]]);
183+
assert.deepStrictEqual(astModule.getJSDocTypeParameterTags(parameters[3]), []);
184+
});
185+
186+
test("getJSDocParameterTags matches binding patterns by position", () => {
187+
using api = spawnAPI();
188+
const source = api.createSourceFile(
189+
"/bindings.js",
190+
`/**
191+
* @param {string} named a named parameter
192+
* @param {object} options an object parameter
193+
* @param {Array} entries an array parameter
194+
*/
195+
function f(named, { value }, [entry], { undocumented }) {}
196+
`,
197+
).sourceFile;
198+
const declaration = source.statements[0];
199+
assert.ok(astModule.isFunctionDeclaration(declaration));
200+
const tags = astModule.getJSDocTags(declaration).filter(astModule.isJSDocParameterTag);
201+
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[1]), [tags[1]]);
202+
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[2]), [tags[2]]);
203+
assert.deepStrictEqual(astModule.getJSDocParameterTags(declaration.parameters[3]), []);
204+
});
205+
});
206+
125207
describe("AST property contracts", { concurrency }, () => {
126208
test("JSDoc typedefs expose type nodes", () => {
127209
using api = spawnAPI();

0 commit comments

Comments
 (0)