@suss/contract-storybook 0.38.1 → 0.39.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
 
@@ -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 each export as the value ops over its facts, and on `@suss/recognize` for the types of those ops, with `ts-morph` as a peer dependency. The component's declaration and a render function's JSX are still read off the syntax tree, through the node each value hands out. 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
@@ -51,13 +51,14 @@ function generateSummariesFromStories(filePaths, options = {}) {
51
51
  storyFile,
52
52
  options.projectRoot ?? (0, import_files.defaultProjectRoot)(storyFile)
53
53
  );
54
- const meta = extractMeta(sf, resolution, componentModules);
54
+ const exported = (0, import_adapter_typescript.exportedValuesOf)(sf, resolution);
55
+ const meta = extractMeta(exported, componentModules);
55
56
  if (meta === null) {
56
57
  continue;
57
58
  }
58
59
  byFile.set(
59
60
  sf.getFilePath(),
60
- extractStories(sf, resolution, meta).map(
61
+ extractStories(exported, meta).map(
61
62
  (story) => buildSummary(story, meta, relPath)
62
63
  )
63
64
  );
@@ -105,19 +106,15 @@ function projectFor(tsconfig) {
105
106
  });
106
107
  }
107
108
  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
- )
109
+ function argReadersOf(storyOrMeta) {
110
+ const keys = new Set(
111
+ storyOrMeta.entries("nothing").map((entry) => entry.key)
113
112
  );
114
- return ARG_READERS.filter((name) => names.has(name));
113
+ return ARG_READERS.filter((name) => keys.has(name));
115
114
  }
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);
115
+ function renderPropsOf(storyOrMeta, componentName) {
116
+ const found = storyOrMeta.property("render")?.function?.() ?? null;
117
+ const render = found === null ? null : (0, import_adapter_typescript.toFunctionRoot)(nodeBehind(found));
121
118
  if (render === null) {
122
119
  return null;
123
120
  }
@@ -171,23 +168,22 @@ function argNamesOf(parameter) {
171
168
  }
172
169
  return names;
173
170
  }
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) {
171
+ function extractMeta(exported, componentModules) {
172
+ for (const { value } of exported.get("default") ?? []) {
173
+ const meta = value.object?.() ?? null;
174
+ const component = meta?.property("component") ?? null;
175
+ if (meta === null || component === null) {
178
176
  continue;
179
177
  }
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
- }
178
+ const componentName = textOf(component);
179
+ return {
180
+ componentName,
181
+ componentModule: componentModules ? declaredModuleOf(nodeBehind(component)) : void 0,
182
+ componentImport: component.importedFrom?.() ?? void 0,
183
+ args: argsOf(meta),
184
+ argReaders: argReadersOf(meta),
185
+ renderProps: renderPropsOf(meta, componentName)
186
+ };
191
187
  }
192
188
  return null;
193
189
  }
@@ -220,55 +216,46 @@ function declaredNameOf(declaration) {
220
216
  }
221
217
  return void 0;
222
218
  }
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;
219
+ function nodeBehind(value) {
220
+ return value.ast();
228
221
  }
229
- function objectBehind(value, resolution) {
230
- const resolved = resolution.resolveObject(value);
231
- return resolved !== null && import_ts_morph.Node.isObjectLiteralExpression(resolved) ? resolved : null;
222
+ function textOf(value) {
223
+ return value.source?.() ?? nodeBehind(value).getText();
232
224
  }
233
- function extractStories(sf, resolution, meta) {
225
+ function extractStories(exported, meta) {
234
226
  const results = [];
235
- for (const [name, decls] of (0, import_adapter_typescript.exportedDeclarationsOf)(sf, resolution)) {
227
+ for (const [name, values] of exported) {
236
228
  if (name === "default") {
237
229
  continue;
238
230
  }
239
- for (const decl of decls) {
240
- const story = objectBehind(decl, resolution);
231
+ for (const { value, line } of values) {
232
+ const story = value.object?.() ?? null;
241
233
  if (story === null) {
242
234
  continue;
243
235
  }
244
- const ownReaders = argReadersOf(story, resolution);
236
+ const ownReaders = argReadersOf(story);
245
237
  results.push({
246
238
  name,
247
- args: { ...meta.args, ...argsOf(story, resolution) },
239
+ args: { ...meta.args, ...argsOf(story) },
248
240
  argReaders: ARG_READERS.filter(
249
241
  (reader) => meta.argReaders.includes(reader) || ownReaders.includes(reader)
250
242
  ),
251
- renderProps: ownReaders.includes("render") ? renderPropsOf(story, meta.componentName, resolution) : meta.renderProps,
252
- line: decl.getStartLineNumber()
243
+ renderProps: ownReaders.includes("render") ? renderPropsOf(story, meta.componentName) : meta.renderProps,
244
+ line
253
245
  });
254
246
  }
255
247
  }
256
248
  return results;
257
249
  }
258
- function argsOf(storyOrMeta, resolution) {
250
+ function argsOf(storyOrMeta) {
259
251
  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) {
252
+ const object = storyOrMeta.property("args")?.object?.() ?? null;
253
+ for (const { key, value } of object?.entries("nothing") ?? []) {
254
+ if (key === null) {
269
255
  continue;
270
256
  }
271
- args[name] = (0, import_adapter_typescript.stringValueOf)(value, resolution) ?? value.getText();
257
+ const settled = value.evaluated?.() ?? null;
258
+ args[key] = typeof settled === "string" ? settled : textOf(value);
272
259
  }
273
260
  return args;
274
261
  }
package/dist/index.js CHANGED
@@ -7,19 +7,12 @@ import {
7
7
  } from "ts-morph";
8
8
  import {
9
9
  declarationsBehind,
10
- exportedDeclarationsOf,
10
+ exportedValuesOf,
11
11
  findNearestTsconfig,
12
- objectLiteralOf,
13
12
  projectPlaceOf,
14
- propertiesOf,
15
- propertyFunctionOf,
16
- propertyNameOf,
17
- propertyOf,
18
- propertyValueOf,
19
13
  ResolutionStore,
20
- specifierImportedAs,
21
- stringValueOf,
22
- symbolBehind
14
+ symbolBehind,
15
+ toFunctionRoot
23
16
  } from "@suss/adapter-typescript";
24
17
  import { functionCallBinding } from "@suss/behavioral-ir";
25
18
  import { defaultProjectRoot, pathInProject } from "@suss/behavioral-ir/files";
@@ -36,13 +29,14 @@ function generateSummariesFromStories(filePaths, options = {}) {
36
29
  storyFile,
37
30
  options.projectRoot ?? defaultProjectRoot(storyFile)
38
31
  );
39
- const meta = extractMeta(sf, resolution, componentModules);
32
+ const exported = exportedValuesOf(sf, resolution);
33
+ const meta = extractMeta(exported, componentModules);
40
34
  if (meta === null) {
41
35
  continue;
42
36
  }
43
37
  byFile.set(
44
38
  sf.getFilePath(),
45
- extractStories(sf, resolution, meta).map(
39
+ extractStories(exported, meta).map(
46
40
  (story) => buildSummary(story, meta, relPath)
47
41
  )
48
42
  );
@@ -90,19 +84,15 @@ 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);
93
+ function renderPropsOf(storyOrMeta, componentName) {
94
+ const found = storyOrMeta.property("render")?.function?.() ?? null;
95
+ const render = found === null ? null : toFunctionRoot(nodeBehind(found));
106
96
  if (render === null) {
107
97
  return null;
108
98
  }
@@ -156,23 +146,22 @@ function argNamesOf(parameter) {
156
146
  }
157
147
  return names;
158
148
  }
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) {
149
+ function extractMeta(exported, componentModules) {
150
+ for (const { value } of exported.get("default") ?? []) {
151
+ const meta = value.object?.() ?? null;
152
+ const component = meta?.property("component") ?? null;
153
+ if (meta === null || component === null) {
163
154
  continue;
164
155
  }
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
- }
156
+ const componentName = textOf(component);
157
+ return {
158
+ componentName,
159
+ componentModule: componentModules ? declaredModuleOf(nodeBehind(component)) : void 0,
160
+ componentImport: component.importedFrom?.() ?? void 0,
161
+ args: argsOf(meta),
162
+ argReaders: argReadersOf(meta),
163
+ renderProps: renderPropsOf(meta, componentName)
164
+ };
176
165
  }
177
166
  return null;
178
167
  }
@@ -205,55 +194,46 @@ function declaredNameOf(declaration) {
205
194
  }
206
195
  return void 0;
207
196
  }
208
- function importSpecifierOf(component) {
209
- if (!N.isIdentifier(component)) {
210
- return void 0;
211
- }
212
- return specifierImportedAs(component.getSourceFile(), component.getText()) ?? void 0;
197
+ function nodeBehind(value) {
198
+ return value.ast();
213
199
  }
214
- function objectBehind(value, resolution) {
215
- const resolved = resolution.resolveObject(value);
216
- return resolved !== null && N.isObjectLiteralExpression(resolved) ? resolved : null;
200
+ function textOf(value) {
201
+ return value.source?.() ?? nodeBehind(value).getText();
217
202
  }
218
- function extractStories(sf, resolution, meta) {
203
+ function extractStories(exported, meta) {
219
204
  const results = [];
220
- for (const [name, decls] of exportedDeclarationsOf(sf, resolution)) {
205
+ for (const [name, values] of exported) {
221
206
  if (name === "default") {
222
207
  continue;
223
208
  }
224
- for (const decl of decls) {
225
- const story = objectBehind(decl, resolution);
209
+ for (const { value, line } of values) {
210
+ const story = value.object?.() ?? null;
226
211
  if (story === null) {
227
212
  continue;
228
213
  }
229
- const ownReaders = argReadersOf(story, resolution);
214
+ const ownReaders = argReadersOf(story);
230
215
  results.push({
231
216
  name,
232
- args: { ...meta.args, ...argsOf(story, resolution) },
217
+ args: { ...meta.args, ...argsOf(story) },
233
218
  argReaders: ARG_READERS.filter(
234
219
  (reader) => meta.argReaders.includes(reader) || ownReaders.includes(reader)
235
220
  ),
236
- renderProps: ownReaders.includes("render") ? renderPropsOf(story, meta.componentName, resolution) : meta.renderProps,
237
- line: decl.getStartLineNumber()
221
+ renderProps: ownReaders.includes("render") ? renderPropsOf(story, meta.componentName) : meta.renderProps,
222
+ line
238
223
  });
239
224
  }
240
225
  }
241
226
  return results;
242
227
  }
243
- function argsOf(storyOrMeta, resolution) {
228
+ function argsOf(storyOrMeta) {
244
229
  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) {
230
+ const object = storyOrMeta.property("args")?.object?.() ?? null;
231
+ for (const { key, value } of object?.entries("nothing") ?? []) {
232
+ if (key === null) {
254
233
  continue;
255
234
  }
256
- args[name] = stringValueOf(value, resolution) ?? value.getText();
235
+ const settled = value.evaluated?.() ?? null;
236
+ args[key] = typeof settled === "string" ? settled : textOf(value);
257
237
  }
258
238
  return args;
259
239
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suss/contract-storybook",
3
- "version": "0.38.1",
3
+ "version": "0.39.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.1",
51
- "@suss/behavioral-ir": "0.38.1"
50
+ "@suss/adapter-typescript": "0.39.0",
51
+ "@suss/behavioral-ir": "0.39.0",
52
+ "@suss/recognize": "0.39.0"
52
53
  },
53
54
  "devDependencies": {
54
55
  "tsup": "*",