Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSDoc.getTags()[0].getTagName() fails on @template tag #779

Open
abirmingham opened this issue Jan 15, 2020 · 2 comments
Open

JSDoc.getTags()[0].getTagName() fails on @template tag #779

abirmingham opened this issue Jan 15, 2020 · 2 comments

Comments

@abirmingham
Copy link

abirmingham commented Jan 15, 2020

Describe the bug
typescript version: 3.7.4
ts-morph version: 6.0.2

Here's an odd one! I am unable to call getTagName() on JSDoc tags if they are @template tags. Oddly, I have a number of other tag names in my codebase, several of which are nonstandard, and yet @template is the only offender.

To Reproduce

import { Project, SyntaxKind, ts } from 'ts-morph';

console.log(ts.version); // 3.7.4

const project = new Project();
const file = project.createSourceFile(
    'src/tmp.ts',
    [
        '/** @template Thing this is a thing */',
        'const printThing = (thing: Thing): Thing => {',
        '  console.log(thing);',
        '  return thing;',
        '};',
    ].join('\n'),
);

// Print all tag names
file.getFirstDescendantByKindOrThrow(SyntaxKind.VariableStatement)
    .getJsDocs()
    .forEach(jsDoc => {
        jsDoc.getTags().forEach(tag => {
            console.log(tag.getTagName()); // TypeError: tag.getTagName is not a function
        });
    });

Expected behavior
Console prints @template.

Actual behavior
TypeError: tag.getTagName is not a function

In my case I am able to work around the issue, but it is a bit of a nuisance (and a curiosity!)

@dsherret
Copy link
Owner

Hey Alex!

I didn't look into this yet, but I think the reason this is happening is because this tag is a JSDocTemplateTag:

image

And that node hasn't been wrapped yet (see here under "not exist").

Thanks for reporting and providing the simple reproduction. I'll look into implementing this soon.

@abirmingham
Copy link
Author

@dsherret thanks for the quick reply! That does make sense.

Looking at wrapped-nodes.md, it looks like there might be more cases of this than JSDocTemplateTag, e.g. JSDocAuthorTag. Thinking out loud here, but I wonder if JSDoc.getTags() return type should become a union that includes the unwrapped nodes? Perhaps a union type wouldn't provide the optimal ergonomics, but it would be nice if the types somehow guaranteed that this issue would be caught at compile time rather than run time.

dsherret added a commit that referenced this issue Feb 8, 2020
Still many more tags to implement though.
dsherret added a commit that referenced this issue Feb 23, 2020
Still many more tags to implement though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants