Skip to content

Commit

Permalink
feat: add filepath and locs into meta.json (tajo#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tajo authored Nov 17, 2021
1 parent 5bacdf7 commit df38918
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/ladle/lib/cli/vite-plugin/generate/get-meta-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const getMetaJson = (entryData) => {
let storyIds = [];
/** @type {{[key: string]: any}} */
let storyParams = {};
/** @type {{[key: string]: any}} */
let storyMeta = {};

Object.keys(entryData).forEach((entry) => {
entryData[entry].stories.forEach(({ storyId }) => {
entryData[entry].stories.forEach(({ storyId, locStart, locEnd }) => {
storyMeta[storyId] = { locStart, locEnd, filePath: entry };
storyIds.push(storyId);
});
storyParams = { ...storyParams, ...entryData[entry].storyParams };
Expand All @@ -27,6 +30,7 @@ const getMetaJson = (entryData) => {
storyIds.forEach((storyId) => {
result.stories[storyId] = {
...storyIdToMeta(storyId),
...storyMeta[storyId],
parameters: storyParams[storyId] ? storyParams[storyId].parameters : {},
};
});
Expand Down
2 changes: 2 additions & 0 deletions packages/ladle/lib/cli/vite-plugin/parse/get-named-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const getNamedExports = (
storyId,
componentName,
namedExport,
locStart: namedExportDeclaration.loc.start.line,
locEnd: namedExportDeclaration.loc.end.line,
});
};

Expand Down
2 changes: 2 additions & 0 deletions packages/ladle/lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export type ParsedStoriesResult = {
storyId: string;
componentName: string;
namedExport: string;
locStart: number;
locEnd: number;
}[];
exportDefaultProps: {
title?: string;
Expand Down
39 changes: 39 additions & 0 deletions packages/ladle/tests/__snapshots__/get-meta-json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ exports[`Capital letters in story names converted into delimiters 1`] = `
\\"levels\\": [
\\"Capitalization\\"
],
\\"locStart\\": 3,
\\"locEnd\\": 5,
\\"filePath\\": \\"tests/fixtures/capitalization.stories.tsx\\",
\\"parameters\\": {}
},
\\"capitalization--big-barking-dog\\": {
\\"name\\": \\"Big barking dog\\",
\\"levels\\": [
\\"Capitalization\\"
],
\\"locStart\\": 7,
\\"locEnd\\": 9,
\\"filePath\\": \\"tests/fixtures/capitalization.stories.tsx\\",
\\"parameters\\": {}
}
}
Expand All @@ -39,6 +45,9 @@ exports[`Capital letters in the filename converted into delimiters 1`] = `
\\"levels\\": [
\\"Filename capitalization\\"
],
\\"locStart\\": 3,
\\"locEnd\\": 5,
\\"filePath\\": \\"tests/fixtures/filenameCapitalization.stories.tsx\\",
\\"parameters\\": {}
}
}
Expand All @@ -58,6 +67,9 @@ exports[`Default title is used instead of the file name 1`] = `
\\"levels\\": [
\\"Title\\"
],
\\"locStart\\": 7,
\\"locEnd\\": 9,
\\"filePath\\": \\"tests/fixtures/default-title.stories.tsx\\",
\\"parameters\\": {}
}
}
Expand All @@ -77,6 +89,9 @@ exports[`Extract and merge story parameters 1`] = `
\\"levels\\": [
\\"Title\\"
],
\\"locStart\\": 13,
\\"locEnd\\": 15,
\\"filePath\\": \\"tests/fixtures/story-parameters.stories.tsx\\",
\\"parameters\\": {
\\"baseweb\\": {
\\"theme\\": \\"dark\\",
Expand All @@ -94,6 +109,9 @@ exports[`Extract and merge story parameters 1`] = `
\\"levels\\": [
\\"Title\\"
],
\\"locStart\\": 25,
\\"locEnd\\": 27,
\\"filePath\\": \\"tests/fixtures/story-parameters.stories.tsx\\",
\\"parameters\\": {
\\"baseweb\\": {
\\"theme\\": \\"dark\\",
Expand Down Expand Up @@ -121,6 +139,9 @@ exports[`Extract default parameters 1`] = `
\\"levels\\": [
\\"Title\\"
],
\\"locStart\\": 12,
\\"locEnd\\": 14,
\\"filePath\\": \\"tests/fixtures/default-parameters.stories.tsx\\",
\\"parameters\\": {
\\"baseweb\\": {
\\"foo\\": \\"title\\"
Expand All @@ -144,13 +165,19 @@ exports[`Single file with two stories 1`] = `
\\"levels\\": [
\\"Animals\\"
],
\\"locStart\\": 3,
\\"locEnd\\": 5,
\\"filePath\\": \\"tests/fixtures/animals.stories.tsx\\",
\\"parameters\\": {}
},
\\"animals--dog\\": {
\\"name\\": \\"Dog\\",
\\"levels\\": [
\\"Animals\\"
],
\\"locStart\\": 7,
\\"locEnd\\": 9,
\\"filePath\\": \\"tests/fixtures/animals.stories.tsx\\",
\\"parameters\\": {}
}
}
Expand All @@ -170,20 +197,29 @@ exports[`Story name replaces named export as a story name 1`] = `
\\"levels\\": [
\\"Storyname\\"
],
\\"locStart\\": 3,
\\"locEnd\\": 8,
\\"filePath\\": \\"tests/fixtures/storyname.stories.tsx\\",
\\"parameters\\": {}
},
\\"storyname--capital-city\\": {
\\"name\\": \\"Capital city\\",
\\"levels\\": [
\\"Storyname\\"
],
\\"locStart\\": 13,
\\"locEnd\\": 15,
\\"filePath\\": \\"tests/fixtures/storyname.stories.tsx\\",
\\"parameters\\": {}
},
\\"storyname--champs-élysées\\": {
\\"name\\": \\"Champs élysées\\",
\\"levels\\": [
\\"Storyname\\"
],
\\"locStart\\": 17,
\\"locEnd\\": 19,
\\"filePath\\": \\"tests/fixtures/storyname.stories.tsx\\",
\\"parameters\\": {}
}
}
Expand All @@ -204,6 +240,9 @@ exports[`Turn file name delimiters into spaces and levels correctly 1`] = `
\\"Our animals\\",
\\"Mammals\\"
],
\\"locStart\\": 3,
\\"locEnd\\": 5,
\\"filePath\\": \\"tests/fixtures/our-animals--mammals.stories.tsx\\",
\\"parameters\\": {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ Object {
"stories": Array [
Object {
"componentName": "capitalization$$blue$tiny$cat",
"locEnd": 5,
"locStart": 3,
"namedExport": "BlueTinyCat",
"storyId": "capitalization--blue-tiny-cat",
},
Object {
"componentName": "capitalization$$big$barking$dog",
"locEnd": 9,
"locStart": 7,
"namedExport": "BigBarkingDog",
"storyId": "capitalization--big-barking-dog",
},
Expand All @@ -39,6 +43,8 @@ Object {
"stories": Array [
Object {
"componentName": "filename$capitalization$$test",
"locEnd": 5,
"locStart": 3,
"namedExport": "Test",
"storyId": "filename-capitalization--test",
},
Expand All @@ -59,6 +65,8 @@ Object {
"stories": Array [
Object {
"componentName": "title$$cat",
"locEnd": 9,
"locStart": 7,
"namedExport": "Cat",
"storyId": "title--cat",
},
Expand Down Expand Up @@ -99,11 +107,15 @@ Object {
"stories": Array [
Object {
"componentName": "title$$cat",
"locEnd": 15,
"locStart": 13,
"namedExport": "Cat",
"storyId": "title--cat",
},
Object {
"componentName": "title$$dog",
"locEnd": 27,
"locStart": 25,
"namedExport": "Dog",
"storyId": "title--dog",
},
Expand Down Expand Up @@ -156,6 +168,8 @@ Object {
"stories": Array [
Object {
"componentName": "title$$cat",
"locEnd": 14,
"locStart": 12,
"namedExport": "Cat",
"storyId": "title--cat",
},
Expand Down Expand Up @@ -186,11 +200,15 @@ Object {
"stories": Array [
Object {
"componentName": "animals$$cat",
"locEnd": 5,
"locStart": 3,
"namedExport": "Cat",
"storyId": "animals--cat",
},
Object {
"componentName": "animals$$dog",
"locEnd": 9,
"locStart": 7,
"namedExport": "Dog",
"storyId": "animals--dog",
},
Expand All @@ -215,16 +233,22 @@ Object {
"stories": Array [
Object {
"componentName": "storyname$$doggo",
"locEnd": 8,
"locStart": 3,
"namedExport": "Cat",
"storyId": "storyname--doggo",
},
Object {
"componentName": "storyname$$capital$city",
"locEnd": 15,
"locStart": 13,
"namedExport": "CapitalCity",
"storyId": "storyname--capital-city",
},
Object {
"componentName": "storyname$$champs$élysées",
"locEnd": 19,
"locStart": 17,
"namedExport": "CapitalReplaced",
"storyId": "storyname--champs-élysées",
},
Expand All @@ -246,6 +270,8 @@ Object {
"stories": Array [
Object {
"componentName": "our$animals$$mammals$$cat",
"locEnd": 5,
"locStart": 3,
"namedExport": "Cat",
"storyId": "our-animals--mammals--cat",
},
Expand Down
18 changes: 18 additions & 0 deletions packages/ladle/tests/parse/get-named-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ test("Basic named exports", async () => {
componentName: "file$$my$story",
namedExport: "MyStory",
storyId: "file--my-story",
locEnd: 1,
locStart: 1,
},
{
componentName: "file$$ha",
locEnd: 2,
locStart: 2,
namedExport: "Ha",
storyId: "file--ha",
},
Expand All @@ -56,6 +60,8 @@ test("Basic named exports in typescript", async () => {
componentName: "file$$my$story",
namedExport: "MyStory",
storyId: "file--my-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand Down Expand Up @@ -84,6 +90,8 @@ test("Named export renamed through storyNames", async () => {
componentName: "file$$renamed$story",
namedExport: "MyStory",
storyId: "file--renamed-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand Down Expand Up @@ -123,6 +131,8 @@ test("Named export with added story parameters", async () => {
componentName: "file$$my$story",
namedExport: "MyStory",
storyId: "file--my-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand Down Expand Up @@ -162,6 +172,8 @@ test("Named export with added default parameters", async () => {
componentName: "file$$my$story",
namedExport: "MyStory",
storyId: "file--my-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand Down Expand Up @@ -217,6 +229,8 @@ test("Named export with added default and story parameters", async () => {
componentName: "file$$my$story",
namedExport: "MyStory",
storyId: "file--my-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand All @@ -238,6 +252,8 @@ test("Class Named Export", async () => {
componentName: "file$$my$class$story",
namedExport: "MyClassStory",
storyId: "file--my-class-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand All @@ -259,6 +275,8 @@ test("Function Named Export", async () => {
componentName: "file$$function$story",
namedExport: "FunctionStory",
storyId: "file--function-story",
locEnd: 1,
locStart: 1,
},
],
}),
Expand Down
9 changes: 8 additions & 1 deletion tests/commonjs/tests/hello.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ test("generatres meta.json", async ({ request }) => {
version: 1,
},
stories: {
"hello--world": { name: "World", levels: ["Hello"], parameters: {} },
"hello--world": {
name: "World",
levels: ["Hello"],
locEnd: 3,
locStart: 1,
filePath: "src/hello.stories.tsx",
parameters: {},
},
},
}),
);
Expand Down
9 changes: 8 additions & 1 deletion tests/programmatic/tests/hello.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ test("meta.json has a single story ok", async ({ request }) => {
version: 1,
},
stories: {
"hello--world": { name: "World", levels: ["Hello"], parameters: {} },
"hello--world": {
name: "World",
levels: ["Hello"],
locEnd: 3,
locStart: 1,
filePath: "src/hello.stories.tsx",
parameters: {},
},
},
}),
);
Expand Down
Loading

0 comments on commit df38918

Please sign in to comment.