From f081f98ff2d3ec0d48582960563e0eb17948a632 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 18 Jun 2019 10:47:58 +0800 Subject: [PATCH 1/5] Source loader library by @libetl --- lib/source-loader/package.json | 38 + .../inject-decorator.test.js.snap | 858 ++++++++++++++++++ .../abstract-syntax-tree/default-options.js | 12 + .../abstract-syntax-tree/generate-helpers.js | 167 ++++ .../abstract-syntax-tree/inject-decorator.js | 84 ++ .../inject-decorator.test.js | 216 +++++ .../src/abstract-syntax-tree/parse-helpers.js | 159 ++++ .../src/abstract-syntax-tree/parsers/index.js | 20 + .../parsers/parser-flow.js | 13 + .../abstract-syntax-tree/parsers/parser-js.js | 21 + .../abstract-syntax-tree/parsers/parser-ts.js | 21 + .../abstract-syntax-tree/traverse-helpers.js | 203 +++++ lib/source-loader/src/build.js | 30 + .../getRidOfUselessFilePrefixes.js | 46 + .../src/dependencies-lookup/readAsObject.js | 119 +++ lib/source-loader/src/events.js | 2 + lib/source-loader/src/index.js | 6 + lib/source-loader/src/preview.js | 92 ++ 18 files changed, 2107 insertions(+) create mode 100644 lib/source-loader/package.json create mode 100644 lib/source-loader/src/abstract-syntax-tree/__snapshots__/inject-decorator.test.js.snap create mode 100644 lib/source-loader/src/abstract-syntax-tree/default-options.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/generate-helpers.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/inject-decorator.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/inject-decorator.test.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/parse-helpers.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/parsers/index.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/parsers/parser-flow.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/parsers/parser-js.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/parsers/parser-ts.js create mode 100644 lib/source-loader/src/abstract-syntax-tree/traverse-helpers.js create mode 100644 lib/source-loader/src/build.js create mode 100644 lib/source-loader/src/dependencies-lookup/getRidOfUselessFilePrefixes.js create mode 100644 lib/source-loader/src/dependencies-lookup/readAsObject.js create mode 100644 lib/source-loader/src/events.js create mode 100644 lib/source-loader/src/index.js create mode 100644 lib/source-loader/src/preview.js diff --git a/lib/source-loader/package.json b/lib/source-loader/package.json new file mode 100644 index 000000000000..60d4a783a1c4 --- /dev/null +++ b/lib/source-loader/package.json @@ -0,0 +1,38 @@ +{ + "name": "@storybook/source-loader", + "version": "5.2.0-alpha.23", + "description": "Source loader", + "keywords": [ + "lib", + "storybook" + ], + "homepage": "https://github.com/storybookjs/storybook/tree/master/lib/source-loader", + "bugs": { + "url": "https://github.com/storybookjs/storybook/issues" + }, + "repository": { + "type": "git", + "url": "https://github.com/storybookjs/storybook.git", + "directory": "lib/source-loader" + }, + "license": "MIT", + "main": "dist/index.js", + "jsnext:main": "src/index.js", + "scripts": { + "prepare": "node ../../scripts/prepare.js" + }, + "dependencies": { + "@storybook/addons": "5.2.0-alpha.23", + "@storybook/router": "5.2.0-alpha.23", + "core-js": "^3.0.1", + "estraverse": "^4.2.0", + "global": "^4.3.2", + "loader-utils": "^1.2.3", + "prettier": "^1.16.4", + "prop-types": "^15.7.2", + "regenerator-runtime": "^0.12.1" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/lib/source-loader/src/abstract-syntax-tree/__snapshots__/inject-decorator.test.js.snap b/lib/source-loader/src/abstract-syntax-tree/__snapshots__/inject-decorator.test.js.snap new file mode 100644 index 000000000000..6cd7c5d832e7 --- /dev/null +++ b/lib/source-loader/src/abstract-syntax-tree/__snapshots__/inject-decorator.test.js.snap @@ -0,0 +1,858 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`inject-decorator injectDecorator option is false - angular does not inject stories decorator after the all "storiesOf" functions 1`] = ` +"import { Component } from '@angular/core'; +import { storiesOf } from '@storybook/angular'; + +@Component({ + selector: 'storybook-with-ng-content', + template: \`
\`, +}) +class WithNgContentComponent {} + +storiesOf('Custom|ng-content', module).addDecorator(withSourceLoader(__STORY__, __ADDS_MAP__,__MAIN_FILE_LOCATION__,__MODULE_DEPENDENCIES__,__LOCAL_DEPENDENCIES__,__SOURCE_PREFIX__,__IDS_TO_FRAMEWORKS__)).add('Default', () => ({ + template: \`

This is rendered in ng-content

\`, + moduleMetadata: { + declarations: [WithNgContentComponent], + }, +})); +" +`; + +exports[`inject-decorator injectDecorator option is false - flow does not inject stories decorator after the all "storiesOf" functions 1`] = ` +"// @flow +import React from 'react'; +import { storiesOf } from '@storybook/react'; +import { withInfo } from '@storybook/addon-info'; + +import TableComponent from '../components/TableComponent'; + +import type { JssClasses } from '../types'; + +type State = { + value: any, +}; + +type Props = { + classes: JssClasses, + name: string, +}; + +class Table extends React.Component { + constructor(props) { + super(props); + this.state = { + value: undefined, + }; + } + + state: State; + + render() { + return ; + } +} + +const stories = storiesOf('Table', module).addDecorator(withSourceLoader(__STORY__, __ADDS_MAP__,__MAIN_FILE_LOCATION__,__MODULE_DEPENDENCIES__,__LOCAL_DEPENDENCIES__,__SOURCE_PREFIX__,__IDS_TO_FRAMEWORKS__)); +stories.add('Flow Class', withInfo('Lorum Ipsum Nem')(() => )); +" +`; + +exports[`inject-decorator injectDecorator option is false - ts does not inject stories decorator after the all "storiesOf" functions 1`] = ` +"var addSourceDecorator = require(\\"@storybook/source-loader\\").addSource; +import { Component } from '@angular/core'; +import { Store, StoreModule } from '@ngrx/store'; +import { storiesOf, moduleMetadata } from '@storybook/angular'; + +@Component({ + selector: 'storybook-comp-with-store', + template: '
{{this.getSotreState()}}
', +}) +class WithStoreComponent { + private store: Store; + + constructor(store: Store) { + this.store = store; + } + + getSotreState(): string { + return this.store === undefined ? 'Store is NOT injected' : 'Store is injected'; + } +} + +storiesOf('ngrx|Store', module) + .addDecorator( + moduleMetadata({ + imports: [StoreModule.forRoot({})], + declarations: [WithStoreComponent], + }) + ) + .add('With component', () => { + return { + component: WithStoreComponent, + }; + });" +`; + +exports[`inject-decorator injectDecorator option is false does not inject stories decorator after the all "storiesOf" functions 1`] = ` +"import React from 'react'; +import { storiesOf } from '@storybook/react'; +import { withInfo } from '@storybook/addon-info'; +import { action } from '@storybook/addon-actions'; + +import DocgenButton from '../components/DocgenButton'; +import FlowTypeButton from '../components/FlowTypeButton'; +import BaseButton from '../components/BaseButton'; +import TableComponent from '../components/TableComponent'; + +storiesOf('Addons|Info.React Docgen', module).addDecorator(withSourceLoader(__STORY__, __ADDS_MAP__,__MAIN_FILE_LOCATION__,__MODULE_DEPENDENCIES__,__LOCAL_DEPENDENCIES__,__SOURCE_PREFIX__,__IDS_TO_FRAMEWORKS__)) + .add( + 'Comments from PropType declarations', + withInfo( + 'Comments above the PropType declarations should be extracted from the React component file itself and rendered in the Info Addon prop table' + )(() => ) + ) + .add( + 'Comments from Flow declarations', + withInfo( + 'Comments above the Flow declarations should be extracted from the React component file itself and rendered in the Info Addon prop table' + )(() => ) + ) + .add( + 'Comments from component declaration', + withInfo( + 'Comments above the component declaration should be extracted from the React component file itself and rendered below the Info Addon heading' + )(() => ) + ); + +const markdownDescription = \` +#### You can use markdown in your withInfo() description. + +Sometimes you might want to manually include some code examples: +~~~js +const Button = () =>