@suss/contract-storybook 0.38.2 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,7 @@ The default export gives the component under test. The reader walks the declarat
12
12
 
13
13
  The `component` identifier is followed through its import, any renames and any barrel re-exports, to the file that declares it. Each story file is read under the tsconfig nearest to it, so an import through one of its path aliases (`@ui/Chip/Chip`) resolves. The declaring file is written relative to the root an extract of that file's project measures from, with that project's workspace name, so the checker can find the extracted component by its own location. When the import leads to no file, for example because the module is generated or comes from a package that is not installed, no module is recorded, and the checker leaves the story unpaired.
14
14
 
15
- Every other named export whose initializer resolves to an object literal is a story. Its `args` object literal becomes the story's arguments, and each property's value is recorded as the source text you wrote. A shorthand property (`{ disabled }`) records its own name. Storybook gives every story the `args` set on the default export, so those are recorded on each story too, and a story's own arg replaces the meta's arg of the same name.
15
+ Every other named export whose initializer resolves to an object literal is a story. Its `args` object literal becomes the story's arguments, and each property's value is recorded as the source text you wrote. A shorthand property (`{ disabled }`) records its own name, and a method (`onPick() {}`) records its source text. A key the file computes, `[sizeKey]` with `const sizeKey = "size"`, records the name it comes to, and a computed key nothing in the file settles is left out. A value written inside `as`, `satisfies` or parentheses is recorded without them, and so is the `component`, so `component: Chip as never` is `Chip` and is followed to its file like any other. Storybook gives every story the `args` set on the default export, so those are recorded on each story too, and a story's own arg replaces the meta's arg of the same name. When a story or its meta spreads another object and then writes `component`, `args` or `render` again, the one written last is the one read, as it is in the object Storybook builds.
16
16
 
17
17
  ## What it produces
18
18
 
@@ -21,7 +21,7 @@ One `component`-kind summary per named story export:
21
21
  - `identity.name` is `Component.Story`, with `exportPath` set to the story's export name, and a function-call boundary binding with `transport: "in-process"`, `recognition: "react"`, and the component identifier as the export name.
22
22
  - One input per arg. The input's `role` is the arg name, and its shape is a `ref` whose name is the arg's source text, so a reader can see the value that was written.
23
23
  - One default transition whose output is a render of the component. The reader does not evaluate the render, so the rendered tree is left unset.
24
- - `metadata.component.storybook` with the story name, the component name, the component's module (`componentModule`, with `workspace`, `file` and `name`) and the specifier it is imported from (`componentImport`), the args map, `argReaders` when the story or its meta has `decorators` or a `render` function, `renderProps` with the props a render function passes the component as its own attributes or children, and `provenance: "independent"`. `renderProps` is left out when the render function renders the component through another element or spreads anything besides its args into it. A decorator or a render function receives the args before the component does, and the reader does not follow either, so the checker does not judge those args against the component. The provenance is what lets a story act as a check on an inferred component summary, since it was written separately from the component.
24
+ - `metadata.component.storybook` with the story name, the component name, the component's module (`componentModule`, with `workspace`, `file` and `name`) and the specifier it is imported from (`componentImport`), the args map, `argReaders` when the story or its meta has `decorators` or a `render` function, `renderProps` with the props a render function passes the component as its own attributes or children, and `provenance: "independent"`. `renderProps` is left out when the render function renders the component through another element or spreads anything besides its args into it. A spread counts as the args only when its name refers to the render function's first parameter, or to the rest of that parameter's destructure, so spreading a callback's own `args` inside the render function does not. A comment alone between the component's tags passes no `children`. A decorator or a render function receives the args before the component does, and the reader does not follow either, so the checker does not judge those args against the component. The provenance is what lets a story act as a check on an inferred component summary, since it was written separately from the component.
25
25
  - Confidence is `derived` at `medium`. People write stories, and a story is authoritative about what it covers, but stories do not list everything a component does.
26
26
 
27
27
  ## What it does not read
@@ -30,7 +30,7 @@ One `component`-kind summary per named story export:
30
30
  - **`argTypes`.** Control types and option lists per arg could support stricter type checking, but they are not read today.
31
31
  - **What `decorators`, `render` and `parameters` do.** Their presence is recorded, as above. A render function is read only for the attributes and children it gives the component's element, and the other bodies are not read. Decorators set in `.storybook/preview` apply to every story and are not seen at all.
32
32
  - **CSF1 and MDX stories.** CSF3 is the supported format.
33
- - **A `component` that is not a plain identifier**, such as `Icons.Chip`. No module is recorded for it.
33
+ - **A `component` that is a member of an object the run cannot see written out**, such as an object a function builds or one a library's declaration file types. No module is recorded for it. A member of an object a module writes out, `Card.Root` with `export const Card = { Root: CardRoot }`, is followed to where `CardRoot` is declared, through type arguments such as `Slider.Root<number>` too, and a member of a namespace import, `Icons.Chip` with `import * as Icons from "./icons"`, is followed like a plain name.
34
34
  - **Arg values as structured shapes.** An arg's value stays as source text inside a `ref` shape, and is not parsed into a `TypeShape`.
35
35
 
36
36
  ## Worked example
@@ -70,7 +70,7 @@ const summaries = generateSummariesFromStories(
70
70
 
71
71
  ## Where it fits in suss
72
72
 
73
- The package depends on `@suss/behavioral-ir` for the IR types it produces and on `@suss/adapter-typescript` for export resolution, with `ts-morph` as a peer dependency. It is the only contract reader that parses TypeScript, because CSF is TypeScript. It still reads a declared artifact, and does not infer behavior from a component's implementation.
73
+ The package depends on `@suss/behavioral-ir` for the IR types it produces, on `@suss/adapter-typescript` for a story file's module ops over its facts, and on `@suss/recognize` for `discoverEntryObject`, which reads the meta and the stories through those ops, with `ts-morph` as a peer dependency. A render function's JSX and the component's declaration are read through the value ops too, so the reader builds the program for each tsconfig and reads no syntax itself. It is the only contract reader that parses TypeScript, because CSF is TypeScript. It still reads a declared artifact, and does not infer behavior from a component's implementation.
74
74
 
75
75
  ## More
76
76
 
package/dist/index.cjs CHANGED
@@ -38,6 +38,13 @@ var import_ts_morph = require("ts-morph");
38
38
  var import_adapter_typescript = require("@suss/adapter-typescript");
39
39
  var import_behavioral_ir = require("@suss/behavioral-ir");
40
40
  var import_files = require("@suss/behavioral-ir/files");
41
+ var import_recognize = require("@suss/recognize");
42
+ var STORIES = {
43
+ type: "entryObject",
44
+ entry: "default",
45
+ target: "component",
46
+ inputs: "args"
47
+ };
41
48
  function generateSummariesFromStories(filePaths, options = {}) {
42
49
  const componentModules = options.componentModules ?? true;
43
50
  const byFile = /* @__PURE__ */ new Map();
@@ -51,14 +58,15 @@ function generateSummariesFromStories(filePaths, options = {}) {
51
58
  storyFile,
52
59
  options.projectRoot ?? (0, import_files.defaultProjectRoot)(storyFile)
53
60
  );
54
- const meta = extractMeta(sf, resolution, componentModules);
55
- if (meta === null) {
61
+ const found = (0, import_recognize.discoverEntryObject)(STORIES, (0, import_adapter_typescript.moduleOpsFor)(sf, resolution));
62
+ if (found === null) {
56
63
  continue;
57
64
  }
65
+ const meta = metaOf(found, componentModules);
58
66
  byFile.set(
59
67
  sf.getFilePath(),
60
- extractStories(sf, resolution, meta).map(
61
- (story) => buildSummary(story, meta, relPath)
68
+ found.members.map(
69
+ (member) => buildSummary(storyOf(member, meta), meta, relPath)
62
70
  )
63
71
  );
64
72
  }
@@ -105,170 +113,73 @@ function projectFor(tsconfig) {
105
113
  });
106
114
  }
107
115
  var ARG_READERS = ["decorators", "render"];
108
- function argReadersOf(storyOrMeta, resolution) {
109
- const names = new Set(
110
- (0, import_adapter_typescript.propertiesOf)(storyOrMeta, resolution).map(
111
- (property) => (0, import_adapter_typescript.propertyNameOf)(property)
112
- )
116
+ function argReadersOf(storyOrMeta) {
117
+ const keys = new Set(
118
+ storyOrMeta.entries("nothing").map((entry) => entry.key)
113
119
  );
114
- return ARG_READERS.filter((name) => names.has(name));
120
+ return ARG_READERS.filter((name) => keys.has(name));
115
121
  }
116
- function renderPropsOf(storyOrMeta, componentName, resolution) {
117
- const property = (0, import_adapter_typescript.propertiesOf)(storyOrMeta, resolution).find(
118
- (candidate) => (0, import_adapter_typescript.propertyNameOf)(candidate) === "render"
119
- );
120
- const render = property === void 0 ? null : (0, import_adapter_typescript.propertyFunctionOf)(property, resolution);
121
- if (render === null) {
122
+ function renderPropsOf(storyOrMeta, componentName) {
123
+ const render = storyOrMeta.property("render")?.function?.() ?? null;
124
+ const elements = render?.elements?.(componentName) ?? [];
125
+ if (render === null || elements.length === 0) {
122
126
  return null;
123
127
  }
124
- const argNames = argNamesOf(render.getParameters()[0]);
125
- const elements = [
126
- ...render.getDescendantsOfKind(import_ts_morph.SyntaxKind.JsxOpeningElement),
127
- ...render.getDescendantsOfKind(import_ts_morph.SyntaxKind.JsxSelfClosingElement)
128
- ].filter((element) => element.getTagNameNode().getText() === componentName);
129
- if (elements.length === 0) {
128
+ const spreadsOther = elements.some(
129
+ (element) => element.spreads().some((spread) => spread.parameterAt?.(render) !== 0)
130
+ );
131
+ if (spreadsOther) {
130
132
  return null;
131
133
  }
132
- const passed = /* @__PURE__ */ new Set();
133
- if (elements.some(hasJsxChildren)) {
134
+ const passed = new Set(elements.flatMap((element) => element.props()));
135
+ if (elements.some((element) => element.hasChildren())) {
134
136
  passed.add("children");
135
137
  }
136
- for (const attribute of elements.flatMap((e) => e.getAttributes())) {
137
- if (import_ts_morph.Node.isJsxAttribute(attribute)) {
138
- passed.add(attribute.getNameNode().getText());
139
- continue;
140
- }
141
- if (!argNames.has(attribute.getExpression().getText())) {
142
- return null;
143
- }
144
- }
145
138
  return [...passed].sort();
146
139
  }
147
- function hasJsxChildren(element) {
148
- const parent = element.getParent();
149
- if (!import_ts_morph.Node.isJsxOpeningElement(element) || !import_ts_morph.Node.isJsxElement(parent)) {
150
- return false;
151
- }
152
- return parent.getJsxChildren().some(
153
- (child) => !(import_ts_morph.Node.isJsxText(child) && child.containsOnlyTriviaWhiteSpaces())
154
- );
155
- }
156
- function argNamesOf(parameter) {
157
- const names = /* @__PURE__ */ new Set();
158
- const nameNode = parameter?.getNameNode();
159
- if (nameNode === void 0) {
160
- return names;
161
- }
162
- if (import_ts_morph.Node.isIdentifier(nameNode)) {
163
- names.add(nameNode.getText());
164
- return names;
165
- }
166
- const elements = import_ts_morph.Node.isObjectBindingPattern(nameNode) ? nameNode.getDescendantsOfKind(import_ts_morph.SyntaxKind.BindingElement).filter((element) => element.getParent() === nameNode) : [];
167
- for (const element of elements) {
168
- if (element.getDotDotDotToken() !== void 0) {
169
- names.add(element.getName());
170
- }
171
- }
172
- return names;
173
- }
174
- function extractMeta(sf, resolution, componentModules) {
175
- for (const exported of resolution.exportsOf(sf).get("default") ?? []) {
176
- const meta = objectBehind(exported, resolution);
177
- if (meta === null) {
178
- continue;
179
- }
180
- const component = (0, import_adapter_typescript.propertyOf)(meta, "component", resolution);
181
- if (component !== null) {
182
- return {
183
- componentName: component.getText(),
184
- componentModule: componentModules ? declaredModuleOf(component) : void 0,
185
- componentImport: importSpecifierOf(component),
186
- args: argsOf(meta, resolution),
187
- argReaders: argReadersOf(meta, resolution),
188
- renderProps: renderPropsOf(meta, component.getText(), resolution)
189
- };
190
- }
191
- }
192
- return null;
140
+ function metaOf(found, componentModules) {
141
+ const { entry: meta, target: component } = found;
142
+ const componentName = textOf(component);
143
+ return {
144
+ componentName,
145
+ componentModule: componentModules ? declaredModuleOf(component, componentName) : void 0,
146
+ componentImport: component.importedFrom?.() ?? void 0,
147
+ argReaders: argReadersOf(meta),
148
+ renderProps: renderPropsOf(meta, componentName)
149
+ };
193
150
  }
194
- function declaredModuleOf(component) {
195
- if (!import_ts_morph.Node.isIdentifier(component)) {
151
+ function declaredModuleOf(component, spelling) {
152
+ const declared = component.declaration?.() ?? null;
153
+ if (declared === null) {
196
154
  return void 0;
197
155
  }
198
- const declaration = componentDeclarationOf(component);
199
- if (declaration === void 0) {
200
- return void 0;
201
- }
202
- const place = (0, import_adapter_typescript.projectPlaceOf)(declaration.getSourceFile().getFilePath());
156
+ const place = (0, import_adapter_typescript.projectPlaceOf)(declared.file);
203
157
  return {
204
158
  ...place.workspace === null ? {} : { workspace: place.workspace },
205
159
  file: place.file,
206
- name: declaredNameOf(declaration) ?? component.getText()
160
+ name: declared.name ?? spelling
207
161
  };
208
162
  }
209
- function componentDeclarationOf(component) {
210
- return (0, import_adapter_typescript.declarationsBehind)((0, import_adapter_typescript.symbolBehind)(component)).find(
211
- (declaration) => !isImportBinding(declaration)
212
- );
213
- }
214
- function isImportBinding(node) {
215
- return import_ts_morph.Node.isImportSpecifier(node) || import_ts_morph.Node.isImportClause(node) || import_ts_morph.Node.isNamespaceImport(node) || import_ts_morph.Node.isImportEqualsDeclaration(node);
216
- }
217
- function declaredNameOf(declaration) {
218
- if (import_ts_morph.Node.isVariableDeclaration(declaration) || import_ts_morph.Node.isFunctionDeclaration(declaration) || import_ts_morph.Node.isClassDeclaration(declaration)) {
219
- return declaration.getName();
220
- }
221
- return void 0;
222
- }
223
- function importSpecifierOf(component) {
224
- if (!import_ts_morph.Node.isIdentifier(component)) {
225
- return void 0;
226
- }
227
- return (0, import_adapter_typescript.specifierImportedAs)(component.getSourceFile(), component.getText()) ?? void 0;
163
+ function textOf(value) {
164
+ return value.source?.() ?? "";
228
165
  }
229
- function objectBehind(value, resolution) {
230
- const resolved = resolution.resolveObject(value);
231
- return resolved !== null && import_ts_morph.Node.isObjectLiteralExpression(resolved) ? resolved : null;
232
- }
233
- function extractStories(sf, resolution, meta) {
234
- const results = [];
235
- for (const [name, decls] of (0, import_adapter_typescript.exportedDeclarationsOf)(sf, resolution)) {
236
- if (name === "default") {
237
- continue;
238
- }
239
- for (const decl of decls) {
240
- const story = objectBehind(decl, resolution);
241
- if (story === null) {
242
- continue;
243
- }
244
- const ownReaders = argReadersOf(story, resolution);
245
- results.push({
246
- name,
247
- args: { ...meta.args, ...argsOf(story, resolution) },
248
- argReaders: ARG_READERS.filter(
249
- (reader) => meta.argReaders.includes(reader) || ownReaders.includes(reader)
250
- ),
251
- renderProps: ownReaders.includes("render") ? renderPropsOf(story, meta.componentName, resolution) : meta.renderProps,
252
- line: decl.getStartLineNumber()
253
- });
254
- }
255
- }
256
- return results;
166
+ function storyOf(member, meta) {
167
+ const ownReaders = argReadersOf(member.value);
168
+ return {
169
+ name: member.name,
170
+ args: argsOf(member.inputs),
171
+ argReaders: ARG_READERS.filter(
172
+ (reader) => meta.argReaders.includes(reader) || ownReaders.includes(reader)
173
+ ),
174
+ renderProps: ownReaders.includes("render") ? renderPropsOf(member.value, meta.componentName) : meta.renderProps,
175
+ line: member.line ?? 0
176
+ };
257
177
  }
258
- function argsOf(storyOrMeta, resolution) {
178
+ function argsOf(inputs) {
259
179
  const args = {};
260
- const written = (0, import_adapter_typescript.propertyOf)(storyOrMeta, "args", resolution);
261
- const object = written === null ? null : (0, import_adapter_typescript.objectLiteralOf)(written, resolution);
262
- if (object === null) {
263
- return args;
264
- }
265
- for (const property of (0, import_adapter_typescript.propertiesOf)(object, resolution)) {
266
- const name = (0, import_adapter_typescript.propertyNameOf)(property);
267
- const value = (0, import_adapter_typescript.propertyValueOf)(property);
268
- if (name === null || value === null) {
269
- continue;
270
- }
271
- args[name] = (0, import_adapter_typescript.stringValueOf)(value, resolution) ?? value.getText();
180
+ for (const { key, value } of inputs) {
181
+ const settled = value.evaluated?.() ?? null;
182
+ args[key] = typeof settled === "string" ? settled : textOf(value);
272
183
  }
273
184
  return args;
274
185
  }
package/dist/index.js CHANGED
@@ -1,28 +1,21 @@
1
1
  // src/index.ts
2
2
  import path from "path";
3
+ import { Project } from "ts-morph";
3
4
  import {
4
- Node as N,
5
- Project,
6
- SyntaxKind
7
- } from "ts-morph";
8
- import {
9
- declarationsBehind,
10
- exportedDeclarationsOf,
11
5
  findNearestTsconfig,
12
- objectLiteralOf,
6
+ moduleOpsFor,
13
7
  projectPlaceOf,
14
- propertiesOf,
15
- propertyFunctionOf,
16
- propertyNameOf,
17
- propertyOf,
18
- propertyValueOf,
19
- ResolutionStore,
20
- specifierImportedAs,
21
- stringValueOf,
22
- symbolBehind
8
+ ResolutionStore
23
9
  } from "@suss/adapter-typescript";
24
10
  import { functionCallBinding } from "@suss/behavioral-ir";
25
11
  import { defaultProjectRoot, pathInProject } from "@suss/behavioral-ir/files";
12
+ import { discoverEntryObject } from "@suss/recognize";
13
+ var STORIES = {
14
+ type: "entryObject",
15
+ entry: "default",
16
+ target: "component",
17
+ inputs: "args"
18
+ };
26
19
  function generateSummariesFromStories(filePaths, options = {}) {
27
20
  const componentModules = options.componentModules ?? true;
28
21
  const byFile = /* @__PURE__ */ new Map();
@@ -36,14 +29,15 @@ function generateSummariesFromStories(filePaths, options = {}) {
36
29
  storyFile,
37
30
  options.projectRoot ?? defaultProjectRoot(storyFile)
38
31
  );
39
- const meta = extractMeta(sf, resolution, componentModules);
40
- if (meta === null) {
32
+ const found = discoverEntryObject(STORIES, moduleOpsFor(sf, resolution));
33
+ if (found === null) {
41
34
  continue;
42
35
  }
36
+ const meta = metaOf(found, componentModules);
43
37
  byFile.set(
44
38
  sf.getFilePath(),
45
- extractStories(sf, resolution, meta).map(
46
- (story) => buildSummary(story, meta, relPath)
39
+ found.members.map(
40
+ (member) => buildSummary(storyOf(member, meta), meta, relPath)
47
41
  )
48
42
  );
49
43
  }
@@ -90,170 +84,73 @@ function projectFor(tsconfig) {
90
84
  });
91
85
  }
92
86
  var ARG_READERS = ["decorators", "render"];
93
- function argReadersOf(storyOrMeta, resolution) {
94
- const names = new Set(
95
- propertiesOf(storyOrMeta, resolution).map(
96
- (property) => propertyNameOf(property)
97
- )
87
+ function argReadersOf(storyOrMeta) {
88
+ const keys = new Set(
89
+ storyOrMeta.entries("nothing").map((entry) => entry.key)
98
90
  );
99
- return ARG_READERS.filter((name) => names.has(name));
91
+ return ARG_READERS.filter((name) => keys.has(name));
100
92
  }
101
- function renderPropsOf(storyOrMeta, componentName, resolution) {
102
- const property = propertiesOf(storyOrMeta, resolution).find(
103
- (candidate) => propertyNameOf(candidate) === "render"
104
- );
105
- const render = property === void 0 ? null : propertyFunctionOf(property, resolution);
106
- if (render === null) {
93
+ function renderPropsOf(storyOrMeta, componentName) {
94
+ const render = storyOrMeta.property("render")?.function?.() ?? null;
95
+ const elements = render?.elements?.(componentName) ?? [];
96
+ if (render === null || elements.length === 0) {
107
97
  return null;
108
98
  }
109
- const argNames = argNamesOf(render.getParameters()[0]);
110
- const elements = [
111
- ...render.getDescendantsOfKind(SyntaxKind.JsxOpeningElement),
112
- ...render.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement)
113
- ].filter((element) => element.getTagNameNode().getText() === componentName);
114
- if (elements.length === 0) {
99
+ const spreadsOther = elements.some(
100
+ (element) => element.spreads().some((spread) => spread.parameterAt?.(render) !== 0)
101
+ );
102
+ if (spreadsOther) {
115
103
  return null;
116
104
  }
117
- const passed = /* @__PURE__ */ new Set();
118
- if (elements.some(hasJsxChildren)) {
105
+ const passed = new Set(elements.flatMap((element) => element.props()));
106
+ if (elements.some((element) => element.hasChildren())) {
119
107
  passed.add("children");
120
108
  }
121
- for (const attribute of elements.flatMap((e) => e.getAttributes())) {
122
- if (N.isJsxAttribute(attribute)) {
123
- passed.add(attribute.getNameNode().getText());
124
- continue;
125
- }
126
- if (!argNames.has(attribute.getExpression().getText())) {
127
- return null;
128
- }
129
- }
130
109
  return [...passed].sort();
131
110
  }
132
- function hasJsxChildren(element) {
133
- const parent = element.getParent();
134
- if (!N.isJsxOpeningElement(element) || !N.isJsxElement(parent)) {
135
- return false;
136
- }
137
- return parent.getJsxChildren().some(
138
- (child) => !(N.isJsxText(child) && child.containsOnlyTriviaWhiteSpaces())
139
- );
140
- }
141
- function argNamesOf(parameter) {
142
- const names = /* @__PURE__ */ new Set();
143
- const nameNode = parameter?.getNameNode();
144
- if (nameNode === void 0) {
145
- return names;
146
- }
147
- if (N.isIdentifier(nameNode)) {
148
- names.add(nameNode.getText());
149
- return names;
150
- }
151
- const elements = N.isObjectBindingPattern(nameNode) ? nameNode.getDescendantsOfKind(SyntaxKind.BindingElement).filter((element) => element.getParent() === nameNode) : [];
152
- for (const element of elements) {
153
- if (element.getDotDotDotToken() !== void 0) {
154
- names.add(element.getName());
155
- }
156
- }
157
- return names;
158
- }
159
- function extractMeta(sf, resolution, componentModules) {
160
- for (const exported of resolution.exportsOf(sf).get("default") ?? []) {
161
- const meta = objectBehind(exported, resolution);
162
- if (meta === null) {
163
- continue;
164
- }
165
- const component = propertyOf(meta, "component", resolution);
166
- if (component !== null) {
167
- return {
168
- componentName: component.getText(),
169
- componentModule: componentModules ? declaredModuleOf(component) : void 0,
170
- componentImport: importSpecifierOf(component),
171
- args: argsOf(meta, resolution),
172
- argReaders: argReadersOf(meta, resolution),
173
- renderProps: renderPropsOf(meta, component.getText(), resolution)
174
- };
175
- }
176
- }
177
- return null;
111
+ function metaOf(found, componentModules) {
112
+ const { entry: meta, target: component } = found;
113
+ const componentName = textOf(component);
114
+ return {
115
+ componentName,
116
+ componentModule: componentModules ? declaredModuleOf(component, componentName) : void 0,
117
+ componentImport: component.importedFrom?.() ?? void 0,
118
+ argReaders: argReadersOf(meta),
119
+ renderProps: renderPropsOf(meta, componentName)
120
+ };
178
121
  }
179
- function declaredModuleOf(component) {
180
- if (!N.isIdentifier(component)) {
122
+ function declaredModuleOf(component, spelling) {
123
+ const declared = component.declaration?.() ?? null;
124
+ if (declared === null) {
181
125
  return void 0;
182
126
  }
183
- const declaration = componentDeclarationOf(component);
184
- if (declaration === void 0) {
185
- return void 0;
186
- }
187
- const place = projectPlaceOf(declaration.getSourceFile().getFilePath());
127
+ const place = projectPlaceOf(declared.file);
188
128
  return {
189
129
  ...place.workspace === null ? {} : { workspace: place.workspace },
190
130
  file: place.file,
191
- name: declaredNameOf(declaration) ?? component.getText()
131
+ name: declared.name ?? spelling
192
132
  };
193
133
  }
194
- function componentDeclarationOf(component) {
195
- return declarationsBehind(symbolBehind(component)).find(
196
- (declaration) => !isImportBinding(declaration)
197
- );
198
- }
199
- function isImportBinding(node) {
200
- return N.isImportSpecifier(node) || N.isImportClause(node) || N.isNamespaceImport(node) || N.isImportEqualsDeclaration(node);
201
- }
202
- function declaredNameOf(declaration) {
203
- if (N.isVariableDeclaration(declaration) || N.isFunctionDeclaration(declaration) || N.isClassDeclaration(declaration)) {
204
- return declaration.getName();
205
- }
206
- return void 0;
207
- }
208
- function importSpecifierOf(component) {
209
- if (!N.isIdentifier(component)) {
210
- return void 0;
211
- }
212
- return specifierImportedAs(component.getSourceFile(), component.getText()) ?? void 0;
213
- }
214
- function objectBehind(value, resolution) {
215
- const resolved = resolution.resolveObject(value);
216
- return resolved !== null && N.isObjectLiteralExpression(resolved) ? resolved : null;
134
+ function textOf(value) {
135
+ return value.source?.() ?? "";
217
136
  }
218
- function extractStories(sf, resolution, meta) {
219
- const results = [];
220
- for (const [name, decls] of exportedDeclarationsOf(sf, resolution)) {
221
- if (name === "default") {
222
- continue;
223
- }
224
- for (const decl of decls) {
225
- const story = objectBehind(decl, resolution);
226
- if (story === null) {
227
- continue;
228
- }
229
- const ownReaders = argReadersOf(story, resolution);
230
- results.push({
231
- name,
232
- args: { ...meta.args, ...argsOf(story, resolution) },
233
- argReaders: ARG_READERS.filter(
234
- (reader) => meta.argReaders.includes(reader) || ownReaders.includes(reader)
235
- ),
236
- renderProps: ownReaders.includes("render") ? renderPropsOf(story, meta.componentName, resolution) : meta.renderProps,
237
- line: decl.getStartLineNumber()
238
- });
239
- }
240
- }
241
- return results;
137
+ function storyOf(member, meta) {
138
+ const ownReaders = argReadersOf(member.value);
139
+ return {
140
+ name: member.name,
141
+ args: argsOf(member.inputs),
142
+ argReaders: ARG_READERS.filter(
143
+ (reader) => meta.argReaders.includes(reader) || ownReaders.includes(reader)
144
+ ),
145
+ renderProps: ownReaders.includes("render") ? renderPropsOf(member.value, meta.componentName) : meta.renderProps,
146
+ line: member.line ?? 0
147
+ };
242
148
  }
243
- function argsOf(storyOrMeta, resolution) {
149
+ function argsOf(inputs) {
244
150
  const args = {};
245
- const written = propertyOf(storyOrMeta, "args", resolution);
246
- const object = written === null ? null : objectLiteralOf(written, resolution);
247
- if (object === null) {
248
- return args;
249
- }
250
- for (const property of propertiesOf(object, resolution)) {
251
- const name = propertyNameOf(property);
252
- const value = propertyValueOf(property);
253
- if (name === null || value === null) {
254
- continue;
255
- }
256
- args[name] = stringValueOf(value, resolution) ?? value.getText();
151
+ for (const { key, value } of inputs) {
152
+ const settled = value.evaluated?.() ?? null;
153
+ args[key] = typeof settled === "string" ? settled : textOf(value);
257
154
  }
258
155
  return args;
259
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suss/contract-storybook",
3
- "version": "0.38.2",
3
+ "version": "0.40.0",
4
4
  "type": "module",
5
5
  "description": "Turn Storybook CSF stories into a suss contract and check the props they pass against the React components that receive them.",
6
6
  "keywords": [
@@ -47,8 +47,9 @@
47
47
  "typecheck": "tsc --noEmit"
48
48
  },
49
49
  "dependencies": {
50
- "@suss/adapter-typescript": "0.38.2",
51
- "@suss/behavioral-ir": "0.38.2"
50
+ "@suss/adapter-typescript": "0.40.0",
51
+ "@suss/behavioral-ir": "0.40.0",
52
+ "@suss/recognize": "0.40.0"
52
53
  },
53
54
  "devDependencies": {
54
55
  "tsup": "*",