Skip to content

Commit

Permalink
fix: transform of prop to string componnet name
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Sep 9, 2020
1 parent 7079b4b commit f65bcf3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
13 changes: 0 additions & 13 deletions core/instrument/src/babel/remove-mdx-attributes.ts

This file was deleted.

29 changes: 29 additions & 0 deletions core/instrument/src/babel/transform-ast-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import babel from '@babel/core';

export const transformTree = () => {
return {
JSXAttribute: (path: any) => {
const node = path.node;
switch (node.name?.name) {
case 'mdxType': {
path.remove();
break;
}
case 'of': {
if (node.value?.type === 'JSXExpressionContainer') {
const component = node.value;
const name = component.expression.name;
path.replaceWith({
...node,
value: babel.types.stringLiteral(name),
});
}
break;
}
}
},
ExportDefaultDeclaration: (path: any) => {
path.replaceWith(path.node.declaration);
},
};
};
4 changes: 2 additions & 2 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

import { extractCSFStories } from './babel/esm-stories';
import { extractMDXStories } from './babel/mdx-stories';
import { removeMDXAttributes } from './babel/remove-mdx-attributes';
import { transformTree } from './babel/transform-ast-tree';
import { extractStoreComponent } from './babel/extract-component';
import { packageInfo } from './misc/package-info';
import { extractStoryExports } from './misc/mdx-exports';
Expand Down Expand Up @@ -162,7 +162,7 @@ export const parseStories = async (
...otherMDXOptions,
});
const ast = parser.parse(mdxParsed, mergedOptions.parser) as any;
traverse(ast, removeMDXAttributes());
traverse(ast, transformTree());
({ code } = generate(ast, {
retainFunctionParens: true,
retainLines: true,
Expand Down
11 changes: 9 additions & 2 deletions examples/docz-migration/docs/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ menu: Components
---

import { Props } from '@component-controls/blocks';
import { Button } from '../src/components/Button'
import { Button } from '../src/components/Button';
import { Button as PropTypesButton } from '../src/components/PropTypesButton';

## Button
## Props / PropsTable

```js:title=props.mdx
---
Expand All @@ -22,4 +23,10 @@ import { Button } from '../src/components/Button'
<Props of={Button} />
```

## Typescript

<Props of={Button} />

## React PropTypes

<Props of={PropTypesButton} />

0 comments on commit f65bcf3

Please sign in to comment.