Skip to content

Commit

Permalink
fix: support for arg usage with assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 23, 2020
1 parent 2c277e1 commit 34149aa
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 108 deletions.
20 changes: 13 additions & 7 deletions blocks/core/src/Source/argument-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jsStringEscape from 'js-string-escape';
import {
ControlTypes,
StoryArguments,
CodeLocation,
ArgUsageLocation,
} from '@component-controls/specification';
import {
LoadedComponentControl,
Expand All @@ -20,7 +20,7 @@ export const getArgumentNames = (args: StoryArguments): string[] => {

interface UsageProp {
name: string;
loc: CodeLocation;
loc: ArgUsageLocation;
}
export const getArgumentsUsage = (args: StoryArguments): UsageProp[] => {
return args.reduce((acc: any, a) => {
Expand Down Expand Up @@ -56,18 +56,24 @@ export const mergeControlValues = (
//sort locations in reverse order, so to replace source backwards
locations
.sort((a, b) => {
if (a.loc.start.line === b.loc.start.line) {
return b.loc.start.column - a.loc.start.column;
if (a.loc.loc.start.line === b.loc.loc.start.line) {
return b.loc.loc.start.column - a.loc.loc.start.column;
}
return b.loc.start.line - a.loc.start.line;
return b.loc.loc.start.line - a.loc.loc.start.line;
})
.forEach(l => {
const { start, end } = l.loc;
const { start, end } = l.loc.loc;
const val: LoadedComponentControl = controls[l.name];
if (val && val.value !== val.defaultValue) {
const value: any = val.value;
const strValue: string =
let strValue: string =
val.type === ControlTypes.TEXT ? `"${jsStringEscape(value)}"` : value;
console.log(l);
if (l.loc.name) {
if (l.loc.name.name === l.name) {
strValue = `${l.loc.name.name}: ${strValue}`;
}
}
if (start.line === end.line) {
lines[start.line] = replaceString(
lines[start.line],
Expand Down
31 changes: 29 additions & 2 deletions core/instrument/src/babel/control-values-in-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Story,
StoryArguments,
StoryArgument,
ArgUsageLocation,
} from '@component-controls/specification';
import { adjustSourceLocation } from './utils';

Expand Down Expand Up @@ -30,20 +31,46 @@ export const addArgumentUsage = (
story: Story,
args: StoryArguments,
node: any,
) => {
): ArgUsageLocation | undefined => {
const param = findArguments(args, node.name);

if (param) {
if (param.usage === undefined) {
param.usage = [];
}
param.usage.push(adjustSourceLocation(story, node.loc));
const loc = adjustSourceLocation(story, node.loc);
const existing = param.usage.find(
p =>
p.loc.start.line === loc.start.line &&
p.loc.start.column === loc.start.column &&
p.loc.end.line === loc.end.line &&
p.loc.end.column === loc.end.column,
);
const usage: ArgUsageLocation = { loc };
if (!existing) {
param.usage.push(usage);
return usage;
}
}
return undefined;
};
export const extractArgumentsUsage = (story: Story, args: StoryArguments) => {
return {
Identifier: (path: any) => {
addArgumentUsage(story, args, path.node);
},
Property: (path: any) => {
const node = path.node;
if (node.value.type === 'Identifier' && node.key.type === 'Identifier') {
const usage = addArgumentUsage(story, args, node.value);
if (usage) {
usage.name = {
loc: adjustSourceLocation(story, node.key.loc),
name: node.key.name,
};
path.skip();
}
}
},
};
};
Loading

0 comments on commit 34149aa

Please sign in to comment.