Skip to content

Commit

Permalink
feat: extract docgentypescript props into readme
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 19, 2020
1 parent 7c90e8e commit fb7b1a8
Show file tree
Hide file tree
Showing 23 changed files with 767 additions and 69 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# table of contents

- [Core packages](#core-packages)

- [Overview](#overview)

- [package: @component-controls/instrument](#package-component-controlsinstrument)

# Core packages

<package-section file="./core/instrument/README.md" section="overview" />
8 changes: 5 additions & 3 deletions core/instrument/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# @component-controls/instrument
# Overview

> Component controls instrumentation library.
## package: @component-controls/instrument

Component controls instrumentation library.
Parsing a source file will generate the following information:
- CSF: List of story named exports
- CSF: Default export stories file information
Expand All @@ -15,7 +17,7 @@ Parsing a source file will generate the following information:
- Extract package.json repository information for the stories file
- Extract package.json repository information for the components file (in canse the components and the stories and in different packages)

## Installation
# Installation

This package is usually installed as part of the @component-controls package, but you can also install it standalone to parse story files and extract information in a script

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"lint-staged": "^9.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"remark": "^11.0.2",
"remark-toc": "^7.0.0",
"rollup": "^1.31.0",
"trash-cli": "^3.0.0",
"ts-jest": "^24.0.2",
Expand Down
31 changes: 15 additions & 16 deletions props-info/react-docgen-typescript/src/transform-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,43 @@ export const transformProps = (props: Props): PropTypes => {
prop.defaultValue = rdProp.defaultValue.value ?? rdProp.defaultValue;
}
const typeName = rdProp.type.name || '';
let type: Partial<TypeInformation> = {};
let propType: Partial<TypeInformation> = {};
if (rdProp.required) {
type.required = rdProp.required;
propType.required = rdProp.required;
}

if (typeName === 'enum') {
type.name = 'enum';
type.value = rdProp.type.value.map(({ value }: any) => {
propType.name = 'enum';
propType.value = rdProp.type.value.map(({ value }: any) => {
const val = cleanQuotes(value);
return {
name: typeof val,
value: val,
};
});
} else if (typeName.endsWith('[]')) {
type.name = 'array';
type.value = [{ name: typeName.split('[]')[0] as TypeValue }];
propType.name = 'array';
propType.value = [{ name: typeName.split('[]')[0] as TypeValue }];
} else if (typeName.match(/\(([^)]+)\)/)) {
type.name = 'function';
propType.name = 'function';
} else if (typeName.match(/\{[^{}]*}/)) {
type.name = 'object';
type.raw = typeName;
propType.name = 'object';
propType.raw = typeName;
} else {
const enumSplit = typeName.split(' | ');
if (enumSplit.length > 1) {
type.name = 'union';
type.value = enumSplit.map(str => ({
propType.name = 'union';
propType.value = enumSplit.map(str => ({
name: cleanQuotes(str),
}));
} else {
type.name = typeName as TypeValue;
propType.name = typeName as TypeValue;
}
}
const raw = rdProp.type.raw || typeName;
if (type.name !== raw) {
type.raw = raw;
if (propType.raw === undefined) {
propType.raw = rdProp.type.raw || typeName;
}
prop.type = type as TypeInformation;
prop.type = propType as TypeInformation;
return { ...acc, [name]: prop };
}, {});
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "boolean",
"raw": "boolean",
},
},
"optionalElement": Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "any",
"raw": "any",
},
},
"optionalElementType": Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "Element",
"raw": "Element",
},
},
"optionalEnum": Object {
Expand Down Expand Up @@ -76,25 +79,29 @@ Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "Message",
"raw": "Message",
},
},
"optionalNode": Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "any",
"raw": "any",
},
},
"optionalNumber": Object {
"defaultValue": 21,
"parentName": "MyComponentProps",
"type": Object {
"name": "number",
"raw": "number",
},
},
"optionalObject": Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "object",
"raw": "object",
},
},
"optionalObjectOf": Object {
Expand Down Expand Up @@ -122,12 +129,14 @@ Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "string",
"raw": "string",
},
},
"optionalSymbol": Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "symbol",
"raw": "symbol",
},
},
"optionalUnion": Object {
Expand All @@ -152,6 +161,7 @@ Object {
"parentName": "MyComponentProps",
"type": Object {
"name": "any",
"raw": "any",
"required": true,
},
},
Expand Down
Loading

0 comments on commit fb7b1a8

Please sign in to comment.