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
8 changes: 5 additions & 3 deletions packages/typescript/src/api/node/node.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
getFirstToken,
getLastToken,
getTokenPosOfNode,
isJSDoc,
type JSDoc,
ModifierFlags,
type Node,
type NodeArray,
Expand Down Expand Up @@ -305,15 +307,15 @@ export class RemoteNode extends RemoteNodeBase implements Node {
}
}

get jsDoc(): readonly Node[] | undefined {
get jsDoc(): readonly JSDoc[] | undefined {
if (!this.hasChildren()) {
return undefined;
}
let result: Node[] | undefined;
let result: JSDoc[] | undefined;
let next = this.index + 1;
do {
const child = this.getOrCreateChildAtNodeIndex(next);
if (!(child instanceof RemoteNodeList) && child.kind === SyntaxKind.JSDoc) {
if (!(child instanceof RemoteNodeList) && isJSDoc(child)) {
(result ??= []).push(child);
}
next = child.next;
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/ast/ast.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ export interface JSDocOverloadTag extends JSDocTagBase {
}
export interface JSDocTypedefTag extends JSDocTagBase {
readonly kind: SyntaxKind.JSDocTypedefTag;
readonly typeExpression?: Node;
readonly typeExpression?: JSDocTypeExpression | JSDocTypeLiteral;
readonly name?: JSDocFullName;
}
export interface JSDocSignature extends JSDocTypeBase, DeclarationBase {
Expand Down
3 changes: 2 additions & 1 deletion packages/typescript/src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
ForStatement,
Identifier,
IfStatement,
JSDoc,
JsxAttribute,
JsxExpression,
JsxSpreadAttribute,
Expand Down Expand Up @@ -138,7 +139,7 @@ export interface Node extends ReadonlyTextRange {
readonly kind: SyntaxKind;
readonly flags: NodeFlags;
readonly parent: Node;
readonly jsDoc?: readonly Node[] | undefined;
readonly jsDoc?: readonly JSDoc[] | undefined;
childrenIter<TNext = void>(): Generator<Node, TNext | undefined, TNext>;
forEachChild<T>(visitor: (node: Node) => T, visitArray?: (nodes: NodeArray<Node>) => T): T | undefined;
getSourceFile(): SourceFile;
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/src/ast/factory.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5210,7 +5210,7 @@ export function createJSDocOverloadTag(tagName: Identifier, typeExpression: Type
}) as unknown as JSDocOverloadTag;
}

export function createJSDocTypedefTag(tagName: Identifier, typeExpression?: Node, name?: JSDocFullName, comment?: readonly JSDocComment[]): JSDocTypedefTag {
export function createJSDocTypedefTag(tagName: Identifier, typeExpression?: JSDocTypeExpression | JSDocTypeLiteral, name?: JSDocFullName, comment?: readonly JSDocComment[]): JSDocTypedefTag {
return new NodeObject(SyntaxKind.JSDocTypedefTag, {
tagName,
typeExpression,
Expand Down Expand Up @@ -5987,7 +5987,7 @@ export function updateJSDocOverloadTag(node: JSDocOverloadTag, tagName: Identifi
return node.tagName !== tagName || node.typeExpression !== typeExpression || node.comment !== comment ? createJSDocOverloadTag(tagName, typeExpression, comment) : node;
}

export function updateJSDocTypedefTag(node: JSDocTypedefTag, tagName: Identifier, typeExpression?: Node, name?: JSDocFullName, comment?: readonly JSDocComment[]): JSDocTypedefTag {
export function updateJSDocTypedefTag(node: JSDocTypedefTag, tagName: Identifier, typeExpression?: JSDocTypeExpression | JSDocTypeLiteral, name?: JSDocFullName, comment?: readonly JSDocComment[]): JSDocTypedefTag {
return node.tagName !== tagName || node.typeExpression !== typeExpression || node.name !== name || node.comment !== comment ? createJSDocTypedefTag(tagName, typeExpression, name, comment) : node;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/typescript/src/ast/visitor.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ import {
isImportClause,
isJSDocFullName,
isJSDocReturnTag,
isJSDocTypeExpressionOrJSDocTypeLiteral,
isJsxAttributeName,
isJsxAttributes,
isJsxAttributeValue,
Expand Down Expand Up @@ -1330,7 +1331,7 @@ const visitEachChildTable: Record<number, VisitEachChildFunction> = {
},
[SyntaxKind.JSDocTypedefTag]: (node: JSDocTypedefTag, visitor: Visitor): JSDocTypedefTag => {
const _tagName = visitNode(node.tagName, visitor, isIdentifier);
const _typeExpression = visitNode(node.typeExpression, visitor);
const _typeExpression = visitNode(node.typeExpression, visitor, isJSDocTypeExpressionOrJSDocTypeLiteral);
const _name = visitNode(node.name, visitor, isJSDocFullName);
const _comment = visitNodes(node.comment, visitor);
return updateJSDocTypedefTag(node, _tagName, _typeExpression, _name, _comment);
Expand Down
36 changes: 35 additions & 1 deletion packages/typescript/test/sync/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,40 @@ test("index signatures allow an omitted type", () => {
});

describe("AST property contracts", { concurrency }, () => {
test("JSDoc typedefs expose type nodes", () => {
using api = spawnAPI();
const source = api.createSourceFile(
"/typedef.js",
`/** @typedef {string} Text */
function text() {}
/**
* @typedef {Object} Options
* @property {string} name
*/
function options() {}
/** @typedef Missing */
function missing() {}
`,
).sourceFile;
const kinds = [SyntaxKind.JSDocTypeExpression, SyntaxKind.JSDocTypeLiteral, undefined];
const declarations = source.statements.filter(astModule.isFunctionDeclaration);
assert.strictEqual(declarations.length, kinds.length);
for (const [index, declaration] of declarations.entries()) {
const docs: readonly JSDoc[] | undefined = declaration.jsDoc;
const tag = docs?.[0].tags?.[0];
assert.ok(tag && astModule.isJSDocTypedefTag(tag));
const type: astModule.JSDocTypeExpression | astModule.JSDocTypeLiteral | undefined = tag.typeExpression;
assert.strictEqual(type?.kind, kinds[index]);
assert.strictEqual(visitEachChild(tag, node => node), tag);
assert.strictEqual(getSynthesizedDeepClone(tag).typeExpression?.kind, kinds[index]);
}

const tagName = factory.createIdentifier("typedef");
const notAType = factory.createTypeReferenceNode(factory.createIdentifier("notAType"));
// @ts-expect-error A typedef type expression must be a JSDoc type expression or literal.
factory.createJSDocTypedefTag(tagName, notAType);
});

test("JSDoc signatures expose tags rather than declaration children", () => {
using api = spawnAPI();
const source = api.createSourceFile(
Expand Down Expand Up @@ -1036,7 +1070,7 @@ export const x = 1;`,
});
try {
const sf = getRemoteSourceFile(api, "/tsconfig.json", "/src/index.ts");
const jsDoc = sf.statements[0].jsDoc?.[0] as JSDoc | undefined;
const jsDoc = sf.statements[0].jsDoc?.[0];
assert.ok(jsDoc?.kind === SyntaxKind.JSDoc);
const comment = jsDoc.comment;
assert.ok(typeof comment !== "string");
Expand Down
6 changes: 5 additions & 1 deletion tools/scripts/tsc/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -4743,7 +4743,11 @@
},
{
"name": "TypeExpression",
"type": "Node",
"type": [
"JSDocTypeExpression",
"JSDocTypeLiteral"
],
"typeGuard": "isJSDocTypeExpressionOrJSDocTypeLiteral",
"optional": true
},
{
Expand Down
8 changes: 5 additions & 3 deletions tools/scripts/tsc/generate-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,8 @@ function emitNodeGeneratedImports(w: CodeWriter) {
w.write(` getFirstToken,`);
w.write(` getLastToken,`);
w.write(` getTokenPosOfNode,`);
w.write(` isJSDoc,`);
w.write(` type JSDoc,`);
w.write(` ModifierFlags,`);
w.write(` type Node,`);
w.write(` type NodeArray,`);
Expand Down Expand Up @@ -1749,15 +1751,15 @@ function emitRemoteNodeClassOpen(w: CodeWriter) {
w.write(` }`);
w.write(` }`);
w.write(``);
w.write(` get jsDoc(): readonly Node[] | undefined {`);
w.write(` get jsDoc(): readonly JSDoc[] | undefined {`);
w.write(` if (!this.hasChildren()) {`);
w.write(` return undefined;`);
w.write(` }`);
w.write(` let result: Node[] | undefined;`);
w.write(` let result: JSDoc[] | undefined;`);
w.write(` let next = this.index + 1;`);
w.write(` do {`);
w.write(` const child = this.getOrCreateChildAtNodeIndex(next);`);
w.write(` if (!(child instanceof RemoteNodeList) && child.kind === SyntaxKind.JSDoc) {`);
w.write(` if (!(child instanceof RemoteNodeList) && isJSDoc(child)) {`);
w.write(` (result ??= []).push(child);`);
w.write(` }`);
w.write(` next = child.next;`);
Expand Down
Loading