UNPKG

@storybook/addon-svelte-csf

Version:
84 lines (83 loc) 2.86 kB
import { AttributeNotArrayError, AttributeNotArrayOfStringsError, AttributeNotStringError, } from '../../../utils/error/parser/analyse/story.js'; export function getStringValueFromAttribute(params) { const { node, filename, component } = params; if (!node) { return; } const { value } = node; if (value === true) { throw new AttributeNotStringError({ filename, component, attribute: node }); } if (!Array.isArray(value) && value.type === 'ExpressionTag' && value.expression.type === 'Literal') { return value.expression.value; } if (value[0].type === 'Text') { return value[0].data; } if (value[0].type === 'ExpressionTag' && value[0].expression.type === 'Literal' && typeof value[0].expression.value === 'string') { return value[0].expression.value; } throw new AttributeNotStringError({ filename, component, attribute: node }); } export function getArrayOfStringsValueFromAttribute(params) { const { node, filename, component } = params; if (!node) { return []; } const { value } = node; if (value === true) { throw new AttributeNotArrayError({ component, filename, attribute: node, }); } // value is SvelteAST.ExpressionTag if (!Array.isArray(value)) { if (value.expression.type !== 'ArrayExpression') { throw new AttributeNotArrayError({ component, filename, attribute: node, }); } let arrayOfStrings = []; for (const element of value.expression.elements) { if (element?.type !== 'Literal' || typeof element.value !== 'string') { throw new AttributeNotArrayOfStringsError({ filename, component, attribute: node, element, }); } arrayOfStrings.push(element.value); } return arrayOfStrings; } // value is Array<SvelteAST.ExpressionTag | SvelteAST.Text> - I haven't figured out when it would happen if (value[0].type !== 'ExpressionTag' || value[0].expression.type !== 'ArrayExpression') { throw new AttributeNotArrayError({ component, filename, attribute: node, }); } const arrayOfStrings = []; for (const element of value[0].expression.elements) { if (element?.type !== 'Literal' || typeof element.value !== 'string') { throw new AttributeNotArrayOfStringsError({ filename, component, attribute: node, element, }); } arrayOfStrings.push(element.value); } return arrayOfStrings; }