Skip to content

Conversation

@mauriciosoares
Copy link
Contributor

Description

This PR updates react-docgen to add typescript support, fixing #18

I made use of the getFakeFlow util to also get the fake props for typescript, from my investigation on react-docsgen documentation, it looks like both flow and typescript have a pretty similar object structure, so I renamed this helper to be a bit more generic, and used the same one for ts and flow.

I also added tests for typescript, pretty much the same tests we have for flow, we now have for flow, and the snapshot results are exactly the same.

I also updated babel and its dependencies, the only thing I had to keep was babel-eslint to use as the parser for standard, I tried to update it to @babel/eslint-parser, but for some reason it was complaining about jsx being used on the components, I tried a bunch of different approaches but none really seemed to fix this issue.

I'll add comments to the code on some key areas, to explain the reason why some things were used

module.exports = function (file, { optional = false, all = false } = {}) {
const source = fs.readFileSync(file)
const isTsFile = file.match(/^.*\.(ts|tsx)$/)
const options = { filename: isTsFile ? file : undefined }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to provide the filename option here for ts files (reactjs/react-docgen#355 (comment)), however if we use this option for flow files react-docgen starts to throw some errors, so I'm only using this for .ts|.tsx extensions.

switch (type.name) {
case 'boolean':
return fakeBool()
case 'tuple':
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing really major happening on this file, I only renamed a bunch of funcions, and added this tuple case here to support props like this:

type Props = {
  test: []
}

empty arrays as props are named as tuple, and were falling into the unknown case.

@mauriciosoares mauriciosoares force-pushed the mauricio/update-react-docgen branch from 31f559f to b6b960f Compare June 17, 2021 02:30
@typicode
Copy link
Owner

Awesome, thanks for the PR! Let me know when it's ready to be merged.
Could you also update the docs?

feat: move dependency to devDependency

docs: Update README
@mauriciosoares mauriciosoares force-pushed the mauricio/update-react-docgen branch from b6b960f to 33c95ce Compare June 17, 2021 12:13
@mauriciosoares
Copy link
Contributor Author

The docs are now updated

Btw this PR is already ready for merging, all previous tests are passing, and the typescript tests seems pretty good.

@typicode
Copy link
Owner

Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants