From 8720ca6111fe683a9d9002a4bef0462cd29037d9 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 16 Jul 2020 16:39:05 +0800 Subject: [PATCH] Refactor: Move SBType to ClientAPI --- .../src/frameworks/common/inferControls.ts | 2 +- .../frameworks/common/normalizeArgTypes.ts | 2 +- .../react/react-argtypes.stories.tsx | 4 +- .../src/frameworks/vue/extractArgTypes.ts | 4 +- .../__testfixtures__/proptypes/arrays.js | 0 .../__testfixtures__/proptypes/enums.js | 0 .../__testfixtures__/proptypes/misc.js | 0 .../__testfixtures__/proptypes/objects.js | 0 .../__testfixtures__/proptypes/react.js | 0 .../__testfixtures__/proptypes/scalars.js | 0 .../__testfixtures__/typescript/aliases.tsx | 0 .../__testfixtures__/typescript/arrays.tsx | 0 .../__testfixtures__/typescript/enums.tsx | 0 .../__testfixtures__/typescript/functions.tsx | 0 .../typescript/interfaces.tsx | 0 .../typescript/intersections.tsx | 0 .../__testfixtures__/typescript/records.tsx | 0 .../__testfixtures__/typescript/scalars.tsx | 0 .../__testfixtures__/typescript/tuples.tsx | 0 .../__testfixtures__/typescript/unions.tsx | 0 .../lib/{sbtypes => convert}/convert.test.ts | 2 +- .../{sbtypes/convert.ts => convert/index.ts} | 0 .../{sbtypes => convert}/proptypes/convert.ts | 2 +- .../{sbtypes => convert/proptypes}/index.ts | 0 .../{sbtypes => convert}/proptypes/types.ts | 0 .../typescript/convert.ts | 2 +- .../proptypes => convert/typescript}/index.ts | 0 .../{sbtypes => convert}/typescript/types.ts | 0 .../src/lib/{sbtypes => convert}/utils.ts | 0 addons/docs/src/lib/docgen/createPropDef.ts | 2 +- addons/docs/src/lib/sbtypes/types.ts | 42 ------------------ .../docs/src/lib/sbtypes/typescript/index.ts | 2 - lib/client-api/src/inferArgTypes.ts | 7 +-- lib/client-api/src/types.ts | 43 +++++++++++++++++++ 34 files changed, 54 insertions(+), 60 deletions(-) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/proptypes/arrays.js (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/proptypes/enums.js (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/proptypes/misc.js (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/proptypes/objects.js (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/proptypes/react.js (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/proptypes/scalars.js (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/aliases.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/arrays.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/enums.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/functions.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/interfaces.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/intersections.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/records.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/scalars.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/tuples.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/__testfixtures__/typescript/unions.tsx (100%) rename addons/docs/src/lib/{sbtypes => convert}/convert.test.ts (99%) rename addons/docs/src/lib/{sbtypes/convert.ts => convert/index.ts} (100%) rename addons/docs/src/lib/{sbtypes => convert}/proptypes/convert.ts (97%) rename addons/docs/src/lib/{sbtypes => convert/proptypes}/index.ts (100%) rename addons/docs/src/lib/{sbtypes => convert}/proptypes/types.ts (100%) rename addons/docs/src/lib/{sbtypes => convert}/typescript/convert.ts (96%) rename addons/docs/src/lib/{sbtypes/proptypes => convert/typescript}/index.ts (100%) rename addons/docs/src/lib/{sbtypes => convert}/typescript/types.ts (100%) rename addons/docs/src/lib/{sbtypes => convert}/utils.ts (100%) delete mode 100644 addons/docs/src/lib/sbtypes/types.ts delete mode 100644 addons/docs/src/lib/sbtypes/typescript/index.ts diff --git a/addons/docs/src/frameworks/common/inferControls.ts b/addons/docs/src/frameworks/common/inferControls.ts index c49a7da84e6..010744a7af1 100644 --- a/addons/docs/src/frameworks/common/inferControls.ts +++ b/addons/docs/src/frameworks/common/inferControls.ts @@ -1,7 +1,7 @@ import mapValues from 'lodash/mapValues'; import { ArgTypes, ArgType } from '@storybook/addons'; import { Control } from '@storybook/components'; -import { SBEnumType } from '../../lib/sbtypes'; +import { SBEnumType } from '@storybook/client-api'; const inferControl = (argType: ArgType): Control => { const { type } = argType; diff --git a/addons/docs/src/frameworks/common/normalizeArgTypes.ts b/addons/docs/src/frameworks/common/normalizeArgTypes.ts index ed2ada32cbe..8af93c8e414 100644 --- a/addons/docs/src/frameworks/common/normalizeArgTypes.ts +++ b/addons/docs/src/frameworks/common/normalizeArgTypes.ts @@ -1,6 +1,6 @@ import mapValues from 'lodash/mapValues'; import { ArgTypes } from '@storybook/api'; -import { SBType } from '../../lib/sbtypes'; +import { SBType } from '@storybook/client-api'; const normalizeType = (type: SBType | string) => (typeof type === 'string' ? { name: type } : type); diff --git a/addons/docs/src/frameworks/react/react-argtypes.stories.tsx b/addons/docs/src/frameworks/react/react-argtypes.stories.tsx index 5441c838cad..f07c8c83223 100644 --- a/addons/docs/src/frameworks/react/react-argtypes.stories.tsx +++ b/addons/docs/src/frameworks/react/react-argtypes.stories.tsx @@ -60,7 +60,7 @@ const typescriptFixtures = [ const typescriptStories = storiesOf('ArgTypes/TypeScript', module); typescriptFixtures.forEach((fixture) => { // eslint-disable-next-line import/no-dynamic-require, global-require, no-shadow - const { Component } = require(`../../lib/sbtypes/__testfixtures__/typescript/${fixture}`); + const { Component } = require(`../../lib/convert/__testfixtures__/typescript/${fixture}`); typescriptStories.add(fixture, () => ); }); @@ -69,7 +69,7 @@ const proptypesFixtures = ['arrays', 'enums', 'misc', 'objects', 'react', 'scala const proptypesStories = storiesOf('ArgTypes/PropTypes', module); proptypesFixtures.forEach((fixture) => { // eslint-disable-next-line import/no-dynamic-require, global-require, no-shadow - const { Component } = require(`../../lib/sbtypes/__testfixtures__/proptypes/${fixture}`); + const { Component } = require(`../../lib/convert/__testfixtures__/proptypes/${fixture}`); proptypesStories.add(fixture, () => ); }); diff --git a/addons/docs/src/frameworks/vue/extractArgTypes.ts b/addons/docs/src/frameworks/vue/extractArgTypes.ts index 8ee12409c1d..c7070be8591 100644 --- a/addons/docs/src/frameworks/vue/extractArgTypes.ts +++ b/addons/docs/src/frameworks/vue/extractArgTypes.ts @@ -1,7 +1,7 @@ import { ArgTypes } from '@storybook/api'; import { ArgTypesExtractor, hasDocgen, extractComponentProps } from '../../lib/docgen'; -import { convert } from '../../lib/sbtypes'; -import { trimQuotes } from '../../lib/sbtypes/utils'; +import { convert } from '../../lib/convert'; +import { trimQuotes } from '../../lib/convert/utils'; const SECTIONS = ['props', 'events', 'slots']; diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/arrays.js b/addons/docs/src/lib/convert/__testfixtures__/proptypes/arrays.js similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/arrays.js rename to addons/docs/src/lib/convert/__testfixtures__/proptypes/arrays.js diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/enums.js b/addons/docs/src/lib/convert/__testfixtures__/proptypes/enums.js similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/enums.js rename to addons/docs/src/lib/convert/__testfixtures__/proptypes/enums.js diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/misc.js b/addons/docs/src/lib/convert/__testfixtures__/proptypes/misc.js similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/misc.js rename to addons/docs/src/lib/convert/__testfixtures__/proptypes/misc.js diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/objects.js b/addons/docs/src/lib/convert/__testfixtures__/proptypes/objects.js similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/objects.js rename to addons/docs/src/lib/convert/__testfixtures__/proptypes/objects.js diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/react.js b/addons/docs/src/lib/convert/__testfixtures__/proptypes/react.js similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/react.js rename to addons/docs/src/lib/convert/__testfixtures__/proptypes/react.js diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/scalars.js b/addons/docs/src/lib/convert/__testfixtures__/proptypes/scalars.js similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/proptypes/scalars.js rename to addons/docs/src/lib/convert/__testfixtures__/proptypes/scalars.js diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/aliases.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/aliases.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/aliases.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/aliases.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/arrays.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/arrays.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/arrays.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/arrays.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/enums.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/enums.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/enums.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/enums.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/functions.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/functions.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/functions.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/functions.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/interfaces.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/interfaces.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/interfaces.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/interfaces.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/intersections.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/intersections.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/intersections.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/intersections.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/records.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/records.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/records.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/records.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/scalars.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/scalars.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/scalars.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/scalars.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/tuples.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/tuples.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/tuples.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/tuples.tsx diff --git a/addons/docs/src/lib/sbtypes/__testfixtures__/typescript/unions.tsx b/addons/docs/src/lib/convert/__testfixtures__/typescript/unions.tsx similarity index 100% rename from addons/docs/src/lib/sbtypes/__testfixtures__/typescript/unions.tsx rename to addons/docs/src/lib/convert/__testfixtures__/typescript/unions.tsx diff --git a/addons/docs/src/lib/sbtypes/convert.test.ts b/addons/docs/src/lib/convert/convert.test.ts similarity index 99% rename from addons/docs/src/lib/sbtypes/convert.test.ts rename to addons/docs/src/lib/convert/convert.test.ts index 337586d82f4..0cbdb3a324f 100644 --- a/addons/docs/src/lib/sbtypes/convert.test.ts +++ b/addons/docs/src/lib/convert/convert.test.ts @@ -4,7 +4,7 @@ import { transformSync } from '@babel/core'; import requireFromString from 'require-from-string'; import fs from 'fs'; -import { convert } from './convert'; +import { convert } from './index'; import { normalizeNewlines } from '../utils'; expect.addSnapshotSerializer({ diff --git a/addons/docs/src/lib/sbtypes/convert.ts b/addons/docs/src/lib/convert/index.ts similarity index 100% rename from addons/docs/src/lib/sbtypes/convert.ts rename to addons/docs/src/lib/convert/index.ts diff --git a/addons/docs/src/lib/sbtypes/proptypes/convert.ts b/addons/docs/src/lib/convert/proptypes/convert.ts similarity index 97% rename from addons/docs/src/lib/sbtypes/proptypes/convert.ts rename to addons/docs/src/lib/convert/proptypes/convert.ts index 4217fa21f89..bc4da957340 100644 --- a/addons/docs/src/lib/sbtypes/proptypes/convert.ts +++ b/addons/docs/src/lib/convert/proptypes/convert.ts @@ -1,7 +1,7 @@ /* eslint-disable no-case-declarations */ import mapValues from 'lodash/mapValues'; +import { SBType } from '@storybook/client-api'; import { PTType } from './types'; -import { SBType } from '../types'; import { trimQuotes } from '../utils'; const SIGNATURE_REGEXP = /^\(.*\) => /; diff --git a/addons/docs/src/lib/sbtypes/index.ts b/addons/docs/src/lib/convert/proptypes/index.ts similarity index 100% rename from addons/docs/src/lib/sbtypes/index.ts rename to addons/docs/src/lib/convert/proptypes/index.ts diff --git a/addons/docs/src/lib/sbtypes/proptypes/types.ts b/addons/docs/src/lib/convert/proptypes/types.ts similarity index 100% rename from addons/docs/src/lib/sbtypes/proptypes/types.ts rename to addons/docs/src/lib/convert/proptypes/types.ts diff --git a/addons/docs/src/lib/sbtypes/typescript/convert.ts b/addons/docs/src/lib/convert/typescript/convert.ts similarity index 96% rename from addons/docs/src/lib/sbtypes/typescript/convert.ts rename to addons/docs/src/lib/convert/typescript/convert.ts index a53d0d79cbc..3affe3e7048 100644 --- a/addons/docs/src/lib/sbtypes/typescript/convert.ts +++ b/addons/docs/src/lib/convert/typescript/convert.ts @@ -1,6 +1,6 @@ /* eslint-disable no-case-declarations */ +import { SBType } from '@storybook/client-api'; import { TSType, TSSigType } from './types'; -import { SBType } from '../types'; const convertSig = (type: TSSigType) => { switch (type.type) { diff --git a/addons/docs/src/lib/sbtypes/proptypes/index.ts b/addons/docs/src/lib/convert/typescript/index.ts similarity index 100% rename from addons/docs/src/lib/sbtypes/proptypes/index.ts rename to addons/docs/src/lib/convert/typescript/index.ts diff --git a/addons/docs/src/lib/sbtypes/typescript/types.ts b/addons/docs/src/lib/convert/typescript/types.ts similarity index 100% rename from addons/docs/src/lib/sbtypes/typescript/types.ts rename to addons/docs/src/lib/convert/typescript/types.ts diff --git a/addons/docs/src/lib/sbtypes/utils.ts b/addons/docs/src/lib/convert/utils.ts similarity index 100% rename from addons/docs/src/lib/sbtypes/utils.ts rename to addons/docs/src/lib/convert/utils.ts diff --git a/addons/docs/src/lib/docgen/createPropDef.ts b/addons/docs/src/lib/docgen/createPropDef.ts index 4e2b1a6bd3c..c25faea5464 100644 --- a/addons/docs/src/lib/docgen/createPropDef.ts +++ b/addons/docs/src/lib/docgen/createPropDef.ts @@ -5,7 +5,7 @@ import { createSummaryValue } from '../utils'; import { createFlowPropDef } from './flow/createPropDef'; import { isDefaultValueBlacklisted } from './utils/defaultValue'; import { createTsPropDef } from './typeScript/createPropDef'; -import { convert } from '../sbtypes'; +import { convert } from '../convert'; export type PropDefFactory = ( propName: string, diff --git a/addons/docs/src/lib/sbtypes/types.ts b/addons/docs/src/lib/sbtypes/types.ts deleted file mode 100644 index d0fbed123f3..00000000000 --- a/addons/docs/src/lib/sbtypes/types.ts +++ /dev/null @@ -1,42 +0,0 @@ -interface SBBaseType { - required?: boolean; - raw?: string; -} - -export type SBScalarType = SBBaseType & { - name: 'boolean' | 'string' | 'number' | 'function'; -}; - -export type SBArrayType = SBBaseType & { - name: 'array'; - value: SBType; -}; -export type SBObjectType = SBBaseType & { - name: 'object'; - value: Record; -}; -export type SBEnumType = SBBaseType & { - name: 'enum'; - value: (string | number)[]; -}; -export type SBIntersectionType = SBBaseType & { - name: 'intersection'; - value: SBType[]; -}; -export type SBUnionType = SBBaseType & { - name: 'union'; - value: SBType[]; -}; -export type SBOtherType = SBBaseType & { - name: 'other'; - value: string; -}; - -export type SBType = - | SBScalarType - | SBEnumType - | SBArrayType - | SBObjectType - | SBIntersectionType - | SBUnionType - | SBOtherType; diff --git a/addons/docs/src/lib/sbtypes/typescript/index.ts b/addons/docs/src/lib/sbtypes/typescript/index.ts deleted file mode 100644 index 0ef743fdb7e..00000000000 --- a/addons/docs/src/lib/sbtypes/typescript/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './convert'; -export * from './types'; diff --git a/lib/client-api/src/inferArgTypes.ts b/lib/client-api/src/inferArgTypes.ts index c55b97b4bdf..b26be86f663 100644 --- a/lib/client-api/src/inferArgTypes.ts +++ b/lib/client-api/src/inferArgTypes.ts @@ -1,12 +1,7 @@ import mapValues from 'lodash/mapValues'; -import { ArgTypesEnhancer } from './types'; +import { SBType, ArgTypesEnhancer } from './types'; import { combineParameters } from './parameters'; -type SBType = { - name: string; - value?: string | SBType | SBType[] | Record; -}; - const inferType = (value?: any): SBType => { const type = typeof value; switch (type) { diff --git a/lib/client-api/src/types.ts b/lib/client-api/src/types.ts index 5c3188b1741..927b746e45e 100644 --- a/lib/client-api/src/types.ts +++ b/lib/client-api/src/types.ts @@ -104,3 +104,46 @@ export type RenderContext = StoreItem & { showError: (error: { title: string; description: string }) => void; showException: (err: Error) => void; }; + +interface SBBaseType { + required?: boolean; + raw?: string; +} + +export type SBScalarType = SBBaseType & { + name: 'boolean' | 'string' | 'number' | 'function'; +}; + +export type SBArrayType = SBBaseType & { + name: 'array'; + value: SBType; +}; +export type SBObjectType = SBBaseType & { + name: 'object'; + value: Record; +}; +export type SBEnumType = SBBaseType & { + name: 'enum'; + value: (string | number)[]; +}; +export type SBIntersectionType = SBBaseType & { + name: 'intersection'; + value: SBType[]; +}; +export type SBUnionType = SBBaseType & { + name: 'union'; + value: SBType[]; +}; +export type SBOtherType = SBBaseType & { + name: 'other'; + value: string; +}; + +export type SBType = + | SBScalarType + | SBEnumType + | SBArrayType + | SBObjectType + | SBIntersectionType + | SBUnionType + | SBOtherType;