react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

Expressions in propTypes not parsed correctly

Open mik01aj opened this issue 9 years ago • 4 comments

Originally reported in https://github.com/sapegin/react-styleguidist/issues/13#issuecomment-146868948.

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.

mik01aj avatar Oct 29 '15 19:10 mik01aj

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.

fkling avatar Oct 29 '15 22:10 fkling

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: '' }
}

joaomilho avatar Apr 24 '16 10:04 joaomilho

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.

joshfarrant avatar Nov 24 '16 10:11 joshfarrant

related to https://github.com/reactjs/react-docgen/issues/153 and https://github.com/reactjs/react-docgen/pull/122

dmitry-zaets avatar Aug 24 '17 11:08 dmitry-zaets