Skip to content

Commit

Permalink
refactor: introduce FileSourcePosition instead of offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Aug 15, 2023
1 parent 77b854f commit 8ca9e03
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 37 deletions.
12 changes: 7 additions & 5 deletions api/src/rpcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ export type DetectComponentsResponse = {

export type Component = {
componentId: string;
start: number;
end: number;
sourcePosition: FileSourcePosition;
exported: boolean;
};

export type Story = {
componentId: string;
start: number;
end: number;
sourcePosition: FileSourcePosition;
associatedComponentId: string | null;
};

export type StoryArgs = {
sourcePosition: FileSourcePosition;
value: SerializableValue;
};

export type FileSourcePosition = {
start: number;
end: number;
value: SerializableValue;
};
8 changes: 6 additions & 2 deletions component-analyzer/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ComponentAnalyzerFactory = (options: {

export interface BaseComponent {
componentId: string;
offsets: [start: number, end: number];
sourcePosition: FileSourcePosition;
}

export interface Component extends BaseComponent {
Expand All @@ -43,9 +43,13 @@ export interface Story extends BaseComponent {
}

export type StoryArgs = {
sourcePosition: FileSourcePosition;
value: SerializableValue;
};

export type FileSourcePosition = {
start: number;
end: number;
value: SerializableValue;
};

export type BasicComponent = Pick<Component, "componentId" | "extractProps">;
1 change: 0 additions & 1 deletion component-analyzer/react/src/extract-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ export const AlsoNotAStory = {
// {
// name: "HocComponent",
// exported: false,
// offsets: expect.anything(),
// },
{
componentId: "App.tsx:ClassComponent1",
Expand Down
11 changes: 8 additions & 3 deletions component-analyzer/react/src/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ export async function extractReactComponents(
extractArgs: async () =>
storyArgs
? {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
sourcePosition: {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
},
value: await parseSerializableValue(storyArgs),
}
: null,
Expand Down Expand Up @@ -132,7 +134,10 @@ export async function extractReactComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name,
}),
offsets: [statement.getStart(), statement.getEnd()],
sourcePosition: {
start: statement.getStart(),
end: statement.getEnd(),
},
},
node,
name
Expand Down
8 changes: 2 additions & 6 deletions core/src/detect-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,16 @@ async function detectComponentsCore(
);
logger.debug(`Done running component detection`);
for (const component of found.components) {
const [start, end] = component.offsets;
components.push({
componentId: component.componentId,
start,
end,
sourcePosition: component.sourcePosition,
exported: component.exported,
});
}
for (const story of found.stories) {
const [start, end] = story.offsets;
stories.push({
componentId: story.componentId,
start,
end,
sourcePosition: story.sourcePosition,
associatedComponentId: story.associatedComponent?.componentId || null,
});
}
Expand Down
9 changes: 8 additions & 1 deletion daemon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export async function startDaemon({
if (!workspace) {
throw new NotFoundError();
}
return workspace.detectComponents({
const { components, stories } = await workspace.detectComponents({
filePaths: [
path
.relative(
Expand All @@ -339,6 +339,13 @@ export async function startDaemon({
.replace(/\\/g, "/"),
],
});
return {
components: [...components, ...stories].map((c) => ({
componentId: c.componentId,
start: c.sourcePosition.start,
end: c.sourcePosition.end,
})),
};
}
);

Expand Down
1 change: 0 additions & 1 deletion framework-plugins/preact/src/extract-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export const AlsoNotAStory = {
// {
// name: "HocComponent",
// exported: false,
// offsets: expect.anything(),
// },
{
componentId: "App.tsx:ClassComponent1",
Expand Down
11 changes: 8 additions & 3 deletions framework-plugins/preact/src/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ export async function extractPreactComponents(
extractArgs: async () =>
storyArgs
? {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
sourcePosition: {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
},
value: await parseSerializableValue(storyArgs),
}
: null,
Expand All @@ -127,7 +129,10 @@ export async function extractPreactComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name,
}),
offsets: [statement.getStart(), statement.getEnd()],
sourcePosition: {
start: statement.getStart(),
end: statement.getEnd(),
},
},
node,
name
Expand Down
11 changes: 8 additions & 3 deletions framework-plugins/solid/src/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ export async function extractSolidComponents(
extractArgs: async () =>
storyArgs
? {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
sourcePosition: {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
},
value: await parseSerializableValue(storyArgs),
}
: null,
Expand All @@ -129,7 +131,10 @@ export async function extractSolidComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name,
}),
offsets: [statement.getStart(), statement.getEnd()],
sourcePosition: {
start: statement.getStart(),
end: statement.getEnd(),
},
},
node,
name
Expand Down
5 changes: 4 additions & 1 deletion framework-plugins/svelte/src/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export async function extractSvelteComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name: inferComponentNameFromSveltePath(absoluteFilePath),
}),
offsets: [0, (await entry.read()).length],
sourcePosition: {
start: 0,
end: (await entry.read()).length,
},
exported: true,
extractProps: async () =>
analyzeSvelteComponentFromSFC(resolver, absoluteFilePath + ".ts"),
Expand Down
16 changes: 12 additions & 4 deletions framework-plugins/vue2/src/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export async function extractVueComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name: inferComponentNameFromVuePath(vueAbsoluteFilePath),
}),
offsets: [0, fileEntry.size()],
sourcePosition: {
start: 0,
end: fileEntry.size(),
},
exported: true,
extractProps: async () =>
analyzeVueComponentFromTemplate(
Expand Down Expand Up @@ -113,8 +116,10 @@ export async function extractVueComponents(
return {
...baseComponent,
extractArgs: async () => ({
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
sourcePosition: {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
},
value: await parseSerializableValue(storyArgs),
}),
associatedComponent,
Expand Down Expand Up @@ -158,7 +163,10 @@ export async function extractVueComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name,
}),
offsets: [statement.getStart(), statement.getEnd()],
sourcePosition: {
start: statement.getStart(),
end: statement.getEnd(),
},
},
node,
name
Expand Down
16 changes: 12 additions & 4 deletions framework-plugins/vue3/src/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export async function extractVueComponents(
filePath: path.relative(rootDir, vueAbsoluteFilePath),
name: inferComponentNameFromVuePath(vueAbsoluteFilePath),
}),
offsets: [0, fileEntry.size()],
sourcePosition: {
start: 0,
end: fileEntry.size(),
},
exported: true,
extractProps: async () =>
analyzeVueComponentFromTemplate(
Expand Down Expand Up @@ -113,8 +116,10 @@ export async function extractVueComponents(
return {
...baseComponent,
extractArgs: async () => ({
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
sourcePosition: {
start: storyArgs.getStart(),
end: storyArgs.getEnd(),
},
value: await parseSerializableValue(storyArgs),
}),
associatedComponent,
Expand Down Expand Up @@ -158,7 +163,10 @@ export async function extractVueComponents(
filePath: path.relative(rootDir, absoluteFilePath),
name,
}),
offsets: [statement.getStart(), statement.getEnd()],
sourcePosition: {
start: statement.getStart(),
end: statement.getEnd(),
},
},
node,
name
Expand Down
11 changes: 8 additions & 3 deletions storybook-helpers/src/extract-csf3-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ export async function extractCsf3Stories(
filePath: path.relative(rootDir, sourceFile.fileName),
name,
}),
offsets: [statement.getStart(), statement.getEnd()],
sourcePosition: {
start: statement.getStart(),
end: statement.getEnd(),
},
extractArgs: async () =>
args
? {
start: args.getStart(),
end: args.getEnd(),
sourcePosition: {
start: args.getStart(),
end: args.getEnd(),
},
value: await parseSerializableValue(args),
}
: null,
Expand Down

0 comments on commit 8ca9e03

Please sign in to comment.