Support extracting of type information from tagged templates

Open
#375 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
react, typescript
Domain
documentation

Research direction

Start in src/utils/getTypeAnnotation.js at the type-annotation lookup linked in the issue, then compare the shown TaggedTemplateExpression AST with the nodes currently handled. Trace how the react-docgen-annotation-resolver consumes the result. Done means type definitions such as FlexProps are extracted from the styled-components example.

Written by the indexing model from the issue text.

Description

It looks like even if I have a resolver that resolves to components that use tagged templates like styled-components, the type annotations aren't extracted.

Consider the following example:

import styled from "styled-components";

interface FlexProps {
    bool: boolean
}

const Flex = styled('div')<FlexProps>`
	display: flex;
`;

/** @component */
export default Flex;

When used with an react-docgen-annotation-resolver, the type definitions won't be extracted.

I scoped it to this place in code that's responsible for finding type annotations: https://github.com/okonet/react-docgen/blob/8fb401089e3fe2b38d41638bc5690436e23aceb3/src/utils/getTypeAnnotation.js#L22

and it looks like it's searching for specific key typeAnnotation in the AST. For styled-component example, though, it won't present. But what's going to present is

Node {
     type: 'TaggedTemplateExpression',
     start: 95,
     end: 138,
     loc: SourceLocation { start: [Position], end: [Position] },
     tag:
      Node {
        type: 'CallExpression',
        start: 95,
        end: 108,
        loc: [SourceLocation],
        callee: [Node],
        arguments: [Array] },
     quasi:
      Node {
        type: 'TemplateLiteral',
        start: 119,
        end: 138,
        loc: [SourceLocation],
        expressions: [],
        quasis: [Array] },
     typeParameters:
      Node {
        type: 'TSTypeParameterInstantiation',
        start: 108,
        end: 119,
        loc: [SourceLocation],
        params: [Array] } },

So, it looks like there is something related to types in there but its not being properly handled.

Does anyone has any pointers on how to fix it?

Dominant language
TypeScript
Stars
3.8k
Forks
316
Avg merge
3h 28m
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from reactjs/react-docgen

All issues in reactjs/react-docgen

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.