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

Expressions in propTypes not parsed correctly #36

Open
mik01aj opened this issue Oct 29, 2015 · 4 comments
Open

Expressions in propTypes not parsed correctly #36

mik01aj opened this issue Oct 29, 2015 · 4 comments

Comments

@mik01aj
Copy link

mik01aj commented Oct 29, 2015

Originally reported in styleguidist/react-styleguidist#13 (comment).

I use an expression in React.PropTypes.oneOf(...). This is the output from react-docgen:

{
  "description": "",
  "props": {
    "issue": {
      "type": {
        "name": "object"
      },
      "required": false,
      "description": ""
    },
    "width": {
      "type": {
        "name": "enum",
        "computed": true,
        "value": "_.map(_.keys(coverWidths), _.parseInt)" <------- problem
      },
      "required": false,
      "description": ""
    },
    "style": {
      "type": {
        "name": "object"
      },
      "required": false,
      "description": ""
    }
  }
}

coverWidths is a variable defined in that file. In runtime it's generated correctly, but react-docgen can't parse it.

value in the marked line should be null, and there should be a raw property with it instead.

@fkling confirmed that this is is a bug in react-docgen.

@fkling
Copy link
Member

fkling commented Oct 29, 2015

Thinking about it more, I will have to check again if it is really a bug. It might also be that the computed: true field should indicate that value is the source instead of the real value.

But even then, it's probably better to leave value empty and have the source in raw.

@joaomilho
Copy link

joaomilho commented Apr 24, 2016

I have the same problem, although my example is simpler. Given the following code:

export default function Tooltip () { ... }

Tooltip.arrows = ['up', 'down', 'left', ...];

Tooltip.propTypes = {
  arrow: PropTypes.oneOf(Tooltip.arrows)
};

And this generates the following JSON:

{
  arrow:
   { type: { name: 'enum', computed: true, value: 'Tooltip.arrows' },
     required: false,
     description: '' }
}

@joshfarrant
Copy link

Any further thoughts on this? I've recently run into the same problem with the following:

const KIND = {
  PRIMARY: 'primary',
  SECONDARY: 'secondary'
};

// ...

static propTypes = {
  kind: PropTypes.oneOf(Object.values(KIND))
};

I'd expect this:

{
  "kind": {
    "type": {
      "name": "enum",
      "value": [{
        "value": "'primary'",
        "computed": false
      }, {
        "value": "'secondary'",
        "computed": false
      }]
    },
    "required": false,
    "description": "",
    "defaultValue": {
      "value": "'primary'",
      "computed": false
    }
  }
}

However I'm getting this:

{
  "kind": {
    "type": {
    "name": "enum",
    "computed": true,
    "value": "Object.values(KIND)"
  },
    "required": false,
    "description": "",
    "defaultValue": {
      "value": "'default'",
      "computed": false
    }
  }
}

Is there a workaround for this, or a fix in the works? If not, I'd be happy to look at submitting a PR to address this, if you could point me in the right direction.

@dmitry-zaets
Copy link

related to #153 and #122

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

5 participants