Skip to content

Commit 50e87d6

Browse files
committed
fix(jsdoc): avoid crash on deprecated tag parsing
closes #1300
1 parent 7796f6d commit 50e87d6

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎src/utils/jsdoc-parser.util.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ export class JsdocParserUtil {
232232
result.push(
233233
..._.filter(doc.tags, (tag) => tag.kind === kind),
234234
);
235+
} else if ((doc as any) && (doc as any).kind === kind) {
236+
result.push(doc as ts.JSDocTag);
237+
} else if (
238+
(doc as any) &&
239+
typeof (doc as any).kind === "number"
240+
) {
241+
// Ignore other JSDoc node kinds to avoid hard crashes on TS AST variations.
242+
continue;
235243
} else {
236244
throw new Error("Unexpected type");
237245
}

‎test/src/utils/jsdoc-parser.util.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,31 @@ const SECOND = 2;`);
312312
});
313313
});
314314

315+
describe('getJSDocTags()', () => {
316+
it('should support direct JSDocTag entries in cache without throwing', () => {
317+
const sourceFile = project.createSourceFile('test.ts', `class Test {
318+
/**
319+
* @deprecated This method has been replaced by AppStripeService:getPrice
320+
*/
321+
test() {}
322+
}`);
323+
324+
const method = sourceFile.getClass('Test')!.getMethod('test')!;
325+
const deprecatedTag = (method.compilerNode as any).jsDoc[0].tags[0];
326+
const utilWithMockedCache = jsdocParserUtil as any;
327+
328+
utilWithMockedCache.getJSDocs = () => [deprecatedTag];
329+
330+
const result = utilWithMockedCache.getJSDocTags(
331+
method.compilerNode,
332+
SyntaxKind.JSDocDeprecatedTag
333+
);
334+
335+
expect(result).to.have.length(1);
336+
expect(result[0]).to.equal(deprecatedTag);
337+
});
338+
});
339+
315340
describe('parseJSDocNode()', () => {
316341
it('should parse JSDoc node with string comment', () => {
317342
const sourceFile = project.createSourceFile('test.ts', '/** Simple comment */ function test() {}');

0 commit comments

Comments
 (0)