Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-docs: Fix "Cannot read property 'props'" #8731

Merged
merged 2 commits into from
Nov 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions addons/docs/src/lib/docgenUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getTypeSystemHandler, getPropTypeSystem } from './type-system-handlers'

export type PropsExtractor = (component: Component) => PropsTableProps | null;

export type PropDefGetter = (type: Component, section: string) => PropDef[];
export type PropDefGetter = (component: Component, section: string) => PropDef[];

export const str = (o: any) => {
if (!o) {
Expand All @@ -17,27 +17,24 @@ export const str = (o: any) => {
throw new Error(`Description: expected string, got: ${JSON.stringify(o)}`);
};

export const hasDocgen = (obj: any) => !!obj.__docgenInfo;
export const hasDocgen = (component: Component) => !!component.__docgenInfo;

export const hasDocgenSection = (obj: any, section: string) =>
obj.__docgenInfo &&
obj.__docgenInfo[section] &&
Object.keys(obj.__docgenInfo[section]).length > 0;
export const hasDocgenSection = (component: Component, section: string) =>
component &&
component.__docgenInfo &&
component.__docgenInfo[section] &&
Object.keys(component.__docgenInfo[section]).length > 0;

export const extractPropsFromDocgen: PropDefGetter = (type, section) => {
const props: Record<string, PropDef> = {};

const docgenInfoProps = type.__docgenInfo[section];
if (!docgenInfoProps) {
export const extractPropsFromDocgen: PropDefGetter = (component, section) => {
if (!hasDocgenSection(component, section)) {
return [];
}

const props: Record<string, PropDef> = {};
const docgenInfoProps = component.__docgenInfo[section];
const propKeys = Object.keys(docgenInfoProps);
if (propKeys.length === 0) {
return [];
}

// Assuming the props for a given type will all have the same type system.
// Assuming the props for a given component will all have the same type system.
const typeSystem = getPropTypeSystem(docgenInfoProps[propKeys[0]]);
const typeSystemHandler = getTypeSystemHandler(typeSystem);

Expand Down