
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
react-docgen-external-proptypes-handler
Advanced tools
evaluate variables from external files for react-docgen
Evaluate variables from external files for react-docgen
This handler does not work with react-docgen >= 5.0.0-beta.1 (yet). react-docgen as of this version will most probably include this functionality by default
All credit goes to Chandrasekhar Pasupuleti for sharing the implementation and Daniel Tschinder for maintaining it.
react-docgen doesn't allow you to use variables from other files to use in propTypes
Example:
import iconNames from './icon-names.js'
Icon.propTypes = {
/** Icon name */
name: PropTypes.oneOf(iconNames).isRequired
}
This doesn't work because it's parsed as a string and not an array
"props": {
"name": {
"type": {
"name": "enum",
"computed": true,
"value": "iconNames"
},
"required": true,
"description": "Icon name"
},
}
npm i react-docgen-external-proptypes-handler --save-dev
If you are importing a variable from an external file to use in propTypes, like in the example below, you need to follow a very specific convention to make it work.
Component code:
import React from 'react'
import PropTypes from 'prop-types'
/* importing variable iconNames from .js file (js, jsx are supported, json is not) */
import iconNames from './icon-names.js'
const Icon = props => {
/*implementation logic*/
}
Icon.propTypes = {
/** Icon name */
name: PropTypes.oneOf(iconNames).isRequired
}
export default Icon
In the imported file, make sure the same variable is exported.
const iconNames = ['copy', 'trash', 'etc']
/* same variable name */
export default iconNames
Â
const docgen = require('react-docgen')
const externalProptypesHandler = require('./react-docgen-external-proptypes-handler')
let metadata = files.map(path => {
/* append display name handler to handlers list */
const handlers = docgen.defaultHandlers.concat(externalProptypesHandler(path))
/* read file to get source code */
const code = fs.readFileSync(path, 'utf8')
/* parse the component code to get metadata */
const data = docgen.parse(code, null, handlers)
return data
})
Enable handlers property in styleguidist config(styleguidist.config.js)
module.export = {
handlers: componentPath =>
require('react-docgen').defaultHandlers.concat(
require('react-docgen-external-proptypes-handler')(componentPath),
require('react-docgen-displayname-handler').createDisplayNameHandler(componentPath)
)
}
Â
:star: this repo
MIT © siddharthkp
FAQs
evaluate variables from external files for react-docgen
The npm package react-docgen-external-proptypes-handler receives a total of 3,455 weekly downloads. As such, react-docgen-external-proptypes-handler popularity was classified as popular.
We found that react-docgen-external-proptypes-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.