Example #1
0
function renderComponent(filepath) {
  try {
    const fileContent = fs.readFileSync(filepath);
    const handlers = docgen.defaultHandlers.concat([
      docgenHelpers.stylePropTypeHandler,
      docgenHelpers.deprecatedPropTypeHandler,
      docgenHelpers.jsDocFormatHandler,
    ]);

    const json = docgen.parse(
      fileContent,
      docgenHelpers.findExportedOrFirst,
      handlers
    );
    json.typedef = getTypedef(filepath, fileContent);

    // ReactNative View component imports its propTypes from ViewPropTypes.
    // This trips up docgen though since it expects them to be defined on View.
    // We need to wire them up by manually importing and parsing ViewPropTypes.
    if (filepath.match(/View\/View\.js/)) {
      const viewPropTypesJSON = getViewPropTypes();
      json.props = viewPropTypesJSON.props;
    }

    return componentsToMarkdown('component', json, filepath, componentCount++, styleDocs);
  } catch (e) {
    console.log('error in renderComponent for', filepath);
    throw e;
  }
}
function renderComponent(filepath) {
  var json = docgen.parse(
    fs.readFileSync(filepath),
    docgenHelpers.findExportedOrFirst,
    docgen.defaultHandlers.concat(docgenHelpers.stylePropTypeHandler)
  );

  return componentsToMarkdown('component', json, filepath, n++, styleDocs);
}
Example #3
0
function getDocsForPath(definitionPath, name) {
	return reactDocgen.parse('', function (/* ast, recast */) {
		return definitionPath;
	}, reactDocgen.defaultHandlers.concat([
		function (documentation /*, definition */) {
			documentation.set('displayName', name);
		},
	]));
}
Example #4
0
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);
}
Example #5
0
function renderComponent(filepath) {
  try {
    const fileContent = fs.readFileSync(filepath);
    const json = docgen.parse(
      fileContent,
      docgenHelpers.findExportedOrFirst,
      docgen.defaultHandlers.concat([
        docgenHelpers.stylePropTypeHandler,
        docgenHelpers.deprecatedPropTypeHandler,
        docgenHelpers.jsDocFormatHandler,
      ])
    );
    json.typedef = getTypedef(filepath, fileContent);

    return componentsToMarkdown('component', json, filepath, componentCount++, styleDocs);
  } catch (e) {
    console.log('error in renderComponent for', filepath);
    throw e;
  }
}
Example #6
0
export default function parseMetadata(content, node, options) {
  let components = []
  options = options || {}
  try {
    components = parse(
      content,
      options.resolver || findAllComponentDefinitions,
      defaultHandlers.concat(makeHandlers(node, options.handlers))
    )
  } catch (err) {
    if (err.message === ERROR_MISSING_DEFINITION) return []
    throw err
  }

  if (components.length === 1) {
    components[0].displayName = components[0].displayName.replace(/_\d+$/, ``)
  }

  components.forEach(component => {
    component.docblock = component.description || ``
    component.doclets = parseDoclets(component)
    component.description = cleanDoclets(component.description)

    component.props = Object.keys(component.props || {}).map(propName => {
      const prop = component.props[propName]
      prop.name = propName
      prop.docblock = prop.description || ``
      prop.doclets = parseDoclets(prop, propName)
      prop.description = cleanDoclets(prop.description)

      applyPropDoclets(prop)
      return prop
    })
  })

  return components
}
		default: componentPath =>
			reactDocgen.defaultHandlers.concat(createDisplayNameHandler(componentPath)),
Example #8
0
			var docgenMap = _.reduce(files, function(acc, file) {
				// Bail out with the error if it exists
				if (acc instanceof Error) {
					return acc;
				}

				var isInComponents = /src\/components\//.test(file);
				var isExample = /\/examples\//.test(file);
				var isTest = /\.spec\./.test(file);

				// These three conditions should help us find only the component files
				if (isExample)       { return acc; }
				if (isTest)          { return acc; }
				if (!isInComponents) { return acc; }

				var componentName = extractComponentName(file);

				var definitionMap;
				var isPrivateComponent = false
				var exportIdentiferName;
				var componentSource = fs.readFileSync(file)
				var docs = reactDocgen.parse(
					componentSource,
					// Resolver
					function (ast, recast) {
						definitionMap = {};
						recast.visit(ast, {
							visitObjectExpression: function (path) {
								_.forEach(path.get('properties').value, function (property) {
									if (property.key.name === '_lucidIsPrivate') {
										isPrivateComponent = true;
									}

									if (property.key.name === 'render') {
										var identifier = findParentNodeIdentifier(path);
										if (identifier) {
											definitionMap[identifier] = path;
										}
									}
								});
								return false;
							},
							visitExportDefaultDeclaration: function (path) {
								exportIdentiferName = path.value.declaration.name;
								return false;
							},
						});
						return definitionMap[exportIdentiferName];
					},
					// Handlers, a series of functions through which the documentation is
					// built up.
					reactDocgen.defaultHandlers.concat(function (documentation /*, definition */) {
						// TODO: determine composition from the `import` statements See
						// existing handlers for examples:
						// https://github.com/reactjs/react-docgen/blob/dca8ec9d57b4833f7ddb3164bedf4d74578eee1e/src/handlers/propTypeCompositionHandler.js
						var childComponentDocs = _.map(_.reject(_.keys(definitionMap), _.partial(_.isEqual, exportIdentiferName)), function (childComponentId) {
							return getDocsForPath(definitionMap[childComponentId], childComponentId);
						});
						documentation.set('childComponents', childComponentDocs);
						documentation.set('isPrivateComponent', isPrivateComponent);
					})
				);

				if (!docs.description) {
					return new Error('Missing a description from ' + file + ' - please put a comment block right above `createClass` and make sure to include the proper JSON blob in it.')
				}

				// Pull out the custom json that should be in the description of every module
				var customJson = /\{.*\}/.exec(docs.description);

				// Strip out the custom json out of the description
				docs.description = docs.description.replace(/\{.*\}/, '').trim();

				if (!customJson) {
					return new Error('Unable to find JSON in the description for %s. Every component must have JSON is in header with `categories` at minimum.', file);
				}

				try {
					docs.customData = JSON.parse(customJson);
				} catch(e) {
					return new Error('Unable to parse JSON from the description of ' + file);
				}

				return _.set(acc, componentName, docs);
			}, {});