-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13263 from storybookjs/13242-fix-cyclic-args
Core: Detect arg inference for cyclic args and warn
- Loading branch information
Showing
2 changed files
with
38 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,51 @@ | ||
import mapValues from 'lodash/mapValues'; | ||
import dedent from 'ts-dedent'; | ||
import { logger } from '@storybook/client-logger'; | ||
import { SBType, ArgTypesEnhancer } from './types'; | ||
import { combineParameters } from './parameters'; | ||
|
||
const inferType = (value?: any): SBType => { | ||
const inferType = (value: any, name: string, visited: Set<any>): SBType => { | ||
const type = typeof value; | ||
switch (type) { | ||
case 'boolean': | ||
case 'string': | ||
case 'number': | ||
case 'function': | ||
return { name: type }; | ||
case 'symbol': | ||
return { name: 'other', value: 'symbol' }; | ||
default: | ||
break; | ||
} | ||
if (Array.isArray(value)) { | ||
const childType: SBType = | ||
value.length > 0 ? inferType(value[0]) : { name: 'other', value: 'unknown' }; | ||
return { name: 'array', value: childType }; | ||
} | ||
if (value) { | ||
const fieldTypes = mapValues(value, (field) => inferType(field)); | ||
if (visited.has(value)) { | ||
logger.warn(dedent` | ||
We've detected a cycle in arg '${name}'. Args should be JSON-serializable (-ish, functions are ok). | ||
More info: https://storybook.js.org/docs/react/essentials/controls#fully-custom-args | ||
`); | ||
return { name: 'other', value: 'cyclic object' }; | ||
} | ||
visited.add(value); | ||
if (Array.isArray(value)) { | ||
const childType: SBType = | ||
value.length > 0 | ||
? inferType(value[0], name, new Set(visited)) | ||
: { name: 'other', value: 'unknown' }; | ||
return { name: 'array', value: childType }; | ||
} | ||
const fieldTypes = mapValues(value, (field) => inferType(field, name, new Set(visited))); | ||
return { name: 'object', value: fieldTypes }; | ||
} | ||
return { name: 'object', value: {} }; | ||
}; | ||
|
||
export const inferArgTypes: ArgTypesEnhancer = (context) => { | ||
const { argTypes: userArgTypes = {}, args = {} } = context.parameters; | ||
const { id, parameters } = context; | ||
const { argTypes: userArgTypes = {}, args = {} } = parameters; | ||
if (!args) return userArgTypes; | ||
const argTypes = mapValues(args, (arg) => ({ type: inferType(arg) })); | ||
const argTypes = mapValues(args, (arg, key) => ({ | ||
type: inferType(arg, `${id}.${key}`, new Set()), | ||
})); | ||
return combineParameters(argTypes, userArgTypes); | ||
}; |