Skip to content

Commit 6d53f85

Browse files
authored
Merge pull request #17 from storybookjs/shilman/extract-tags
Add tags extraction
2 parents 7638e5a + a2e38ab commit 6d53f85

File tree

4 files changed

+102
-43
lines changed

4 files changed

+102
-43
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@types/babel__traverse": "^7.20.4",
4444
"@types/lodash": "^4.14.202",
4545
"@types/node": "^18.0.0",
46-
"auto": "^11.0.4",
46+
"auto": "^11.1.2",
4747
"concurrently": "^8.2.2",
4848
"estree-to-babel": "^9.0.0",
4949
"hast-util-to-estree": "^3.1.0",

src/analyze.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('analyze', () => {
5050
{
5151
"imports": [],
5252
"isTemplate": false,
53+
"metaTags": undefined,
5354
"name": undefined,
5455
"of": undefined,
5556
"title": "foobar",
@@ -80,6 +81,7 @@ describe('analyze', () => {
8081
{
8182
"imports": [],
8283
"isTemplate": false,
84+
"metaTags": undefined,
8385
"name": "foobar",
8486
"of": undefined,
8587
"title": undefined,
@@ -113,6 +115,7 @@ describe('analyze', () => {
113115
"./Button.stories",
114116
],
115117
"isTemplate": false,
118+
"metaTags": undefined,
116119
"name": undefined,
117120
"of": "./Button.stories",
118121
"title": undefined,
@@ -152,6 +155,7 @@ describe('analyze', () => {
152155
"./Button.stories",
153156
],
154157
"isTemplate": false,
158+
"metaTags": undefined,
155159
"name": undefined,
156160
"of": "./Button.stories",
157161
"title": undefined,
@@ -179,6 +183,7 @@ describe('analyze', () => {
179183
"../src/A.stories",
180184
],
181185
"isTemplate": false,
186+
"metaTags": undefined,
182187
"name": "Story One",
183188
"of": "../src/A.stories",
184189
"title": undefined,
@@ -207,6 +212,7 @@ describe('analyze', () => {
207212
{
208213
"imports": [],
209214
"isTemplate": true,
215+
"metaTags": undefined,
210216
"name": undefined,
211217
"of": undefined,
212218
"title": undefined,
@@ -225,6 +231,7 @@ describe('analyze', () => {
225231
{
226232
"imports": [],
227233
"isTemplate": true,
234+
"metaTags": undefined,
228235
"name": undefined,
229236
"of": undefined,
230237
"title": undefined,
@@ -240,6 +247,7 @@ describe('analyze', () => {
240247
{
241248
"imports": [],
242249
"isTemplate": false,
250+
"metaTags": undefined,
243251
"name": undefined,
244252
"of": undefined,
245253
"title": undefined,
@@ -275,6 +283,7 @@ describe('analyze', () => {
275283
{
276284
"imports": [],
277285
"isTemplate": false,
286+
"metaTags": undefined,
278287
"name": undefined,
279288
"of": undefined,
280289
"title": undefined,
@@ -293,6 +302,7 @@ describe('analyze', () => {
293302
"./Button.stories",
294303
],
295304
"isTemplate": false,
305+
"metaTags": undefined,
296306
"name": undefined,
297307
"of": undefined,
298308
"title": undefined,
@@ -337,6 +347,32 @@ describe('analyze', () => {
337347
"./Button.stories",
338348
],
339349
"isTemplate": false,
350+
"metaTags": undefined,
351+
"name": undefined,
352+
"of": "./Button.stories",
353+
"title": undefined,
354+
}
355+
`);
356+
});
357+
it('Meta tags', () => {
358+
const input = dedent`
359+
import meta, { Basic } from './Button.stories';
360+
361+
<Meta of={meta} tags={['a', 'b', 'c']} />
362+
363+
{/* whatever */}
364+
`;
365+
expect(analyze(input)).toMatchInlineSnapshot(`
366+
{
367+
"imports": [
368+
"./Button.stories",
369+
],
370+
"isTemplate": false,
371+
"metaTags": [
372+
"a",
373+
"b",
374+
"c",
375+
],
340376
"name": undefined,
341377
"of": "./Button.stories",
342378
"title": undefined,

src/analyze.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const extractTitle = (root: t.File, varToImport: Record<string, string>) => {
2323
of: string | undefined;
2424
name: string | undefined;
2525
isTemplate: boolean;
26+
metaTags: string[] | undefined;
2627
};
2728
let contents: t.ExpressionStatement;
2829
root.program.body.forEach((child) => {
@@ -93,6 +94,26 @@ const extractTitle = (root: t.File, varToImport: Record<string, string>) => {
9394
);
9495
}
9596
}
97+
const tagsAttr = getAttr(child.openingElement, 'tags');
98+
if (tagsAttr) {
99+
if (t.isJSXExpressionContainer(tagsAttr.value)) {
100+
const tags = tagsAttr.value.expression;
101+
if (t.isArrayExpression(tags)) {
102+
const metaTags = tags.elements.map((tag) => {
103+
if (t.isStringLiteral(tag)) {
104+
return tag.value;
105+
} else {
106+
throw new Error(`Expected string literal tag, received ${tag.type}`);
107+
}
108+
});
109+
result.metaTags = metaTags;
110+
} else {
111+
throw new Error(`Expected array tags, received ${tags.type}`);
112+
}
113+
} else {
114+
throw new Error(`Expected JSX expression tags, received ${tagsAttr.value.type}`);
115+
}
116+
}
96117
}
97118
}
98119
} else if (t.isJSXExpressionContainer(child)) {
@@ -156,11 +177,12 @@ export const plugin = (store: any) => (root: any) => {
156177
const clone = cloneDeep(estree);
157178
const babel = toBabel(clone);
158179
const varToImport = extractImports(babel);
159-
const { title, of, name, isTemplate } = extractTitle(babel, varToImport);
180+
const { title, of, name, isTemplate, metaTags } = extractTitle(babel, varToImport);
160181
store.title = title;
161182
store.of = of;
162183
store.name = name;
163184
store.isTemplate = isTemplate;
185+
store.metaTags = metaTags;
164186
store.imports = Array.from(new Set(Object.values(varToImport)));
165187

166188
return root;
@@ -172,12 +194,13 @@ export const analyze = (code: string) => {
172194
of: undefined,
173195
name: undefined,
174196
isTemplate: false,
197+
metaTags: undefined,
175198
imports: undefined,
176199
toEstree,
177200
} as any;
178201
compileSync(code, {
179202
rehypePlugins: [[plugin, store]],
180203
});
181-
const { title, of, name, isTemplate, imports = [] } = store;
182-
return { title, of, name, isTemplate, imports };
204+
const { title, of, name, isTemplate, metaTags, imports = [] } = store;
205+
return { title, of, name, isTemplate, metaTags, imports };
183206
};

yarn.lock

+39-39
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ __metadata:
1515
languageName: node
1616
linkType: hard
1717

18-
"@auto-it/bot-list@npm:11.0.4":
19-
version: 11.0.4
20-
resolution: "@auto-it/bot-list@npm:11.0.4"
21-
checksum: 00e830ea64cf393404040e970d22e136e61eb60c4cf0af4e1f8a4c7f323b04a84bf2a9d54cb9d733c0268aee76e8dd37dc7403d6e8f757c41ebbb1e6d7249c69
18+
"@auto-it/bot-list@npm:11.1.2":
19+
version: 11.1.2
20+
resolution: "@auto-it/bot-list@npm:11.1.2"
21+
checksum: a3a9af6f96de344a832bde79f7c0c221bdcab4f66d4f59562a748c7b6079ba02dc96ea5f51c25e9e86de8a4e1bdecaf2262519bd2974cb5e35531406871ea321
2222
languageName: node
2323
linkType: hard
2424

25-
"@auto-it/core@npm:11.0.4":
26-
version: 11.0.4
27-
resolution: "@auto-it/core@npm:11.0.4"
25+
"@auto-it/core@npm:11.1.2":
26+
version: 11.1.2
27+
resolution: "@auto-it/core@npm:11.1.2"
2828
dependencies:
29-
"@auto-it/bot-list": "npm:11.0.4"
29+
"@auto-it/bot-list": "npm:11.1.2"
3030
"@endemolshinegroup/cosmiconfig-typescript-loader": "npm:^3.0.2"
3131
"@octokit/core": "npm:^3.5.1"
3232
"@octokit/plugin-enterprise-compatibility": "npm:1.3.0"
@@ -71,16 +71,16 @@ __metadata:
7171
peerDependenciesMeta:
7272
"@types/node":
7373
optional: true
74-
checksum: 0685993c3b45b0aad9212ad1942cb8265f118bf18e3f0cff443c23194d511b8e0ebfe04e673fded102d4f975c731035275d568c7c676b05e6f10b56e585cd226
74+
checksum: 38878f80faff8e12b74be27c6b61b028d157a50813c0859415719417f02583f612bd57c5bd01c66b4d83fc265f711502667a75e2f0ff6e3e2812ab6dd8e88e7b
7575
languageName: node
7676
linkType: hard
7777

78-
"@auto-it/npm@npm:11.0.4":
79-
version: 11.0.4
80-
resolution: "@auto-it/npm@npm:11.0.4"
78+
"@auto-it/npm@npm:11.1.2":
79+
version: 11.1.2
80+
resolution: "@auto-it/npm@npm:11.1.2"
8181
dependencies:
82-
"@auto-it/core": "npm:11.0.4"
83-
"@auto-it/package-json-utils": "npm:11.0.4"
82+
"@auto-it/core": "npm:11.1.2"
83+
"@auto-it/package-json-utils": "npm:11.1.2"
8484
await-to-js: "npm:^3.0.0"
8585
endent: "npm:^2.1.0"
8686
env-ci: "npm:^5.0.1"
@@ -93,44 +93,44 @@ __metadata:
9393
typescript-memoize: "npm:^1.0.0-alpha.3"
9494
url-join: "npm:^4.0.0"
9595
user-home: "npm:^2.0.0"
96-
checksum: 7739dfc3ccafabff5ed2af415a223c51e0a8bba6a4fdf18f78fe5e640827f0b6187b5c7d52393b3a3274032b09312bc457cce03d0823d1aaf8587172ebae4e7c
96+
checksum: 4caf456f1ad066afb80bdf0db060ef21e10929df912566994ff0419fa4b2f65fcc810c4e0c060bfe1a54ae98adfbe82722466214760e740f8e89b85eee8f9bb4
9797
languageName: node
9898
linkType: hard
9999

100-
"@auto-it/package-json-utils@npm:11.0.4":
101-
version: 11.0.4
102-
resolution: "@auto-it/package-json-utils@npm:11.0.4"
100+
"@auto-it/package-json-utils@npm:11.1.2":
101+
version: 11.1.2
102+
resolution: "@auto-it/package-json-utils@npm:11.1.2"
103103
dependencies:
104104
parse-author: "npm:^2.0.0"
105105
parse-github-url: "npm:1.0.2"
106-
checksum: 778169580c90d2a29a51121a1861bf51c483ec656a14338219748ebe35a0bfba4531aaf99fcbe776cc41b67822a5e239a8205e99ae5ec81d71b0250d61ec6aaf
106+
checksum: 31c52280ee6c18c8c55c40f32a8926c5ce178eda0285fe802fd682377e34e7e9b800a8bb2d9d5ca5bcea3d46337372b5f1289243945926c46747c5e76666effa
107107
languageName: node
108108
linkType: hard
109109

110-
"@auto-it/released@npm:11.0.4":
111-
version: 11.0.4
112-
resolution: "@auto-it/released@npm:11.0.4"
110+
"@auto-it/released@npm:11.1.2":
111+
version: 11.1.2
112+
resolution: "@auto-it/released@npm:11.1.2"
113113
dependencies:
114-
"@auto-it/bot-list": "npm:11.0.4"
115-
"@auto-it/core": "npm:11.0.4"
114+
"@auto-it/bot-list": "npm:11.1.2"
115+
"@auto-it/core": "npm:11.1.2"
116116
deepmerge: "npm:^4.0.0"
117117
fp-ts: "npm:^2.5.3"
118118
io-ts: "npm:^2.1.2"
119119
tslib: "npm:2.1.0"
120-
checksum: e57ba13fac04280a2d4a255267c8804075ed9217c9fd82deb9f58e6ffd7f5094f1248026b4f212b197dfb42deb9126fc81ca8a1451af4176cb79d80b6b7aacd7
120+
checksum: 8e1dadf4e8e561f0a4a1b2d9b5bee881461b128f6cb7d794e216e78d3c791823951a1c995b12da913bcadc3fdc9750099f3dfdb138195aba28ef86bbd6b82fc3
121121
languageName: node
122122
linkType: hard
123123

124-
"@auto-it/version-file@npm:11.0.4":
125-
version: 11.0.4
126-
resolution: "@auto-it/version-file@npm:11.0.4"
124+
"@auto-it/version-file@npm:11.1.2":
125+
version: 11.1.2
126+
resolution: "@auto-it/version-file@npm:11.1.2"
127127
dependencies:
128-
"@auto-it/core": "npm:11.0.4"
128+
"@auto-it/core": "npm:11.1.2"
129129
fp-ts: "npm:^2.5.3"
130130
io-ts: "npm:^2.1.2"
131131
semver: "npm:^7.0.0"
132132
tslib: "npm:1.10.0"
133-
checksum: fcfb273b567689c0bd71786c7fbbeb8a181aca03eb4f03420a03f74d9770ed9755ab11aa5842c2691cc0440c2bbd46f7a77363038f79989edcde2eac9dfa162f
133+
checksum: ffdf9814738068db2264960578e4ab0b55a7de36a1d246676ef854901df8bd44e89278b0b221264158c1f9c0b3082e932f1780c03c18b04e4d66ea71afd1232b
134134
languageName: node
135135
linkType: hard
136136

@@ -2272,7 +2272,7 @@ __metadata:
22722272
"@types/babel__traverse": "npm:^7.20.4"
22732273
"@types/lodash": "npm:^4.14.202"
22742274
"@types/node": "npm:^18.0.0"
2275-
auto: "npm:^11.0.4"
2275+
auto: "npm:^11.1.2"
22762276
concurrently: "npm:^8.2.2"
22772277
estree-to-babel: "npm:^9.0.0"
22782278
hast-util-to-estree: "npm:^3.1.0"
@@ -2716,14 +2716,14 @@ __metadata:
27162716
languageName: node
27172717
linkType: hard
27182718

2719-
"auto@npm:^11.0.4":
2720-
version: 11.0.4
2721-
resolution: "auto@npm:11.0.4"
2719+
"auto@npm:^11.1.2":
2720+
version: 11.1.2
2721+
resolution: "auto@npm:11.1.2"
27222722
dependencies:
2723-
"@auto-it/core": "npm:11.0.4"
2724-
"@auto-it/npm": "npm:11.0.4"
2725-
"@auto-it/released": "npm:11.0.4"
2726-
"@auto-it/version-file": "npm:11.0.4"
2723+
"@auto-it/core": "npm:11.1.2"
2724+
"@auto-it/npm": "npm:11.1.2"
2725+
"@auto-it/released": "npm:11.1.2"
2726+
"@auto-it/version-file": "npm:11.1.2"
27272727
await-to-js: "npm:^3.0.0"
27282728
chalk: "npm:^4.0.0"
27292729
command-line-application: "npm:^0.10.1"
@@ -2734,7 +2734,7 @@ __metadata:
27342734
tslib: "npm:2.1.0"
27352735
bin:
27362736
auto: dist/bin/auto.js
2737-
checksum: 4e8d9454f0deea289286c860f073947974aebe176a622151b270e677fa266b6a7e016626ee8612cab084829db14fefcdd214b35c2e9fda6101fc5bf5a3a7d698
2737+
checksum: 8825ef7ce3435a899e92ded4cfba885a466ad216611b3082505943ea2555f92a620904c7da8fb7941104e7263ff2c0917e570b179839ebec3f7cf42a04ce4bf1
27382738
languageName: node
27392739
linkType: hard
27402740

0 commit comments

Comments
 (0)