How to use the react-docgen.defaultHandlers function in react-docgen

To help you get started, we’ve selected a few react-docgen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github auth0 / cosmos / tooling / component-metadata.js View on Github external
.map(path => {
      try {
        /* ignore secondary files */
        const directoryName = path.split('/').splice(-2, 1)[0]
        const componentFileName = directoryName.replace('_', '') + '.js'

        /* if component file does not exist, move on*/
        if (!path.includes(componentFileName)) return

        /* append display name handler to handlers list */
        const handlers = docgen.defaultHandlers
          .concat(createDisplayNameHandler(path))
          .concat(deprecationHandler)

        /* 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)

        /* make modifications to prop types to improve documentation */
        if (data.props) {
          Object.values(data.props).forEach(prop => {
            /* remove redundant quotes from default value of type string */
            if (prop.defaultValue) {
              if (typeof prop.defaultValue.value === 'string') {
                prop.defaultValue.value = prop.defaultValue.value.replace(/^'(.*)'$/, '$1')
github facebook / react-360 / website / server / extractDocs.js View on Github external
astPath.get('properties').value
          ),
          null, // TypeAnnotation
          true // static
        )]
      )
    );
    astPath.replace(FauxView);
  }

  return docgen.parse(
    fs.readFileSync(docsList.viewPropTypes),
    viewPropTypesResolver,
    [
      viewPropTypesConversionHandler,
      ...docgen.defaultHandlers,
    ]
  );
}
github facebook / react-native / website / server / extractDocs.js View on Github external
astPath.get('properties').value
          ),
          null, // TypeAnnotation
          true // static
        )]
      )
    );
    astPath.replace(FauxView);
  }

  return docgen.parse(
    fs.readFileSync(docsList.viewPropTypes),
    viewPropTypesResolver,
    [
      viewPropTypesConversionHandler,
    ].concat(docgen.defaultHandlers)
  );
}
github Automattic / wp-calypso / server / devdocs / bin / generate-proptypes-index.js View on Github external
const startTime = process.hrtime();

/**
 * External Dependencies
 */
require( 'babel-register' );
const fs = require( 'fs' );
const path = require( 'path' );
const reactDocgen = require( 'react-docgen' );
const { getPropertyName, getMemberValuePath, resolveToValue } = require( 'react-docgen/dist/utils' );
const util = require( 'client/devdocs/docs-example/util' );
const globby = require( 'globby' );

const root = path.dirname( path.join( __dirname, '..', '..' ) );
const handlers = [ ...reactDocgen.defaultHandlers, commentHandler ];

/**
 * Replaces **'s in comment blocks and trims comments
 * @param {string} str The doc string to clean
 */
function parseDocblock( str ) {
	const lines = str.split( '\n' );
	for ( let i = 0, l = lines.length; i < l; i++ ) {
		lines[ i ] = lines[ i ].replace( /^\s*\*\s?/, '' );
	}
	return lines.join( '\n' ).trim();
}

/**
 * Given a path, this function returns the closest preceding comment, if it exists
 * @param {NodePath} path The node path from react-docgen
github doczjs / docz / core / docz-core / src / utils / docgen.ts View on Github external
const jsParser = async (filepath: string, config: Config) => {
  const resolver =
    config.docgenConfig.resolver ||
    reactDocgen.resolver.findAllExportedComponentDefinitions

  const handlers = reactDocgen.defaultHandlers.concat([
    externalProptypesHandler(filepath),
    actualNameHandler,
  ])

  try {
    const code = fs.readFileSync(filepath, 'utf-8')
    const data = reactDocgen.parse(code, resolver, handlers)
    return { [filepath]: data }
  } catch (err) {
    if (config.debug) throwError(err)
    return null
  }
}
github microsoft / react-native-windows / website / server / extractDocs.js View on Github external
function renderComponent(filepath) {
  const json = docgen.parse(
    fs.readFileSync(filepath),
    docgenHelpers.findExportedOrFirst,
    docgen.defaultHandlers.concat([
      docgenHelpers.stylePropTypeHandler,
      docgenHelpers.deprecatedPropTypeHandler,
    ])
  );

  return componentsToMarkdown('component', json, filepath, componentCount++, styleDocs);
}
github doczjs / docz / core / docz-core / src / utils / docgen / javascript.ts View on Github external
const parseFilepathProps = (filepath: string) => {
    const fullpath = path.resolve(root, filepath)
    const handlers = reactDocgen.defaultHandlers.concat([
      externalProptypesHandler(filepath),
      actualNameHandler,
    ])

    try {
      const code = fs.readFileSync(fullpath, { encoding: 'utf-8' })
      const props = reactDocgen.parse(code, resolver, handlers)
      return { key: unixPath(filepath), value: props }
    } catch (err) {
      if (config.debug) throwError(err)
      return null
    }
  }
github CMSgov / design-system / tools / gulp / docs / react-docgen-handlers / index.js View on Github external
const defaultHandlers = require('react-docgen').defaultHandlers;
const markdownHandler = require('./markdownHandler');

/**
 * Add our custom handlers to react-docgen's default list of handlers
 * @param {String} rootPath - Root docs site path
 * @return {Array}
 */
module.exports = rootPath => {
  return defaultHandlers.concat([markdownHandler(rootPath)]);
};
github alibaba / ice / tools / props-schema-generator / lib / apiExtractor.js View on Github external
function extractSource(source, options = {}) {
  const { filePath, componentName } = options;
  let componentInfo;
  try {
    componentInfo = reactDocs.parse(
      source,
      reactDocs.resolver.findExportedComponentDefinition,
      reactDocs.defaultHandlers
    );
  } catch (e) {
    if (e.message !== 'No suitable component definition found.') {
      e.message = `${e.message}\n[${filePath}]`;
      throw e;
    }
  }

  if (componentInfo) {
    return filter(componentInfo, options);
  }
  return null;
}
github vtex / styleguide / styleguide.config.js View on Github external
handlers: componentPath => {
    require('react-docgen').defaultHandlers.concat(
      require('react-docgen-displayname-handler').createDisplayNameHandler(
        componentPath
      )
    )
  },
  webpackConfig: {