Skip to content

Commit

Permalink
fix: convert class Sring to string in attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 17, 2021
1 parent 5484b4a commit bd616e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
12 changes: 8 additions & 4 deletions core/instrument/src/babel/extract-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ interface StoryAttribute {
value: any;
}

export const getAttribueValue = (value: any): string =>
value instanceof String ? value.toString() : value;

const nodeToValue = (node: any): any => {
if (node) {
switch (node.type) {
Expand Down Expand Up @@ -76,8 +79,8 @@ export const extractAttributes = (
const name: string = propNode.key
? propNode.key.name ?? propNode.key.value
: propNode.property?.name || propNode.name;

return { ...acc, [name]: attribute.value };
const value = getAttribueValue(attribute.value);
return { ...acc, [name]: value };
} else {
return acc;
}
Expand All @@ -88,7 +91,7 @@ export const extractAttributes = (
}
const attribute = nodeToAttribute(node);
if (attribute) {
return attribute.value;
return getAttribueValue(attribute.value);
}
}
return undefined;
Expand All @@ -100,9 +103,10 @@ export const collectAttributes = (node: any): Record<string, string> => {
if (!attribute.value) {
//console.log(attribute);
} else if (attribute.value.type === 'StringLiteral') {
const value = getAttribueValue(attribute.value.value);
return {
...acc,
[attribute.name.name]: attribute.value.value,
[attribute.name.name]: value,
};
} else if (attribute.value.type === 'JSXExpressionContainer') {
return {
Expand Down
36 changes: 6 additions & 30 deletions core/instrument/src/misc/component-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,24 @@
import { Story, Document } from '@component-controls/core';

const componentName = (
component: string | Record<string, unknown> | undefined,
) => {
if (component) {
if (typeof component === 'string') {
return component;
}
if (typeof component.toString === 'function') {
return component.toString();
}
}
return undefined;
};
export const componentsFromParams = (
element: (Document | Story) & { of?: string },
): string[] => {
const result = [];
const { component } = element;
const name = componentName(component as string);
if (name) {
// transform to string
element.component = name;
result.push(name);
if (component) {
result.push(component as string);
}
const { of: componentShorthand } = element;
const ofName = componentName(componentShorthand);
const { of: ofName } = element;
if (ofName) {
result.push(ofName);
}
const { subcomponents } = element;
if (typeof subcomponents === 'string') {
result.push(subcomponents);
result.push(subcomponents as string);
}
if (typeof subcomponents === 'object') {
element.subcomponents = Object.keys(subcomponents).reduce(
(acc, key) => ({
...acc,
[key]: componentName(subcomponents[key] as string),
}),
{},
);
Object.keys(element.subcomponents).forEach(key =>
result.push((element.subcomponents as any)[key]),
Object.keys(subcomponents).forEach(key =>
result.push((subcomponents as any)[key] as string),
);
}
return result;
Expand Down

0 comments on commit bd616e2

Please sign in to comment.