Comments (7)
react-docgen-typescript may be useful for some use cases.
from react-docgen.
Not sure if Typescript AST can be easy converted to Babylon one. Here is an example of parsing Typescript one: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
from react-docgen.
For anyone still finding this, I ended up going with documentalist - it parses typescript as well as markdown and similarly spits out splendid json. It's not built specifically for react components - which can be a good thing - so you'll need to write a little bit of logic to aggregate a component's data to it's props (it's really not that bad though. I might even share a gist when I'm done doing this myself.) So it's closer to something like typedoc - however it's JSON output is nicely documented and (IMHO) much easier to make sense of.
from react-docgen.
Yeah, react-docgen doesn't work with generated code. I don't think is there anything we can do about it, since generated / transpiled code can be quite arbitrary and have helpers that make analysis more difficult.
Regarding TypeScript: I don't think the libraries that react-docgen builds upon will be handle to deal with TypeScript. However, if there is a way to convert a TypeScript AST into an equivalent JavaScraipt / Babylon AST, I'm happy to look into it. That said, I could imagine that there already is a documentation generation tool for TypeScript which likely would work fine for your use case as well.
I'm closing this for now since I don't think there is anything else I can do here.
from react-docgen.
@StJohn3D Please, do share the gist!
from react-docgen.
@sophieH29 so for my needs I ended up not aggregating the data as it allows me to be more flexible in my documentation workflow to just call a function and pass in the name of the export I'm looking for.
That said, when you run documentalist and inspect the json output you should find something like this.
Assuming an index.ts which exports the following:
export interface IMyClassProps ...
export class MyClass extends React.Component<IMyClassProps , IMyClassState>...
{
"typescript": {
"MyClass": {
"kind": "class",
"extends": [
"Component<IMyClassProps, IMyClassState>"
],
},
"IMyClassProps": {
"kind": "interface",
"properties": [
{
"fileName": "src/myClass.tsx",
"flags": {
"isExported": true,
"isExternal": false,
"isOptional": true,
"isPrivate": false,
"isProtected": false,
"isPublic": false,
"isStatic": false
},
"kind": "property",
"name": "headerText",
"sourceUrl": "https://github.com/redacted/src/myClass.tsx#L7",
"type": "undefined | string"
}
]
}
}
}
There's actually WAY more data that you'll get than this but I'm highlighting the relevant bits for brevity.
With that json youtput you could use a regex to find each class' properties...
Assuming you import that json to a variable and then call the following function with that object.
const getData = (docs) => {
const result = []
for (key in docs.typescript) {
const data = docs.typescript[key]
if (data.kind === "class") {
const propsInterfaceName = data.extends[0].match(extendsRegex)[1]
const propsData = docs.typescript[propsInterfaceName]
data.properties = propsData.properties
result.push(data)
}
}
return result
}
Obviously that function is not production ready - there's no error catching - it's just assuming everything will be there. But the point is to show how easy it would be to aggregate the data.
The output of that function would be an array of all of your exported classes. And each object in that array would also contain a propterties
key with all of the aggregated props information from it's associated exported props interface.
Hope that helps!
For my purposes I'm using documentalist to parse both typescript and markdown.
In my markdown I just put comments for where I want to insert data. e.g. <!-- props IMyClassProps -->
and then I've written custom regex patterns and logic to parse the markdown objects and replace those comments with the data from typescript. So it's not something easy to share and very specific to my repo but the examples above should help get the wheels turning :)
from react-docgen.
@StJohn3D thank you! I'll take a look on that
from react-docgen.
Related Issues (20)
- react-docgen cannot extract comments with hermes-parser HOT 3
- Fow indexed access types results in `unknow` HOT 2
- useCallback + useImperativeHandle methods
- Expose more TypeScript types HOT 1
- react-docgen v6.0.0 & older changelogs HOT 2
- Property accessors as template literals causes errors with react-docgen v7 HOT 4
- Better TS enum support
- TS forwardRef support HOT 1
- With Typescript Omit Union types get lost
- With forwardRef Union types get lost HOT 1
- Internal server error: Argument must be Identifier, Literal, QualifiedTypeIdentifier or TSQualifiedName. Received 'TemplateLiteral' HOT 2
- Fails when a namespaced export is used HOT 1
- When postcss modules are present, in some scenarios react-docgen tries to parse css files HOT 2
- Typescript: Component props not parsed with 'call signature' type
- Typescript: Component declaration reference is not recognized by parser
- @defaultValue is not parsed from interface
- "Attempted to resolveName for an unsupported path" error HOT 2
- Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.
- Using `Readonly<T>` utility type results in losing pieces of `T`'s information
- TypeScript: Missing Union Elements of Indexed Access Type
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-docgen.