signalium 2.2.3 → 2.3.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/CHANGELOG.md +10 -0
- package/dist/cjs/development/config-B0MtLBgx.js.map +1 -1
- package/dist/cjs/development/{debug-3nd-6Gnf.js → debug-gCDAvnLM.js} +211 -207
- package/dist/cjs/development/debug-gCDAvnLM.js.map +1 -0
- package/dist/cjs/development/debug.js +9 -3
- package/dist/cjs/development/debug.js.map +1 -1
- package/dist/cjs/development/index.js +8 -8
- package/dist/cjs/development/react/index.js +43 -38
- package/dist/cjs/development/react/index.js.map +1 -1
- package/dist/cjs/development/snapshot-Di0yziPX.js +147 -0
- package/dist/cjs/development/snapshot-Di0yziPX.js.map +1 -0
- package/dist/cjs/development/transform/index.js +137 -118
- package/dist/cjs/development/transform/index.js.map +1 -1
- package/dist/cjs/development/utils.js +5 -3
- package/dist/cjs/development/utils.js.map +1 -1
- package/dist/cjs/production/config-B0MtLBgx.js.map +1 -1
- package/dist/cjs/production/{contexts-DoZWv_3I.js → contexts-Wgq2NOVX.js} +130 -133
- package/dist/cjs/production/contexts-Wgq2NOVX.js.map +1 -0
- package/dist/cjs/production/debug.js +61 -66
- package/dist/cjs/production/debug.js.map +1 -1
- package/dist/cjs/production/index.js +8 -8
- package/dist/cjs/production/react/index.js +43 -38
- package/dist/cjs/production/react/index.js.map +1 -1
- package/dist/cjs/production/snapshot-YJJyLbxS.js +147 -0
- package/dist/cjs/production/snapshot-YJJyLbxS.js.map +1 -0
- package/dist/cjs/production/transform/index.js +137 -118
- package/dist/cjs/production/transform/index.js.map +1 -1
- package/dist/cjs/production/utils.js +5 -3
- package/dist/cjs/production/utils.js.map +1 -1
- package/dist/esm/development/config-CPQL7hX-.js.map +1 -1
- package/dist/esm/development/{debug-BfudYKc4.js → debug-AoHfqs62.js} +195 -189
- package/dist/esm/development/debug-AoHfqs62.js.map +1 -0
- package/dist/esm/development/debug.js +1 -1
- package/dist/esm/development/index.js +6 -6
- package/dist/esm/development/react/index.js +43 -38
- package/dist/esm/development/react/index.js.map +1 -1
- package/dist/esm/development/snapshot-Bq0Um_hQ.js +148 -0
- package/dist/esm/development/snapshot-Bq0Um_hQ.js.map +1 -0
- package/dist/esm/development/transform/index.js +137 -118
- package/dist/esm/development/transform/index.js.map +1 -1
- package/dist/esm/development/utils.js +7 -4
- package/dist/esm/development/utils.js.map +1 -1
- package/dist/esm/internals/core-api.d.ts +2 -2
- package/dist/esm/internals/core-api.d.ts.map +1 -1
- package/dist/esm/internals/edge.d.ts +4 -4
- package/dist/esm/internals/edge.d.ts.map +1 -1
- package/dist/esm/internals/reactive.d.ts.map +1 -1
- package/dist/esm/internals/scheduling.d.ts.map +1 -1
- package/dist/esm/internals/signal.d.ts.map +1 -1
- package/dist/esm/internals/utils/snapshot.d.ts +29 -0
- package/dist/esm/internals/utils/snapshot.d.ts.map +1 -0
- package/dist/esm/production/config-CPQL7hX-.js.map +1 -1
- package/dist/esm/production/{contexts-CilfS6eG.js → contexts-X0gSj6rQ.js} +133 -136
- package/dist/esm/production/contexts-X0gSj6rQ.js.map +1 -0
- package/dist/esm/production/debug.js +51 -54
- package/dist/esm/production/debug.js.map +1 -1
- package/dist/esm/production/index.js +7 -7
- package/dist/esm/production/react/index.js +43 -38
- package/dist/esm/production/react/index.js.map +1 -1
- package/dist/esm/production/snapshot-CDS1d8mq.js +148 -0
- package/dist/esm/production/snapshot-CDS1d8mq.js.map +1 -0
- package/dist/esm/production/transform/index.js +137 -118
- package/dist/esm/production/transform/index.js.map +1 -1
- package/dist/esm/production/utils.js +7 -4
- package/dist/esm/production/utils.js.map +1 -1
- package/dist/esm/react/index.d.ts +1 -1
- package/dist/esm/react/index.d.ts.map +1 -1
- package/dist/esm/react/provider.d.ts.map +1 -1
- package/dist/esm/react/use-reactive.d.ts +1 -0
- package/dist/esm/react/use-reactive.d.ts.map +1 -1
- package/dist/esm/transform/callback.d.ts.map +1 -1
- package/dist/esm/transform/promise.d.ts.map +1 -1
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.d.ts.map +1 -1
- package/package.json +7 -5
- package/dist/cjs/development/core-api-C6HCIyL3.js +0 -55
- package/dist/cjs/development/core-api-C6HCIyL3.js.map +0 -1
- package/dist/cjs/development/debug-3nd-6Gnf.js.map +0 -1
- package/dist/cjs/production/contexts-DoZWv_3I.js.map +0 -1
- package/dist/cjs/production/core-api-CUviCxtM.js +0 -55
- package/dist/cjs/production/core-api-CUviCxtM.js.map +0 -1
- package/dist/esm/development/core-api-CjsScNn1.js +0 -56
- package/dist/esm/development/core-api-CjsScNn1.js.map +0 -1
- package/dist/esm/development/debug-BfudYKc4.js.map +0 -1
- package/dist/esm/production/contexts-CilfS6eG.js.map +0 -1
- package/dist/esm/production/core-api-tTQttL8R.js +0 -56
- package/dist/esm/production/core-api-tTQttL8R.js.map +0 -1
|
@@ -19,53 +19,56 @@ const createTransformedImports = (defaultImports, additionalImports, globalImpor
|
|
|
19
19
|
return transformedImports;
|
|
20
20
|
};
|
|
21
21
|
function createSignaliumAsyncTransform(api, opts) {
|
|
22
|
-
const transformedImports = createTransformedImports(
|
|
23
|
-
[
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
],
|
|
31
|
-
opts?.transformedImports,
|
|
32
|
-
opts?.importPaths
|
|
33
|
-
);
|
|
22
|
+
const transformedImports = createTransformedImports([
|
|
23
|
+
["callback", ["signalium"]],
|
|
24
|
+
["reactive", ["signalium"]],
|
|
25
|
+
["reactiveMethod", ["signalium"]],
|
|
26
|
+
["relay", ["signalium"]],
|
|
27
|
+
["task", ["signalium"]],
|
|
28
|
+
["watcher", ["signalium"]]
|
|
29
|
+
], opts?.transformedImports, opts?.importPaths);
|
|
34
30
|
const t = api.types;
|
|
35
31
|
const isTrackedImport = (localName, path) => {
|
|
36
32
|
const binding = path.scope.getBinding(localName);
|
|
37
|
-
if (!binding || !t.isImportSpecifier(binding.path.node))
|
|
33
|
+
if (!binding || !t.isImportSpecifier(binding.path.node))
|
|
34
|
+
return false;
|
|
38
35
|
const importSpec = binding.path.node;
|
|
39
36
|
const importedName = importSpec.imported.name;
|
|
40
37
|
const importDecl = binding.path.parent;
|
|
41
|
-
if (!t.isImportDeclaration(importDecl))
|
|
38
|
+
if (!t.isImportDeclaration(importDecl))
|
|
39
|
+
return false;
|
|
42
40
|
const importPaths = transformedImports.get(importedName);
|
|
43
|
-
if (!importPaths)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
);
|
|
41
|
+
if (!importPaths)
|
|
42
|
+
return false;
|
|
43
|
+
return importPaths.some((p) => typeof p === "string" ? importDecl.source.value === p : p.test(importDecl.source.value));
|
|
47
44
|
};
|
|
48
45
|
const isReactiveCall = (path) => {
|
|
49
|
-
if (!t.isCallExpression(path.node))
|
|
46
|
+
if (!t.isCallExpression(path.node))
|
|
47
|
+
return false;
|
|
50
48
|
const callee = path.node.callee;
|
|
51
|
-
if (!t.isIdentifier(callee))
|
|
49
|
+
if (!t.isIdentifier(callee))
|
|
50
|
+
return false;
|
|
52
51
|
return isTrackedImport(callee.name, path);
|
|
53
52
|
};
|
|
54
53
|
const isWithinTrackedCall = (path) => {
|
|
55
54
|
let current = path.parentPath;
|
|
56
55
|
while (current) {
|
|
57
|
-
if (current.isCallExpression() && isReactiveCall(current))
|
|
56
|
+
if (current.isCallExpression() && isReactiveCall(current))
|
|
57
|
+
return true;
|
|
58
58
|
current = current.parentPath;
|
|
59
59
|
}
|
|
60
60
|
return false;
|
|
61
61
|
};
|
|
62
62
|
function convertReactiveToGenerator(path) {
|
|
63
|
-
if (!isWithinTrackedCall(path))
|
|
64
|
-
|
|
63
|
+
if (!isWithinTrackedCall(path))
|
|
64
|
+
return;
|
|
65
|
+
if (!path.node.async)
|
|
66
|
+
return;
|
|
65
67
|
path.traverse({
|
|
66
68
|
AwaitExpression(awaitPath) {
|
|
67
69
|
const funcParent = awaitPath.getFunctionParent();
|
|
68
|
-
if (funcParent?.node !== path.node)
|
|
70
|
+
if (funcParent?.node !== path.node)
|
|
71
|
+
return;
|
|
69
72
|
awaitPath.replaceWith(t.yieldExpression(awaitPath.node.argument));
|
|
70
73
|
}
|
|
71
74
|
});
|
|
@@ -103,37 +106,37 @@ function signaliumAsyncTransform(apiOrOpts, maybeOpts) {
|
|
|
103
106
|
return (api) => createSignaliumAsyncTransform(api, apiOrOpts);
|
|
104
107
|
}
|
|
105
108
|
function createSignaliumCallbackTransform(api, opts) {
|
|
106
|
-
const transformedImports = createTransformedImports(
|
|
107
|
-
[
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
opts?.transformedImports,
|
|
115
|
-
opts?.importPaths
|
|
116
|
-
);
|
|
109
|
+
const transformedImports = createTransformedImports([
|
|
110
|
+
["component", ["signalium/react"]],
|
|
111
|
+
["reactive", ["signalium"]],
|
|
112
|
+
["reactiveMethod", ["signalium"]],
|
|
113
|
+
["relay", ["signalium"]],
|
|
114
|
+
["task", ["signalium"]],
|
|
115
|
+
["watcher", ["signalium"]]
|
|
116
|
+
], opts?.transformedImports, opts?.importPaths);
|
|
117
117
|
const t = api.types;
|
|
118
118
|
const callbackImportPath = opts?.callbackImportPath ?? "signalium";
|
|
119
119
|
const isTrackedImport = (localName, path) => {
|
|
120
120
|
const binding = path.scope.getBinding(localName);
|
|
121
|
-
if (!binding || !t.isImportSpecifier(binding.path.node))
|
|
121
|
+
if (!binding || !t.isImportSpecifier(binding.path.node))
|
|
122
|
+
return false;
|
|
122
123
|
const importSpec = binding.path.node;
|
|
123
124
|
const importedName = importSpec.imported.name;
|
|
124
125
|
const importDecl = binding.path.parent;
|
|
125
|
-
if (!t.isImportDeclaration(importDecl))
|
|
126
|
+
if (!t.isImportDeclaration(importDecl))
|
|
127
|
+
return false;
|
|
126
128
|
const importPaths = transformedImports.get(importedName);
|
|
127
|
-
if (!importPaths)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
);
|
|
129
|
+
if (!importPaths)
|
|
130
|
+
return false;
|
|
131
|
+
const matches = importPaths.find((p) => typeof p === "string" ? importDecl.source.value === p : p.test(importDecl.source.value));
|
|
131
132
|
return matches ? typeof matches === "string" ? matches : importDecl.source.value : false;
|
|
132
133
|
};
|
|
133
134
|
const isTargetWrapperCall = (path) => {
|
|
134
|
-
if (!t.isCallExpression(path.node))
|
|
135
|
+
if (!t.isCallExpression(path.node))
|
|
136
|
+
return false;
|
|
135
137
|
const callee = path.node.callee;
|
|
136
|
-
if (!t.isIdentifier(callee))
|
|
138
|
+
if (!t.isIdentifier(callee))
|
|
139
|
+
return false;
|
|
137
140
|
return !!isTrackedImport(callee.name, path);
|
|
138
141
|
};
|
|
139
142
|
function isIdentifierInTypePosition(refPath) {
|
|
@@ -143,7 +146,8 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
143
146
|
const nodeType = current.node.type;
|
|
144
147
|
if (nodeType && nodeType.startsWith("TS")) {
|
|
145
148
|
if (current.isTSAsExpression() || current.isTSSatisfiesExpression?.() || current.isTSNonNullExpression() || current.isTSInstantiationExpression?.()) {
|
|
146
|
-
if (child.key === "expression")
|
|
149
|
+
if (child.key === "expression")
|
|
150
|
+
return false;
|
|
147
151
|
return true;
|
|
148
152
|
}
|
|
149
153
|
return true;
|
|
@@ -155,9 +159,11 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
155
159
|
}
|
|
156
160
|
function ensureCallbackIdentifier(programPath) {
|
|
157
161
|
for (const bodyPath of programPath.get("body")) {
|
|
158
|
-
if (!bodyPath.isImportDeclaration())
|
|
162
|
+
if (!bodyPath.isImportDeclaration())
|
|
163
|
+
continue;
|
|
159
164
|
const importDecl2 = bodyPath.node;
|
|
160
|
-
if (importDecl2.source.value !== callbackImportPath)
|
|
165
|
+
if (importDecl2.source.value !== callbackImportPath)
|
|
166
|
+
continue;
|
|
161
167
|
for (const spec of importDecl2.specifiers) {
|
|
162
168
|
if (spec.type === "ImportSpecifier") {
|
|
163
169
|
const ispec = spec;
|
|
@@ -169,18 +175,17 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
169
175
|
}
|
|
170
176
|
}
|
|
171
177
|
for (const bodyPath of programPath.get("body")) {
|
|
172
|
-
if (!bodyPath.isImportDeclaration())
|
|
178
|
+
if (!bodyPath.isImportDeclaration())
|
|
179
|
+
continue;
|
|
173
180
|
const node = bodyPath.node;
|
|
174
|
-
if (node.source.value !== callbackImportPath)
|
|
181
|
+
if (node.source.value !== callbackImportPath)
|
|
182
|
+
continue;
|
|
175
183
|
const localName2 = programPath.scope.generateUidIdentifier("callback").name;
|
|
176
184
|
node.specifiers.push(t.importSpecifier(t.identifier(localName2), t.identifier("callback")));
|
|
177
185
|
return localName2;
|
|
178
186
|
}
|
|
179
187
|
const localName = "callback";
|
|
180
|
-
const importDecl = t.importDeclaration(
|
|
181
|
-
[t.importSpecifier(t.identifier(localName), t.identifier("callback"))],
|
|
182
|
-
t.stringLiteral(callbackImportPath)
|
|
183
|
-
);
|
|
188
|
+
const importDecl = t.importDeclaration([t.importSpecifier(t.identifier(localName), t.identifier("callback"))], t.stringLiteral(callbackImportPath));
|
|
184
189
|
const [first] = programPath.get("body");
|
|
185
190
|
if (first) {
|
|
186
191
|
first.insertBefore(importDecl);
|
|
@@ -196,18 +201,24 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
196
201
|
innerFn.traverse({
|
|
197
202
|
ReferencedIdentifier(refPath) {
|
|
198
203
|
const nearestFn = refPath.getFunctionParent();
|
|
199
|
-
if (!nearestFn || nearestFn.node !== innerNode)
|
|
204
|
+
if (!nearestFn || nearestFn.node !== innerNode)
|
|
205
|
+
return;
|
|
200
206
|
const name = refPath.node.name;
|
|
201
207
|
const binding = refPath.scope.getBinding(name);
|
|
202
|
-
if (!binding)
|
|
203
|
-
|
|
204
|
-
if (
|
|
208
|
+
if (!binding)
|
|
209
|
+
return;
|
|
210
|
+
if (isIdentifierInTypePosition(refPath))
|
|
211
|
+
return;
|
|
212
|
+
if (binding.scope.path.isProgram())
|
|
213
|
+
return;
|
|
205
214
|
let declScope = binding.scope;
|
|
206
215
|
while (declScope) {
|
|
207
|
-
if (declScope === innerScope)
|
|
216
|
+
if (declScope === innerScope)
|
|
217
|
+
return;
|
|
208
218
|
declScope = declScope.parent;
|
|
209
219
|
}
|
|
210
|
-
if (binding.kind === "param" && binding.scope === innerScope)
|
|
220
|
+
if (binding.kind === "param" && binding.scope === innerScope)
|
|
221
|
+
return;
|
|
211
222
|
depNames.add(name);
|
|
212
223
|
}
|
|
213
224
|
});
|
|
@@ -217,10 +228,13 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
217
228
|
name: "signalium-transform-callback-wrapping",
|
|
218
229
|
visitor: {
|
|
219
230
|
CallExpression(callPath) {
|
|
220
|
-
if (!isTargetWrapperCall(callPath))
|
|
231
|
+
if (!isTargetWrapperCall(callPath))
|
|
232
|
+
return;
|
|
221
233
|
const arg0 = callPath.get("arguments")[0];
|
|
222
|
-
if (!arg0)
|
|
223
|
-
|
|
234
|
+
if (!arg0)
|
|
235
|
+
return;
|
|
236
|
+
if (!(arg0.isFunctionExpression() || arg0.isArrowFunctionExpression()))
|
|
237
|
+
return;
|
|
224
238
|
const outerFn = arg0;
|
|
225
239
|
const programPath = callPath.findParent((p) => p.isProgram());
|
|
226
240
|
const callbackName = ensureCallbackIdentifier(programPath);
|
|
@@ -235,10 +249,12 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
235
249
|
// Initialize counters for any function-like node when first seen
|
|
236
250
|
FunctionExpression: {
|
|
237
251
|
enter(fnPath) {
|
|
238
|
-
if (!counters.has(fnPath.node))
|
|
252
|
+
if (!counters.has(fnPath.node))
|
|
253
|
+
counters.set(fnPath.node, 0);
|
|
239
254
|
},
|
|
240
255
|
exit(innerFnPath) {
|
|
241
|
-
if (innerFnPath.node === outerFn.node)
|
|
256
|
+
if (innerFnPath.node === outerFn.node)
|
|
257
|
+
return;
|
|
242
258
|
const immediateParent = innerFnPath.parentPath;
|
|
243
259
|
if (immediateParent && immediateParent.isCallExpression()) {
|
|
244
260
|
const callee = immediateParent.node.callee;
|
|
@@ -285,10 +301,12 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
285
301
|
},
|
|
286
302
|
ArrowFunctionExpression: {
|
|
287
303
|
enter(fnPath) {
|
|
288
|
-
if (!counters.has(fnPath.node))
|
|
304
|
+
if (!counters.has(fnPath.node))
|
|
305
|
+
counters.set(fnPath.node, 0);
|
|
289
306
|
},
|
|
290
307
|
exit(innerFnPath) {
|
|
291
|
-
if (innerFnPath.node === outerFn.node)
|
|
308
|
+
if (innerFnPath.node === outerFn.node)
|
|
309
|
+
return;
|
|
292
310
|
const immediateParent = innerFnPath.parentPath;
|
|
293
311
|
if (immediateParent && immediateParent.isCallExpression()) {
|
|
294
312
|
const callee = immediateParent.node.callee;
|
|
@@ -335,18 +353,14 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
335
353
|
},
|
|
336
354
|
FunctionDeclaration: {
|
|
337
355
|
enter(fnPath) {
|
|
338
|
-
if (!counters.has(fnPath.node))
|
|
356
|
+
if (!counters.has(fnPath.node))
|
|
357
|
+
counters.set(fnPath.node, 0);
|
|
339
358
|
},
|
|
340
359
|
exit(innerDeclPath) {
|
|
341
360
|
const id = innerDeclPath.node.id;
|
|
342
|
-
if (!id)
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
innerDeclPath.node.params,
|
|
346
|
-
innerDeclPath.node.body,
|
|
347
|
-
innerDeclPath.node.generator,
|
|
348
|
-
innerDeclPath.node.async
|
|
349
|
-
);
|
|
361
|
+
if (!id)
|
|
362
|
+
return;
|
|
363
|
+
const fnExpr = t.functionExpression(id, innerDeclPath.node.params, innerDeclPath.node.body, innerDeclPath.node.generator, innerDeclPath.node.async);
|
|
350
364
|
const deps = Array.from(collectDeps(innerDeclPath));
|
|
351
365
|
const parentFn = innerDeclPath.parentPath?.getFunctionParent() || outerFn;
|
|
352
366
|
const argIndex = getNextIndexFor(parentFn.node);
|
|
@@ -362,17 +376,13 @@ function createSignaliumCallbackTransform(api, opts) {
|
|
|
362
376
|
},
|
|
363
377
|
ObjectMethod: {
|
|
364
378
|
enter(fnPath) {
|
|
365
|
-
if (!counters.has(fnPath.node))
|
|
379
|
+
if (!counters.has(fnPath.node))
|
|
380
|
+
counters.set(fnPath.node, 0);
|
|
366
381
|
},
|
|
367
382
|
exit(innerMethodPath) {
|
|
368
|
-
if (innerMethodPath.node.kind !== "method")
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
innerMethodPath.node.params,
|
|
372
|
-
innerMethodPath.node.body,
|
|
373
|
-
innerMethodPath.node.generator,
|
|
374
|
-
innerMethodPath.node.async
|
|
375
|
-
);
|
|
383
|
+
if (innerMethodPath.node.kind !== "method")
|
|
384
|
+
return;
|
|
385
|
+
const fnExpr = t.functionExpression(null, innerMethodPath.node.params, innerMethodPath.node.body, innerMethodPath.node.generator, innerMethodPath.node.async);
|
|
376
386
|
const deps = Array.from(collectDeps(innerMethodPath));
|
|
377
387
|
const parentFn = innerMethodPath.parentPath?.getFunctionParent() || outerFn;
|
|
378
388
|
const argIndex = getNextIndexFor(parentFn.node);
|
|
@@ -402,51 +412,54 @@ function signaliumCallbackTransform(apiOrOpts, opts) {
|
|
|
402
412
|
}
|
|
403
413
|
const PROMISE_STATIC_METHODS = /* @__PURE__ */ new Set(["all", "race", "any", "allSettled", "resolve", "reject", "withResolvers"]);
|
|
404
414
|
function createSignaliumPromiseMethodsTransform(api, opts) {
|
|
405
|
-
const transformedImports = createTransformedImports(
|
|
406
|
-
[
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
opts?.transformedImports,
|
|
414
|
-
opts?.importPaths
|
|
415
|
-
);
|
|
415
|
+
const transformedImports = createTransformedImports([
|
|
416
|
+
["callback", ["signalium"]],
|
|
417
|
+
["reactive", ["signalium"]],
|
|
418
|
+
["reactiveMethod", ["signalium"]],
|
|
419
|
+
["relay", ["signalium"]],
|
|
420
|
+
["task", ["signalium"]],
|
|
421
|
+
["watcher", ["signalium"]]
|
|
422
|
+
], opts?.transformedImports, opts?.importPaths);
|
|
416
423
|
const t = api.types;
|
|
417
424
|
const promiseImportPath = opts?.promiseImportPath ?? "signalium";
|
|
418
425
|
const isTrackedImport = (localName, path) => {
|
|
419
426
|
const binding = path.scope.getBinding(localName);
|
|
420
|
-
if (!binding || !t.isImportSpecifier(binding.path.node))
|
|
427
|
+
if (!binding || !t.isImportSpecifier(binding.path.node))
|
|
428
|
+
return false;
|
|
421
429
|
const importSpec = binding.path.node;
|
|
422
430
|
const importedName = importSpec.imported.name;
|
|
423
431
|
const importDecl = binding.path.parent;
|
|
424
|
-
if (!t.isImportDeclaration(importDecl))
|
|
432
|
+
if (!t.isImportDeclaration(importDecl))
|
|
433
|
+
return false;
|
|
425
434
|
const importPaths = transformedImports.get(importedName);
|
|
426
|
-
if (!importPaths)
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
);
|
|
435
|
+
if (!importPaths)
|
|
436
|
+
return false;
|
|
437
|
+
return importPaths.some((p) => typeof p === "string" ? importDecl.source.value === p : p.test(importDecl.source.value));
|
|
430
438
|
};
|
|
431
439
|
const isReactiveCall = (path) => {
|
|
432
|
-
if (!t.isCallExpression(path.node))
|
|
440
|
+
if (!t.isCallExpression(path.node))
|
|
441
|
+
return false;
|
|
433
442
|
const callee = path.node.callee;
|
|
434
|
-
if (!t.isIdentifier(callee))
|
|
443
|
+
if (!t.isIdentifier(callee))
|
|
444
|
+
return false;
|
|
435
445
|
return isTrackedImport(callee.name, path);
|
|
436
446
|
};
|
|
437
447
|
const isWithinTrackedCall = (path) => {
|
|
438
448
|
let current = path.parentPath;
|
|
439
449
|
while (current) {
|
|
440
|
-
if (current.isCallExpression() && isReactiveCall(current))
|
|
450
|
+
if (current.isCallExpression() && isReactiveCall(current))
|
|
451
|
+
return true;
|
|
441
452
|
current = current.parentPath;
|
|
442
453
|
}
|
|
443
454
|
return false;
|
|
444
455
|
};
|
|
445
456
|
function ensureReactivePromiseIdentifier(programPath) {
|
|
446
457
|
for (const bodyPath of programPath.get("body")) {
|
|
447
|
-
if (!bodyPath.isImportDeclaration())
|
|
458
|
+
if (!bodyPath.isImportDeclaration())
|
|
459
|
+
continue;
|
|
448
460
|
const importDecl2 = bodyPath.node;
|
|
449
|
-
if (importDecl2.source.value !== promiseImportPath)
|
|
461
|
+
if (importDecl2.source.value !== promiseImportPath)
|
|
462
|
+
continue;
|
|
450
463
|
for (const spec of importDecl2.specifiers) {
|
|
451
464
|
if (spec.type === "ImportSpecifier") {
|
|
452
465
|
const ispec = spec;
|
|
@@ -458,18 +471,17 @@ function createSignaliumPromiseMethodsTransform(api, opts) {
|
|
|
458
471
|
}
|
|
459
472
|
}
|
|
460
473
|
for (const bodyPath of programPath.get("body")) {
|
|
461
|
-
if (!bodyPath.isImportDeclaration())
|
|
474
|
+
if (!bodyPath.isImportDeclaration())
|
|
475
|
+
continue;
|
|
462
476
|
const node = bodyPath.node;
|
|
463
|
-
if (node.source.value !== promiseImportPath)
|
|
477
|
+
if (node.source.value !== promiseImportPath)
|
|
478
|
+
continue;
|
|
464
479
|
const localName2 = "ReactivePromise";
|
|
465
480
|
node.specifiers.push(t.importSpecifier(t.identifier(localName2), t.identifier("ReactivePromise")));
|
|
466
481
|
return localName2;
|
|
467
482
|
}
|
|
468
483
|
const localName = "ReactivePromise";
|
|
469
|
-
const importDecl = t.importDeclaration(
|
|
470
|
-
[t.importSpecifier(t.identifier(localName), t.identifier("ReactivePromise"))],
|
|
471
|
-
t.stringLiteral(promiseImportPath)
|
|
472
|
-
);
|
|
484
|
+
const importDecl = t.importDeclaration([t.importSpecifier(t.identifier(localName), t.identifier("ReactivePromise"))], t.stringLiteral(promiseImportPath));
|
|
473
485
|
const [first] = programPath.get("body");
|
|
474
486
|
if (first) {
|
|
475
487
|
first.insertBefore(importDecl);
|
|
@@ -482,17 +494,24 @@ function createSignaliumPromiseMethodsTransform(api, opts) {
|
|
|
482
494
|
name: "signalium-transform-reactive-promise-methods",
|
|
483
495
|
visitor: {
|
|
484
496
|
CallExpression(callPath) {
|
|
485
|
-
if (!isWithinTrackedCall(callPath))
|
|
497
|
+
if (!isWithinTrackedCall(callPath))
|
|
498
|
+
return;
|
|
486
499
|
const callee = callPath.node.callee;
|
|
487
|
-
if (!t.isMemberExpression(callee))
|
|
488
|
-
|
|
500
|
+
if (!t.isMemberExpression(callee))
|
|
501
|
+
return;
|
|
502
|
+
if (callee.computed)
|
|
503
|
+
return;
|
|
489
504
|
const object = callee.object;
|
|
490
505
|
const property = callee.property;
|
|
491
|
-
if (!t.isIdentifier(object, { name: "Promise" }))
|
|
492
|
-
|
|
493
|
-
if (
|
|
506
|
+
if (!t.isIdentifier(object, { name: "Promise" }))
|
|
507
|
+
return;
|
|
508
|
+
if (callPath.scope.getBinding("Promise"))
|
|
509
|
+
return;
|
|
510
|
+
if (!t.isIdentifier(property))
|
|
511
|
+
return;
|
|
494
512
|
const methodName = property.name;
|
|
495
|
-
if (!PROMISE_STATIC_METHODS.has(methodName))
|
|
513
|
+
if (!PROMISE_STATIC_METHODS.has(methodName))
|
|
514
|
+
return;
|
|
496
515
|
const programPath = callPath.findParent((p) => p.isProgram());
|
|
497
516
|
const reactivePromiseId = ensureReactivePromiseIdentifier(programPath);
|
|
498
517
|
const newCallee = t.memberExpression(t.identifier(reactivePromiseId), t.identifier(methodName));
|