Skip to content

Commit

Permalink
feat: component import code moved to block
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 8, 2020
1 parent 4998ed6 commit 2c9828c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 44 deletions.
1 change: 1 addition & 0 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const extractComponent = async (
from: follow.from,
request: follow.filePath,
loc: follow.loc,
importedName: follow.importedName,
source: components?.storeSourceFile ? follow.source : undefined,
repository: await packageInfo(
follow.originalFilePath,
Expand Down
20 changes: 17 additions & 3 deletions core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export interface FollowImportType {
from: string;
filePath?: string;
originalFilePath?: string;
importedName?: string;
loc?: CodeLocation;
source?: string;
imported?: string;
}

export const followImports = (
Expand Down Expand Up @@ -118,11 +120,23 @@ export const followImports = (
options,
);
return imported
? { ...imported, from: findImport.from }
: { exportedAs: findImport.importedName, from: findImport.from };
? {
...imported,
importedName: findImport.importedName,
from: findImport.from,
}
: {
exportedAs: findImport.importedName,
importedName: findImport.importedName,
from: findImport.from,
};
} catch (e) {
//non-existing file
return { exportedAs: findImport.importedName, from: findImport.from };
return {
exportedAs: findImport.importedName,
importedName: findImport.importedName,
from: findImport.from,
};
}
}
return undefined;
Expand Down
11 changes: 0 additions & 11 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ const parseSource = async (
);
}
*/
const componentNames = Object.keys(store.components);
for (let i = 0; i < componentNames.length; i += 1) {
const component = store.components[componentNames[i]];
if (component.from) {
component.import = await prettify(
component.imported
? `import { ${component.name} } from '${component.from}';`
: `import ${component.name} from '${component.from}';`,
);
}
}
Object.keys(store.stories).forEach((key: string) => {
const story: Story = store.stories[key];
story.source = getASTSource(source, story.loc);
Expand Down
3 changes: 1 addition & 2 deletions core/instrument/test/__snapshots__/csf-toggle.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Object {
"components": Object {
"Toggle": Object {
"from": "./Toggle",
"import": "import Toggle from './Toggle';
",
"importedName": "Toggle",
"loc": undefined,
"name": "Toggle",
"repository": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`extract-component default-alias-import.js 1`] = `
Object {
"from": "../components/button-default-class-export",
"importedName": "namespace",
"loc": Object {
"end": Object {
"column": 1,
Expand Down Expand Up @@ -36,6 +37,7 @@ export default Button;
exports[`extract-component default-import.js 1`] = `
Object {
"from": "../components/button-default-arrow-func",
"importedName": "default",
"loc": Object {
"end": Object {
"column": 65,
Expand Down Expand Up @@ -71,6 +73,7 @@ Object {
exports[`extract-component named-alias-import.js 1`] = `
Object {
"from": "../components/button-named-arrow-func",
"importedName": "Btn",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand All @@ -82,6 +85,7 @@ Object {
exports[`extract-component named-import.js 1`] = `
Object {
"from": "../components/button-named-class",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 1,
Expand Down Expand Up @@ -113,6 +117,7 @@ export class Button extends React.Component {
exports[`extract-component node-modules.js 1`] = `
Object {
"from": "theme-ui",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 2,
Expand Down Expand Up @@ -163,6 +168,7 @@ export const Button = React.forwardRef((props, ref) => (
exports[`extract-component node-modules-source.js 1`] = `
Object {
"from": "@component-controls/block-components",
"importedName": "Subtitle",
"loc": Object {
"end": Object {
"column": 1,
Expand Down Expand Up @@ -821,6 +827,7 @@ exports.themes = themes;
exports[`extract-component non-existing-file.js 1`] = `
Object {
"from": "./Button",
"importedName": "Btn",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand All @@ -832,6 +839,7 @@ Object {
exports[`extract-component parameters-component.js 1`] = `
Object {
"from": "../components/button-named-arrow-func",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 72,
Expand Down Expand Up @@ -859,6 +867,7 @@ export const Button = props => <button>{\`Hello, \${props.name}\`}</button>;
exports[`extract-component story-component.js 1`] = `
Object {
"from": "../components/button-named-class",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Object {
"exportedAs": "Button",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/exports/cjs-named-export.js",
"from": "../exports/cjs-named-export",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 23,
Expand Down Expand Up @@ -385,6 +386,7 @@ Object {
"exportedAs": "default",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/follow-imports/button-default-export.js",
"from": "./button-default-export",
"importedName": "default",
"loc": Object {
"end": Object {
"column": 24,
Expand Down Expand Up @@ -427,6 +429,7 @@ Object {
"exportedAs": "Button",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/follow-imports/button-named-export.js",
"from": "./",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 30,
Expand All @@ -448,6 +451,7 @@ Object {
"exportedAs": "default",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/follow-imports/button-default-export.js",
"from": "./button-default-export",
"importedName": "namespace",
"loc": Object {
"end": Object {
"column": 24,
Expand Down
12 changes: 4 additions & 8 deletions core/instrument/test/__snapshots__/mdx-component.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../../components/button-named-arrow-func",
"import": "import Button from '../../components/button-named-arrow-func';
",
"importedName": "Button",
"loc": Object {
"end": Object {
"column": 72,
Expand Down Expand Up @@ -59,8 +58,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../../components/button-named-class",
"import": "import Button from '../../components/button-named-class';
",
"importedName": "Btn",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down Expand Up @@ -95,8 +93,7 @@ Object {
"components": Object {
"Button": Object {
"from": "./Button",
"import": "import Button from './Button';
",
"importedName": "Btn",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand All @@ -105,8 +102,7 @@ Object {
},
"Toggle": Object {
"from": "./Toggle",
"import": "import Toggle from './Toggle';
",
"importedName": "Toggle",
"loc": undefined,
"name": "Toggle",
"repository": undefined,
Expand Down
18 changes: 6 additions & 12 deletions core/instrument/test/__snapshots__/mdx-stories.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../components/BaseButton",
"import": "import Button from '../components/BaseButton';
",
"importedName": "default",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down Expand Up @@ -128,8 +127,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../../components/button-default-arrow-func",
"import": "import Button from '../../components/button-default-arrow-func';
",
"importedName": "default",
"loc": Object {
"end": Object {
"column": 65,
Expand Down Expand Up @@ -216,8 +214,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../components/BaseButton",
"import": "import Button from '../components/BaseButton';
",
"importedName": "default",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down Expand Up @@ -362,8 +359,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../components/BaseButton",
"import": "import Button from '../components/BaseButton';
",
"importedName": "default",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down Expand Up @@ -474,8 +470,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../components/BaseButton",
"import": "import Button from '../components/BaseButton';
",
"importedName": "default",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down Expand Up @@ -571,8 +566,7 @@ Object {
"components": Object {
"Button": Object {
"from": "../components/BaseButton",
"import": "import Button from '../components/BaseButton';
",
"importedName": "default",
"loc": undefined,
"name": "Button",
"repository": undefined,
Expand Down
13 changes: 6 additions & 7 deletions core/specification/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,19 @@ export interface StoryComponent {
*/
name: string;
/**
* imported name ex: import { Btn as Button } from 'buttons';
* imported name ex:
* - default import import Button from 'buttons';
* - namespace import import * as Button from 'buttons';
* - named import import { Button } from 'buttons';
* - named alias import import { Btn as Button } from 'buttons';
*/
imported?: string;
importedName?: 'default' | 'namespace' | string;

/**
* imported from
*/
from?: string;

/**
* import string for the component
*/
import?: string;

/**
* resolved import request
*/
Expand Down
10 changes: 9 additions & 1 deletion ui/blocks/src/ComponentSource/ComponentSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ export const ComponentSource: FC<ComponentSourceProps> = ({
id,
name,
});
const source = component && component.import;
let source;
const { from, importedName, name: componentName } = component || {};
if (from) {
source =
importedName !== 'default' && importedName !== 'namespace'
? `import { ${componentName} } from '${from}';`
: `import ${componentName} from '${from}';`;
}

if (!source) {
return null;
}
Expand Down

0 comments on commit 2c9828c

Please sign in to comment.