Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 5065a04

Browse files
authored
feature file processor to support csf3 format (#37)
1 parent 191f0cb commit 5065a04

File tree

5 files changed

+840
-133
lines changed

5 files changed

+840
-133
lines changed

__tests__/fixtures/feature-storybook-ref/b.stories.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@
33
import { storiesOf } from "@storybook/react";
44
import * as React from "react";
55

6-
storiesOf("b", module).add("Foo", () => null);
6+
export default {
7+
title: 'b',
8+
component: null,
9+
};
10+
11+
export const Foo = {
12+
render: () => null,
13+
}

package-lock.json

Lines changed: 43 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@canva/dependency-tree",
3-
"version": "3.2.1",
3+
"version": "3.3.0",
44
"description": "Calculates a dependency tree for set of files",
55
"main": "dist/index.js",
66
"author": "Canva Pty Ltd",
@@ -36,7 +36,7 @@
3636
"enhanced-resolve": "^4.1.0",
3737
"esquery": "^1.3.1",
3838
"fast-glob": "^3.0.4",
39-
"gherkin": "^7.0.3",
39+
"gherkin": "^8.2.1",
4040
"lodash.escaperegexp": "^4.1.2",
4141
"lodash.memoize": "^4.1.2",
4242
"memoize-fs": "^2.2.0",

src/processors/feature.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const info = logger.extend('info');
1616
const warn = logger.extend('warn');
1717

1818
const STORIES_IMPORT = 'storiesOf';
19+
const CSF3_EXPORT_TITLE_FIELD = 'title';
1920
const STORIES_PACKAGE = '@storybook/react';
2021
const STORIES_FILE_RE = new RegExp(
2122
`([^${escapeRegExp(path.sep)}]+)\\.stories\\.tsx?$`,
@@ -107,6 +108,28 @@ export class FeatureFileProcessor extends TypeScriptFileProcessor {
107108
return node.kind === ts.SyntaxKind.CallExpression;
108109
}
109110

111+
private static isExportAssignment(
112+
node: ts.Node,
113+
): node is ts.ExportAssignment {
114+
return node.kind === ts.SyntaxKind.ExportAssignment;
115+
}
116+
117+
private static isObjectLiteralExpression(
118+
node: ts.Node,
119+
): node is ts.ObjectLiteralExpression {
120+
return node.kind === ts.SyntaxKind.ObjectLiteralExpression;
121+
}
122+
123+
private static isPropertyAssignment(
124+
node: ts.Node,
125+
): node is ts.PropertyAssignment {
126+
return node.kind === ts.SyntaxKind.PropertyAssignment;
127+
}
128+
129+
private static isIdentifier(node: ts.Node): node is ts.Identifier {
130+
return node.kind === ts.SyntaxKind.Identifier;
131+
}
132+
110133
private static walkStories(
111134
sourceFile: ts.SourceFile,
112135
callback: (storybook: Storybook) => void,
@@ -129,6 +152,27 @@ export class FeatureFileProcessor extends TypeScriptFileProcessor {
129152
return;
130153
}
131154

155+
if (FeatureFileProcessor.isExportAssignment(node)) {
156+
const { expression } = node;
157+
if (
158+
FeatureFileProcessor.isObjectLiteralExpression(expression) &&
159+
expression.properties.length > 0
160+
) {
161+
const property = expression.properties[0];
162+
if (FeatureFileProcessor.isPropertyAssignment(property)) {
163+
const { name, initializer } = property;
164+
if (
165+
FeatureFileProcessor.isIdentifier(name) &&
166+
FeatureFileProcessor.isStringLiteral(initializer) &&
167+
name.escapedText === CSF3_EXPORT_TITLE_FIELD
168+
) {
169+
callback(initializer.text);
170+
return;
171+
}
172+
}
173+
}
174+
}
175+
132176
ts.forEachChild(node, walkTree);
133177
};
134178

0 commit comments

Comments
 (0)