Skip to content

Commit

Permalink
fix: second level parameter parse + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 14, 2020
1 parent 300cbeb commit 35e34ae
Show file tree
Hide file tree
Showing 4 changed files with 432 additions and 4 deletions.
347 changes: 347 additions & 0 deletions core/instrument/src/__snapshots__/get-csf-stories.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Extract CSF stories No parameters 1`] = `
Object {
"myStory": Object {
"parameters": Array [],
"source": Object {
"end": Object {
"column": 37,
"line": 2,
},
"start": Object {
"column": 35,
"line": 2,
},
},
},
}
`;

exports[`Extract CSF stories Props parameters 1`] = `
Object {
"myStory": Object {
"parameters": Array [
Object {
"loc": Object {
"end": Object {
"column": 34,
"line": 2,
},
"start": Object {
"column": 29,
"line": 2,
},
},
"name": "props",
"value": "props",
},
],
"source": Object {
"end": Object {
"column": 40,
"line": 2,
},
"start": Object {
"column": 38,
"line": 2,
},
},
},
}
`;

exports[`Extract CSF stories Three levels - alias 1`] = `
Object {
"myStory": Object {
"parameters": Array [
Object {
"loc": Object {
"end": Object {
"column": 60,
"line": 2,
},
"start": Object {
"column": 30,
"line": 2,
},
},
"name": undefined,
"value": Array [
Object {
"loc": Object {
"end": Object {
"column": 53,
"line": 2,
},
"start": Object {
"column": 38,
"line": 2,
},
},
"name": "name",
"value": Array [
Object {
"loc": Object {
"end": Object {
"column": 45,
"line": 2,
},
"start": Object {
"column": 40,
"line": 2,
},
},
"name": "first",
"value": "first",
},
Object {
"loc": Object {
"end": Object {
"column": 51,
"line": 2,
},
"start": Object {
"column": 47,
"line": 2,
},
},
"name": "last",
"value": "last",
},
],
},
Object {
"loc": Object {
"end": Object {
"column": 58,
"line": 2,
},
"start": Object {
"column": 55,
"line": 2,
},
},
"name": "age",
"value": "age",
},
],
},
],
"source": Object {
"end": Object {
"column": 67,
"line": 2,
},
"start": Object {
"column": 65,
"line": 2,
},
},
},
}
`;

exports[`Extract CSF stories Two levels - alias 1`] = `
Object {
"myStory": Object {
"parameters": Array [
Object {
"loc": Object {
"end": Object {
"column": 50,
"line": 2,
},
"start": Object {
"column": 30,
"line": 2,
},
},
"name": undefined,
"value": Array [
Object {
"loc": Object {
"end": Object {
"column": 43,
"line": 2,
},
"start": Object {
"column": 38,
"line": 2,
},
},
"name": "name",
"value": "MyNam",
},
Object {
"loc": Object {
"end": Object {
"column": 48,
"line": 2,
},
"start": Object {
"column": 45,
"line": 2,
},
},
"name": "age",
"value": "age",
},
],
},
],
"source": Object {
"end": Object {
"column": 57,
"line": 2,
},
"start": Object {
"column": 55,
"line": 2,
},
},
},
}
`;

exports[`Extract CSF stories Two levels sub parameters 1`] = `
Object {
"myStory": Object {
"parameters": Array [
Object {
"loc": Object {
"end": Object {
"column": 43,
"line": 2,
},
"start": Object {
"column": 30,
"line": 2,
},
},
"name": undefined,
"value": Array [
Object {
"loc": Object {
"end": Object {
"column": 36,
"line": 2,
},
"start": Object {
"column": 32,
"line": 2,
},
},
"name": "name",
"value": "name",
},
Object {
"loc": Object {
"end": Object {
"column": 41,
"line": 2,
},
"start": Object {
"column": 38,
"line": 2,
},
},
"name": "age",
"value": "age",
},
],
},
],
"source": Object {
"end": Object {
"column": 50,
"line": 2,
},
"start": Object {
"column": 48,
"line": 2,
},
},
},
}
`;

exports[`Extract CSF stories Two parameters 1`] = `
Object {
"myStory": Object {
"parameters": Array [
Object {
"loc": Object {
"end": Object {
"column": 35,
"line": 2,
},
"start": Object {
"column": 30,
"line": 2,
},
},
"name": "props",
"value": "props",
},
Object {
"loc": Object {
"end": Object {
"column": 44,
"line": 2,
},
"start": Object {
"column": 37,
"line": 2,
},
},
"name": "context",
"value": "context",
},
],
"source": Object {
"end": Object {
"column": 51,
"line": 2,
},
"start": Object {
"column": 49,
"line": 2,
},
},
},
}
`;

exports[`Extract CSF stories Typescript 1`] = `
Object {
"myStory": Object {
"parameters": Array [
Object {
"loc": Object {
"end": Object {
"column": 47,
"line": 2,
},
"start": Object {
"column": 30,
"line": 2,
},
},
"name": "props",
"value": "props",
},
],
"source": Object {
"end": Object {
"column": 54,
"line": 2,
},
"start": Object {
"column": 52,
"line": 2,
},
},
},
}
`;
20 changes: 16 additions & 4 deletions core/instrument/src/babel-traverse/get-function-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Story, CodeSource, StoryParameters } from '../types';

interface KeyType {
name: string;
}
interface ASTPropNode {
name?: string;
loc: CodeSource;
properties?: any;
key?: KeyType;
}
export const extractFunctionParameters = (story: Story) => ({
ArrowFunctionExpression: (path: any) => {
const node = path.node;
if (!story.parameters) {
story.parameters = [];
}
const pushParams = (node: ASTPropNode, parameters: StoryParameters) => {
const pushParams = (
node: ASTPropNode,
parameters: StoryParameters,
key?: KeyType,
) => {
const loc = {
start: {
column: node.loc.start.column,
Expand All @@ -25,17 +33,21 @@ export const extractFunctionParameters = (story: Story) => ({
if (node.name) {
parameters.push({
value: node.name,
name: key ? key.name : node.name,
loc,
});
} else if (node.properties) {
const nestedParameters: StoryParameters = [];
parameters.push({
value: nestedParameters,
name: key ? key.name : node.name,
loc,
});
node.properties.forEach(({ value }: { value: ASTPropNode }) => {
pushParams(value, nestedParameters);
});
node.properties.forEach(
({ value, key }: { value: ASTPropNode; key?: KeyType }) => {
pushParams(value, nestedParameters, key);
},
);
}
};
if (node.params) {
Expand Down
Loading

0 comments on commit 35e34ae

Please sign in to comment.