Skip to content

Fix to Flow Maximum Callstack exceeded error on recursive generic structs #428

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

Merged
merged 2 commits into from
Feb 6, 2020

Conversation

FermiDirak
Copy link
Contributor

@FermiDirak FermiDirak commented Jan 24, 2020

Resolves #427

Nested structs using generics currently cause a maximum callstack exceeded error. This PR resolves the issue by short-circuiting the recursion when the type resolves to a generic.

This approach has a shortcoming where the below example will resolve the prop prop as T instead of as string. But this is currently the same behavior seen when switching to typescript and should be fine since it's an an overall improvement to docgen erroring.

import * as React from "react";

type Identity<T> = T;

type Props<T> = {
  prop: Identity<T>;
}

export default function Test(props: Props<string>) {
  return <div />;
}

I'm happy to iterate on this PR, let me know if you need me to change anything.

@elicwhite
Copy link
Collaborator

elicwhite commented Jan 24, 2020

We are running into a similar problem at Facebook on 5.1.0. I haven't gotten it down to a minimal repro yet, but our stack trace looks like this:

RangeError: Maximum call stack size exceeded
        at Object.hasOwnProperty (<anonymous>)

      at defFromValue (node_modules/ast-types/lib/types.js:339:24)
      at DefImpl.check (node_modules/ast-types/lib/types.js:362:24)
      at PredicateType.predicate (node_modules/ast-types/lib/types.js:351:109)
      at PredicateType.Object.<anonymous>.PredicateType.check (node_modules/ast-types/lib/types.js:122:27)
      at Object.handleGenericTypeAnnotation [as GenericTypeAnnotation] (node_modules/react-docgen/dist/utils/getFlowType.js:109:52)
      at getFlowTypeWithResolvedTypes (node_modules/react-docgen/dist/utils/getFlowType.js:296:33)
      at Object.handleGenericTypeAnnotation [as GenericTypeAnnotation] (node_modules/react-docgen/dist/utils/getFlowType.js:135:12)
      at getFlowTypeWithResolvedTypes (node_modules/react-docgen/dist/utils/getFlowType.js:296:33)
      at Object.handleGenericTypeAnnotation [as GenericTypeAnnotation] (node_modules/react-docgen/dist/utils/getFlowType.js:135:12)

Does this line up with the stack trace that this PR is fixing?

Edit:
Yep, my minimal repro got down to this with the same error.

type Props<T> = {
  segments: Array<T>,
};

export default class FBSegments<T> extends React.Component<Props<T>> {
  render(): React.Node {
    return null;
  }

  foo(props: Props<T>) {}
}

Interestingly, if I remove foo, it doesn't error. @FermiDirak, can you include a class example with a method like this in your test fixtures?

@FermiDirak
Copy link
Contributor Author

FermiDirak commented Jan 24, 2020

@TheSavior I've added the minimal repo you've listed which confirms the fix works for class components as well.

https://github.com/reactjs/react-docgen/pull/428/files#diff-a415f878f867f9bdce185d67c4407d86R1520-R1579

@danez
Copy link
Collaborator

danez commented Feb 6, 2020

Thanks for the fix.

@danez danez merged commit 2c42e8c into reactjs:master Feb 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flow Maximum Call stack exceeded on recursive type structures
3 participants