effect-atom-svelte-devtools 0.2.2 → 0.4.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.
@@ -25,6 +25,19 @@ export const componentName = (file) => path
25
25
  .map((part) => part[0]?.toUpperCase() + part.slice(1))
26
26
  .join("") || "Component";
27
27
  const parser = Parser.extend(tsPlugin());
28
+ // The parser gives every node a type, a start and an end; a type is what tells one from data.
29
+ const isNode = (value) => typeof value === "object" &&
30
+ value !== null &&
31
+ "type" in value &&
32
+ typeof value.type === "string";
33
+ /** The node a field holds, if it holds one. */
34
+ const child = (value) => isNode(value) ? value : undefined;
35
+ /** The nodes a list field holds. */
36
+ const children = (value) => Array.isArray(value) ? value.filter(isNode) : [];
37
+ /** An identifier's name: `todosAtom`. */
38
+ const identifierName = (node) => node?.type === "Identifier" && typeof node.name === "string"
39
+ ? node.name
40
+ : undefined;
28
41
  // Type annotations around the call, as in `Atom.make(0) as Atom.Writable<number>`, are looked
29
42
  // through: only the call is wrapped.
30
43
  const wrappers = new Set([
@@ -34,41 +47,59 @@ const wrappers = new Set([
34
47
  "TSTypeAssertion",
35
48
  "ParenthesizedExpression",
36
49
  ]);
37
- const unwrap = (node) => wrappers.has(node.type) ? unwrap(node.expression) : node;
38
- /** The identifier a callee chain starts from: `Atom` in `Atom.make(...).pipe(...)`. */
39
- const chainStart = (node) => {
50
+ const unwrap = (node) => {
51
+ const inner = wrappers.has(node.type) ? child(node.expression) : undefined;
52
+ return inner === undefined ? node : unwrap(inner);
53
+ };
54
+ /** The node a field holds, with its type annotations looked through. */
55
+ const unwrapped = (value) => {
56
+ const node = child(value);
57
+ return node === undefined ? undefined : unwrap(node);
58
+ };
59
+ /** The next node down a callee chain: a member's object, a call's callee, an annotation's expression. */
60
+ const chainNext = (node) => {
40
61
  if (node.type === "MemberExpression") {
41
- return chainStart(node.object);
62
+ return child(node.object);
42
63
  }
43
64
  if (node.type === "CallExpression") {
44
- return chainStart(node.callee);
65
+ return child(node.callee);
45
66
  }
46
- return wrappers.has(node.type) ? chainStart(node.expression) : node;
67
+ return wrappers.has(node.type) ? child(node.expression) : undefined;
68
+ };
69
+ /** The identifier a callee chain starts from: `Atom` in `Atom.make(...).pipe(...)`. */
70
+ const chainStart = (node) => {
71
+ const next = chainNext(node);
72
+ return next === undefined ? node : chainStart(next);
47
73
  };
48
74
  /**
49
75
  * A call to wrap, or `undefined`. Svelte's runes (`$state(...)`, `$derived.by(...)`) must stay as
50
76
  * they are, and hooks (`useAtomValue(...)`) never return an atom, so both are left be.
51
77
  */
52
78
  const call = (init) => {
53
- if (init === null || init === undefined) {
54
- return undefined;
55
- }
56
- const node = unwrap(init);
57
- if (node.type !== "CallExpression") {
79
+ const node = unwrapped(init);
80
+ if (node?.type !== "CallExpression") {
58
81
  return undefined;
59
82
  }
60
- const start = chainStart(node.callee);
61
- if (start.type === "Identifier" &&
62
- /^(?:\$|use[A-Z])/u.test(String(start.name))) {
83
+ const start = identifierName(chainStart(node));
84
+ if (start !== undefined && /^(?:\$|use[A-Z])/u.test(start)) {
63
85
  return undefined;
64
86
  }
65
87
  return node;
66
88
  };
67
89
  /** Whether the call is `Atom.family(...)` (or a bare `family(...)`), whose members are labelled. */
68
90
  const isFamily = (node) => {
69
- const callee = node.callee;
70
- const property = callee.type === "MemberExpression" ? callee.property : callee;
71
- return property.type === "Identifier" && property.name === "family";
91
+ const callee = child(node.callee);
92
+ const property = callee?.type === "MemberExpression" ? child(callee.property) : callee;
93
+ return identifierName(property) === "family";
94
+ };
95
+ /** The method a member expression takes from `Atom`: `make` in `Atom.make`. */
96
+ const atomMethod = (node) => {
97
+ if (node?.type !== "MemberExpression") {
98
+ return undefined;
99
+ }
100
+ return identifierName(child(node.object)) === "Atom"
101
+ ? identifierName(child(node.property))
102
+ : undefined;
72
103
  };
73
104
  // The arguments `Atom.make` takes as a plain value, rather than a function or an effect.
74
105
  const plainValues = new Set([
@@ -95,58 +126,52 @@ const stateKeeping = new Set([
95
126
  /** Whether `Atom.keepAlive`, `Atom.setIdleTTL(...)` or the like: one of `stateKeeping`. */
96
127
  const keepsState = (argument) => {
97
128
  const node = unwrap(argument);
98
- const callee = node.type === "CallExpression" ? unwrap(node.callee) : node;
99
- return (callee.type === "MemberExpression" &&
100
- !callee.computed &&
101
- callee.object.type === "Identifier" &&
102
- callee.object.name === "Atom" &&
103
- stateKeeping.has(String(callee.property.name)));
129
+ const callee = node.type === "CallExpression" ? unwrapped(node.callee) : node;
130
+ return (callee?.computed !== true && stateKeeping.has(atomMethod(callee) ?? ""));
104
131
  };
105
132
  /**
106
133
  * Whether the call makes a state atom, such as `Atom.make(0)` or `Atom.make([]).pipe(...)`: one
107
134
  * that holds what is written to it, which a hot reload can carry over.
108
135
  */
109
136
  const isState = (node) => {
110
- const callee = node.callee;
111
- if (callee.type !== "MemberExpression") {
137
+ const callee = child(node.callee);
138
+ if (callee?.type !== "MemberExpression") {
112
139
  return false;
113
140
  }
114
- const object = callee.object;
115
- const property = callee.property;
116
- if (property.type === "Identifier" && property.name === "pipe") {
117
- return (object.type === "CallExpression" &&
141
+ const args = children(node.arguments);
142
+ if (identifierName(child(callee.property)) === "pipe") {
143
+ const object = child(callee.object);
144
+ return (object?.type === "CallExpression" &&
118
145
  isState(object) &&
119
- node.arguments.every(keepsState));
146
+ args.every(keepsState));
120
147
  }
121
- const [argument] = node.arguments;
122
- return (object.type === "Identifier" &&
123
- object.name === "Atom" &&
124
- property.type === "Identifier" &&
125
- property.name === "make" &&
148
+ const [argument] = args;
149
+ return (atomMethod(callee) === "make" &&
126
150
  argument !== undefined &&
127
151
  plainValues.has(unwrap(argument).type));
128
152
  };
129
153
  /** A property's name, when it is written out: `todosAtom` in `{ todosAtom: ... }`. */
130
154
  const propertyName = (property) => {
131
- const key = property.key;
132
- if (property.computed) {
155
+ if (property.computed === true) {
133
156
  return undefined;
134
157
  }
135
- if (key.type === "Identifier") {
136
- return String(key.name);
158
+ const key = child(property.key);
159
+ if (key?.type === "Identifier") {
160
+ return identifierName(key);
137
161
  }
138
- return key.type === "Literal" && typeof key.value === "string"
162
+ return key?.type === "Literal" && typeof key.value === "string"
139
163
  ? key.value
140
164
  : undefined;
141
165
  };
142
166
  /** Every node below `node`, depth first. */
143
167
  const descendants = (node, found = []) => {
144
- for (const value of Object.values(node)) {
145
- const children = Array.isArray(value) ? value : [value];
146
- for (const child of children) {
147
- if (typeof child === "object" && child !== null && "type" in child) {
148
- found.push(child);
149
- descendants(child, found);
168
+ const values = Object.values(node);
169
+ for (const value of values) {
170
+ const below = Array.isArray(value) ? value : [value];
171
+ for (const item of below) {
172
+ if (isNode(item)) {
173
+ found.push(item);
174
+ descendants(item, found);
150
175
  }
151
176
  }
152
177
  }
@@ -175,18 +200,19 @@ const sites = (program, base, defaultName, once) => {
175
200
  });
176
201
  };
177
202
  const members = (object, prefix) => {
178
- for (const property of object.properties) {
179
- const key = property.type === "Property" ? propertyName(property) : undefined;
180
- if (key === undefined) {
203
+ for (const property of children(object.properties)) {
204
+ const key = child(property.key);
205
+ const name = property.type === "Property" ? propertyName(property) : undefined;
206
+ const value = unwrapped(property.value);
207
+ if (key === undefined || name === undefined || value === undefined) {
181
208
  continue;
182
209
  }
183
- const value = unwrap(property.value);
184
210
  const node = call(value);
185
211
  if (node) {
186
- add(node, `${prefix}.${key}`, property.key.start);
212
+ add(node, `${prefix}.${name}`, key.start);
187
213
  }
188
214
  else if (value.type === "ObjectExpression") {
189
- members(value, `${prefix}.${key}`);
215
+ members(value, `${prefix}.${name}`);
190
216
  }
191
217
  }
192
218
  };
@@ -194,25 +220,26 @@ const sites = (program, base, defaultName, once) => {
194
220
  if (declaration.type !== "VariableDeclaration") {
195
221
  return;
196
222
  }
197
- for (const declarator of declaration.declarations) {
223
+ for (const declarator of children(declaration.declarations)) {
198
224
  handled.add(declarator);
199
- const id = declarator.id;
200
- if (id.type !== "Identifier" || !declarator.init) {
225
+ const id = child(declarator.id);
226
+ const name = identifierName(id);
227
+ const init = unwrapped(declarator.init);
228
+ if (id === undefined || name === undefined || init === undefined) {
201
229
  continue;
202
230
  }
203
- const init = unwrap(declarator.init);
204
231
  const node = call(init);
205
232
  if (node) {
206
- add(node, String(id.name), id.start);
233
+ add(node, name, id.start);
207
234
  }
208
235
  else if (init.type === "ObjectExpression") {
209
- members(init, String(id.name));
236
+ members(init, name);
210
237
  }
211
238
  }
212
239
  };
213
- for (const statement of program.body) {
240
+ for (const statement of children(program.body)) {
214
241
  if (statement.type === "ExportNamedDeclaration") {
215
- const { declaration } = statement;
242
+ const declaration = child(statement.declaration);
216
243
  if (declaration) {
217
244
  declare(declaration);
218
245
  }
@@ -231,14 +258,14 @@ const sites = (program, base, defaultName, once) => {
231
258
  if (node.type !== "VariableDeclarator" || handled.has(node)) {
232
259
  continue;
233
260
  }
234
- const id = node.id;
235
- const init = node.init ? call(node.init) : undefined;
236
- const start = init ? chainStart(init.callee) : undefined;
237
- if (id.type === "Identifier" &&
238
- init &&
239
- start?.type === "Identifier" &&
240
- start.name === "Atom") {
241
- add(init, String(id.name), id.start, false);
261
+ const id = child(node.id);
262
+ const name = identifierName(id);
263
+ const init = call(node.init);
264
+ if (id !== undefined &&
265
+ name !== undefined &&
266
+ init !== undefined &&
267
+ identifierName(chainStart(init)) === "Atom") {
268
+ add(init, name, id.start, false);
242
269
  }
243
270
  }
244
271
  return found;
@@ -252,14 +279,14 @@ const factoryName = /^(?!use[A-Z]|is[A-Z])[\w$]+Atom$/u;
252
279
  * as `mapDraftAtom({"doc":1})`, so a cache that makes a new atom for a key it has seen shows it.
253
280
  */
254
281
  const factoryCalls = (program, base) => descendants(program).flatMap((node) => {
255
- const callee = node.type === "CallExpression" ? node.callee : undefined;
256
- return callee?.type === "Identifier" &&
257
- factoryName.test(String(callee.name))
282
+ const callee = node.type === "CallExpression" ? child(node.callee) : undefined;
283
+ const name = identifierName(callee);
284
+ return callee !== undefined && name !== undefined && factoryName.test(name)
258
285
  ? [
259
286
  {
260
287
  calleeEnd: base + callee.end,
261
288
  calleeStart: base + callee.start,
262
- name: String(callee.name),
289
+ name,
263
290
  },
264
291
  ]
265
292
  : [];
@@ -319,12 +346,10 @@ const scripts = (source) => {
319
346
  .map((m) => [m.index, m.index + m[0].length]);
320
347
  for (const match of matches) {
321
348
  const { attributes, code } = match.groups ?? {};
322
- if (attributes === undefined || code === undefined) {
323
- continue;
324
- }
325
- if (top
349
+ const nested = top
326
350
  ? !top.has(match.index)
327
- : heads.some(([from, to]) => match.index > from && match.index < to)) {
351
+ : heads.some(([from, to]) => match.index > from && match.index < to);
352
+ if (attributes === undefined || code === undefined || nested) {
328
353
  continue;
329
354
  }
330
355
  found.push({
@@ -378,12 +403,17 @@ const address = (file, root) => {
378
403
  /** Wraps each labelled declaration's call in `label(...)`, and each factory's callee in `call(...)`. */
379
404
  const wrap = (output, code, found, calls, where) => {
380
405
  for (const site of found) {
381
- const options = {
382
- ...(site.family ? { family: true } : {}),
406
+ const options = {};
407
+ if (site.family) {
408
+ options.family = true;
409
+ }
410
+ if (site.keep) {
383
411
  // The declaration's source, hashed: an edit to it means the old value no longer applies.
384
- ...(site.keep ? { keep: hash(code.slice(site.start, site.end)) } : {}),
385
- ...(site.local ? { local: true } : {}),
386
- };
412
+ options.keep = hash(code.slice(site.start, site.end));
413
+ }
414
+ if (site.local) {
415
+ options.local = true;
416
+ }
387
417
  const args = [
388
418
  JSON.stringify(site.name),
389
419
  JSON.stringify(where(site.at)),
package/dist/vite.js CHANGED
@@ -91,7 +91,7 @@ export const atomLabels = (options = {}) => {
91
91
  if (id.includes("?") ||
92
92
  !filter(id) ||
93
93
  packages.some((directory) => id.startsWith(directory))) {
94
- return undefined;
94
+ return;
95
95
  }
96
96
  return labelAtoms(code, id, root, { keep: serving });
97
97
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "effect-atom-svelte-devtools",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Developer tools for effect-atom-svelte: atom names from your source, and a live view of the registry",
5
5
  "keywords": [
6
6
  "devtools",
@@ -56,19 +56,20 @@
56
56
  "magic-string": "^0.30.21"
57
57
  },
58
58
  "devDependencies": {
59
- "@sveltejs/package": "^3.0.0",
60
- "@sveltejs/vite-plugin-svelte": "^7.2.0",
59
+ "@sveltejs/package": "^3.0.3",
60
+ "@sveltejs/vite-plugin-svelte": "^7.3.2",
61
61
  "@types/node": "^24",
62
- "effect": "4.0.1",
62
+ "effect": "4.1.7",
63
63
  "effect-atom-svelte": "workspace:*",
64
- "svelte": "^5.57.2",
65
- "svelte-check": "^4.6.0",
66
- "vite": "^8.3.0",
67
- "vitest": "^5.0.3"
64
+ "svelte": "^5.59.2",
65
+ "svelte-check": "^4.7.8",
66
+ "typescript": "^6.0.3",
67
+ "vite": "^8.3.4",
68
+ "vitest": "^5.0.5"
68
69
  },
69
70
  "peerDependencies": {
70
- "effect": "~4.0.0",
71
- "effect-atom-svelte": "^0.3.0",
71
+ "effect": "~4.1.0",
72
+ "effect-atom-svelte": "^0.5.0",
72
73
  "svelte": "^5.57.2",
73
74
  "vite": ">=5.0.0"
74
75
  },