Skip to content

Commit

Permalink
fix: args usage in empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 23, 2020
1 parent 1a90295 commit 2c277e1
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 120 deletions.
4 changes: 2 additions & 2 deletions blocks/core/src/ControlsTable/ControlsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export const ControlsTable: FC<ControlsTableProps & {
item.onAction(props);
},
})),
{ title: 'Reset', onClick: onReset },
{ title: copied ? 'Copied' : 'Copy', onClick: onCopy },
{ title: 'reset', onClick: onReset },
{ title: copied ? 'copied' : 'copy', onClick: onCopy },
]}
/>
</StyledContainer>
Expand Down
2 changes: 1 addition & 1 deletion blocks/core/src/Source/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const Source: FC<SourceProps> = ({
actions.push({ title: 'values', onClick: onMergeValues });
}

actions.push({ title: copied ? 'Copied' : 'Copy', onClick: onCopy });
actions.push({ title: copied ? 'copied' : 'copy', onClick: onCopy });
let colorIdx = 0;
const colorRoll = parameters
? parameters.map(() => {
Expand Down
36 changes: 19 additions & 17 deletions core/instrument/src/babel/control-values-in-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
} from '@component-controls/specification';
import { adjustSourceLocation } from './utils';

const findParameter = (
parameters: StoryArguments,
const findArguments = (
args: StoryArguments,
paramName: string,
): StoryArgument | undefined => {
let result: StoryArgument | undefined;
for (let i = 0; i < parameters.length; i += 1) {
const p = parameters[i];
for (let i = 0; i < args.length; i += 1) {
const p = args[i];
if (Array.isArray(p.value)) {
result = findParameter(p.value, paramName);
result = findArguments(p.value, paramName);
if (result) {
break;
}
Expand All @@ -26,22 +26,24 @@ const findParameter = (
}
return result;
};
export const extractParametersUsage = (
export const addArgumentUsage = (
story: Story,
parameters: StoryArguments,
args: StoryArguments,
node: any,
) => {
const param = findArguments(args, node.name);

if (param) {
if (param.usage === undefined) {
param.usage = [];
}
param.usage.push(adjustSourceLocation(story, node.loc));
}
};
export const extractArgumentsUsage = (story: Story, args: StoryArguments) => {
return {
Identifier: (path: any) => {
const node = path.node;

const param = findParameter(parameters, node.name);

if (param) {
if (param.usage === undefined) {
param.usage = [];
}
param.usage.push(adjustSourceLocation(story, node.loc));
}
addArgumentUsage(story, args, path.node);
},
};
};
21 changes: 14 additions & 7 deletions core/instrument/src/babel/get-function-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
StoryArguments,
} from '@component-controls/specification';
import { adjustSourceLocation } from './utils';
import { extractParametersUsage } from './control-values-in-source';
import {
extractArgumentsUsage,
addArgumentUsage,
} from './control-values-in-source';

interface KeyType {
name: string;
Expand Down Expand Up @@ -70,12 +73,16 @@ export const extractFunctionParameters = (story: Story) => ({
}
if (story.arguments.length) {
const params = story.arguments[0];
traverse(
node.body,
extractParametersUsage(story, [params]),
path.scope,
path,
);
if (node.body.type === 'Identifier') {
addArgumentUsage(story, [params], node.body);
} else {
traverse(
node.body,
extractArgumentsUsage(story, [params]),
path.scope,
path,
);
}
}
path.skip();
},
Expand Down
10 changes: 8 additions & 2 deletions core/instrument/src/babel/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ export const adjustSourceLocation = (
return {
start: {
line: loc.start.line - story.location.start.line,
column: loc.start.column,
column:
loc.start.line === story.location.start.line
? loc.start.column - story.location.start.column
: loc.start.column,
},
end: {
line: loc.end.line - story.location.start.line,
column: loc.end.column,
column:
loc.end.line === story.location.start.line
? loc.end.column - story.location.start.column
: loc.end.column,
},
};
};
60 changes: 30 additions & 30 deletions core/instrument/test/__snapshots__/get-csf-stories.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 34,
"column": 5,
"line": 0,
},
"start": Object {
"column": 29,
"column": 0,
"line": 0,
},
},
Expand Down Expand Up @@ -79,11 +79,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 60,
"column": 31,
"line": 0,
},
"start": Object {
"column": 30,
"column": 1,
"line": 0,
},
},
Expand All @@ -92,11 +92,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 53,
"column": 24,
"line": 0,
},
"start": Object {
"column": 38,
"column": 9,
"line": 0,
},
},
Expand All @@ -105,11 +105,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 45,
"column": 16,
"line": 0,
},
"start": Object {
"column": 40,
"column": 11,
"line": 0,
},
},
Expand All @@ -119,11 +119,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 51,
"column": 22,
"line": 0,
},
"start": Object {
"column": 47,
"column": 18,
"line": 0,
},
},
Expand All @@ -135,11 +135,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 58,
"column": 29,
"line": 0,
},
"start": Object {
"column": 55,
"column": 26,
"line": 0,
},
},
Expand Down Expand Up @@ -174,11 +174,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 35,
"column": 6,
"line": 0,
},
"start": Object {
"column": 30,
"column": 1,
"line": 0,
},
},
Expand All @@ -188,11 +188,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 44,
"column": 15,
"line": 0,
},
"start": Object {
"column": 37,
"column": 8,
"line": 0,
},
},
Expand Down Expand Up @@ -225,11 +225,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 50,
"column": 21,
"line": 0,
},
"start": Object {
"column": 30,
"column": 1,
"line": 0,
},
},
Expand All @@ -238,11 +238,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 43,
"column": 14,
"line": 0,
},
"start": Object {
"column": 38,
"column": 9,
"line": 0,
},
},
Expand All @@ -252,11 +252,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 48,
"column": 19,
"line": 0,
},
"start": Object {
"column": 45,
"column": 16,
"line": 0,
},
},
Expand Down Expand Up @@ -291,11 +291,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 43,
"column": 14,
"line": 0,
},
"start": Object {
"column": 30,
"column": 1,
"line": 0,
},
},
Expand All @@ -304,11 +304,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 36,
"column": 7,
"line": 0,
},
"start": Object {
"column": 32,
"column": 3,
"line": 0,
},
},
Expand All @@ -318,11 +318,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 41,
"column": 12,
"line": 0,
},
"start": Object {
"column": 38,
"column": 9,
"line": 0,
},
},
Expand Down Expand Up @@ -357,11 +357,11 @@ Object {
Object {
"loc": Object {
"end": Object {
"column": 47,
"column": 18,
"line": 0,
},
"start": Object {
"column": 30,
"column": 1,
"line": 0,
},
},
Expand Down
Loading

0 comments on commit 2c277e1

Please sign in to comment.