dismantle 0.4.3 → 0.5.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.
Files changed (73) hide show
  1. package/dist/cjs/development/index.cjs +963 -1197
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/development/runtime.cjs +55 -1
  4. package/dist/cjs/development/runtime.cjs.map +2 -2
  5. package/dist/cjs/production/index.cjs +1 -1
  6. package/dist/cjs/production/runtime.cjs +1 -1
  7. package/dist/esm/development/index.mjs +963 -1197
  8. package/dist/esm/development/index.mjs.map +4 -4
  9. package/dist/esm/development/runtime.mjs +55 -1
  10. package/dist/esm/development/runtime.mjs.map +2 -2
  11. package/dist/esm/production/index.mjs +1 -1
  12. package/dist/esm/production/runtime.mjs +1 -1
  13. package/dist/types/runtime/index.d.ts +9 -0
  14. package/dist/types/runtime/index.d.ts.map +1 -1
  15. package/dist/types/src/constants.d.ts +8 -0
  16. package/dist/types/src/constants.d.ts.map +1 -0
  17. package/dist/types/src/patch-v8-identifier.d.ts +4 -0
  18. package/dist/types/src/patch-v8-identifier.d.ts.map +1 -0
  19. package/dist/types/src/plugin.d.ts.map +1 -1
  20. package/dist/types/src/split-block-directive.d.ts +1 -1
  21. package/dist/types/src/split-block-directive.d.ts.map +1 -1
  22. package/dist/types/src/split-function.d.ts +5 -0
  23. package/dist/types/src/split-function.d.ts.map +1 -0
  24. package/dist/types/src/split.d.ts +13 -7
  25. package/dist/types/src/split.d.ts.map +1 -1
  26. package/dist/types/src/transform-block-directive.d.ts.map +1 -1
  27. package/dist/types/src/transform-call.d.ts.map +1 -1
  28. package/dist/types/src/transform-function-directive.d.ts.map +1 -1
  29. package/dist/types/src/types.d.ts +2 -3
  30. package/dist/types/src/types.d.ts.map +1 -1
  31. package/dist/types/src/utils/bubble-function-declaration.d.ts +1 -1
  32. package/dist/types/src/utils/bubble-function-declaration.d.ts.map +1 -1
  33. package/dist/types/src/utils/directive-check.d.ts +8 -3
  34. package/dist/types/src/utils/directive-check.d.ts.map +1 -1
  35. package/dist/types/src/utils/get-foreign-bindings.d.ts +3 -1
  36. package/dist/types/src/utils/get-foreign-bindings.d.ts.map +1 -1
  37. package/dist/types/src/utils/get-import-identifier.d.ts +2 -2
  38. package/dist/types/src/utils/get-import-identifier.d.ts.map +1 -1
  39. package/dist/types/src/utils/is-valid-function.d.ts +3 -0
  40. package/dist/types/src/utils/is-valid-function.d.ts.map +1 -0
  41. package/dist/types/src/utils/unwrap.d.ts.map +1 -1
  42. package/package.json +27 -29
  43. package/src/{hidden-imports.ts → constants.ts} +12 -0
  44. package/src/patch-v8-identifier.ts +67 -0
  45. package/src/plugin.ts +31 -16
  46. package/src/split-block-directive.ts +113 -86
  47. package/src/split-function.ts +56 -0
  48. package/src/split.ts +431 -173
  49. package/src/transform-block-directive.ts +29 -10
  50. package/src/transform-call.ts +9 -25
  51. package/src/transform-function-directive.ts +41 -16
  52. package/src/types.ts +10 -6
  53. package/src/utils/bubble-function-declaration.ts +38 -35
  54. package/src/utils/directive-check.ts +25 -36
  55. package/src/utils/get-foreign-bindings.ts +143 -20
  56. package/src/utils/get-import-identifier.ts +4 -4
  57. package/src/utils/is-valid-function.ts +7 -0
  58. package/src/utils/unwrap.ts +2 -1
  59. package/dist/types/src/hidden-imports.d.ts +0 -4
  60. package/dist/types/src/hidden-imports.d.ts.map +0 -1
  61. package/dist/types/src/split-call.d.ts +0 -6
  62. package/dist/types/src/split-call.d.ts.map +0 -1
  63. package/dist/types/src/split-function-directive.d.ts +0 -5
  64. package/dist/types/src/split-function-directive.d.ts.map +0 -1
  65. package/dist/types/src/utils/get-identifiers-from-lval.d.ts +0 -3
  66. package/dist/types/src/utils/get-identifiers-from-lval.d.ts.map +0 -1
  67. package/dist/types/src/utils/get-root-statement-path.d.ts +0 -3
  68. package/dist/types/src/utils/get-root-statement-path.d.ts.map +0 -1
  69. package/dist/types/src/utils/is-statement-top-level.d.ts +0 -4
  70. package/dist/types/src/utils/is-statement-top-level.d.ts.map +0 -1
  71. package/src/split-call.ts +0 -410
  72. package/src/split-function-directive.ts +0 -349
  73. package/src/utils/get-identifiers-from-lval.ts +0 -44
@@ -37,9 +37,9 @@ var babel = __toESM(require("@babel/core"), 1);
37
37
  var import_node_path = __toESM(require("node:path"), 1);
38
38
 
39
39
  // src/split-block-directive.ts
40
- var t9 = __toESM(require("@babel/types"), 1);
40
+ var t7 = __toESM(require("@babel/types"), 1);
41
41
 
42
- // src/hidden-imports.ts
42
+ // src/constants.ts
43
43
  var HIDDEN_FUNC = {
44
44
  kind: "named",
45
45
  source: "dismantle/runtime",
@@ -50,9 +50,23 @@ var HIDDEN_GENERATOR = {
50
50
  source: "dismantle/runtime",
51
51
  name: "$$gen"
52
52
  };
53
+ var HIDDEN_CONTEXT = {
54
+ kind: "named",
55
+ source: "dismantle/runtime",
56
+ name: "$$context"
57
+ };
58
+ var DISMANTLE_CONTEXT = "dismantle__context";
59
+ var DISMANTLE_GEN = "dismantle__gen";
60
+ var DISMANTLE_FUNC = "dismantle__func";
53
61
 
54
62
  // src/split.ts
55
- var t7 = __toESM(require("@babel/types"), 1);
63
+ var t5 = __toESM(require("@babel/types"), 1);
64
+
65
+ // src/patch-v8-identifier.ts
66
+ var import_traverse = __toESM(require("@babel/traverse"), 1);
67
+
68
+ // src/utils/get-import-identifier.ts
69
+ var t2 = __toESM(require("@babel/types"), 1);
56
70
 
57
71
  // src/utils/generate-unique-name.ts
58
72
  var t = __toESM(require("@babel/types"), 1);
@@ -69,6 +83,69 @@ function generateUniqueName(path2, name) {
69
83
  return t.identifier(uid);
70
84
  }
71
85
 
86
+ // src/utils/get-import-identifier.ts
87
+ function getImportIdentifier(imports, path2, registration) {
88
+ const name = registration.kind === "named" ? registration.name : "default";
89
+ const target = `${registration.source}[${name}]`;
90
+ const current = imports.get(target);
91
+ if (current) {
92
+ return current;
93
+ }
94
+ const programParent = path2.scope.getProgramParent();
95
+ const uid = generateUniqueName(programParent.path, name);
96
+ programParent.registerDeclaration(
97
+ programParent.path.unshiftContainer(
98
+ "body",
99
+ t2.importDeclaration(
100
+ [
101
+ registration.kind === "named" ? t2.importSpecifier(uid, t2.identifier(registration.name)) : t2.importDefaultSpecifier(uid)
102
+ ],
103
+ t2.stringLiteral(registration.source)
104
+ )
105
+ )[0]
106
+ );
107
+ imports.set(target, uid);
108
+ return uid;
109
+ }
110
+
111
+ // src/patch-v8-identifier.ts
112
+ function getV8Replacement(ctx, path2) {
113
+ switch (path2.node.name) {
114
+ case DISMANTLE_CONTEXT:
115
+ return getImportIdentifier(ctx.map, path2, {
116
+ ...HIDDEN_CONTEXT,
117
+ source: ctx.runtime
118
+ });
119
+ case DISMANTLE_FUNC:
120
+ return getImportIdentifier(ctx.map, path2, {
121
+ ...HIDDEN_FUNC,
122
+ source: ctx.runtime
123
+ });
124
+ case DISMANTLE_GEN:
125
+ return getImportIdentifier(ctx.map, path2, {
126
+ ...HIDDEN_GENERATOR,
127
+ source: ctx.runtime
128
+ });
129
+ default:
130
+ return void 0;
131
+ }
132
+ }
133
+ var V8_PATCH = {
134
+ V8IntrinsicIdentifier(path2, ctx) {
135
+ const result = getV8Replacement(ctx, path2);
136
+ if (result) {
137
+ path2.replaceWith(result);
138
+ }
139
+ }
140
+ };
141
+ var traverse = typeof import_traverse.default !== "function" ? import_traverse.default.default : import_traverse.default;
142
+ function patchV8Identifier(ctx, node) {
143
+ traverse(node, V8_PATCH, void 0, {
144
+ map: /* @__PURE__ */ new Map(),
145
+ runtime: ctx.options.runtime
146
+ });
147
+ }
148
+
72
149
  // src/utils/generator-shim.ts
73
150
  var import_generator = __toESM(require("@babel/generator"), 1);
74
151
  var generator = typeof import_generator.default !== "function" ? import_generator.default.default : import_generator.default;
@@ -122,99 +199,13 @@ function getDescriptiveName(path2, defaultName) {
122
199
  return defaultName;
123
200
  }
124
201
 
125
- // src/utils/get-foreign-bindings.ts
126
- var t2 = __toESM(require("@babel/types"), 1);
127
- function isForeignBinding(source, current, name, mode) {
128
- if (current.scope.hasGlobal(name)) {
129
- return false;
130
- }
131
- if (source === current) {
132
- return true;
133
- }
134
- if (current.scope.hasOwnBinding(name)) {
135
- if (mode === "block") {
136
- const binding = current.scope.getBinding(name);
137
- return !!binding && binding.kind === "param";
138
- }
139
- return false;
140
- }
141
- if (current.parentPath) {
142
- return isForeignBinding(source, current.parentPath, name, mode);
143
- }
144
- return true;
145
- }
146
- function isInTypescript(path2) {
147
- let parent = path2.parentPath;
148
- while (parent) {
149
- if (t2.isTypeScript(parent.node) && !t2.isExpression(parent.node)) {
150
- return true;
151
- }
152
- parent = parent.parentPath;
153
- }
154
- return false;
155
- }
156
- function getForeignBindings(path2, mode) {
157
- const identifiers = /* @__PURE__ */ new Set();
158
- path2.traverse({
159
- ReferencedIdentifier(p) {
160
- if (!isInTypescript(p) && (mode === "expression" ? !path2.scope.hasGlobal(p.node.name) : isForeignBinding(path2, p, p.node.name, mode))) {
161
- identifiers.add(p.node.name);
162
- }
163
- }
164
- });
165
- return identifiers;
166
- }
167
-
168
- // src/utils/get-identifiers-from-lval.ts
169
- var t3 = __toESM(require("@babel/types"), 1);
170
- function getIdentifiersFromArrayPattern(node) {
171
- const ids = [];
172
- for (let i = 0, len = node.elements.length; i < len; i++) {
173
- const el = node.elements[i];
174
- if (el) {
175
- ids.push(...getIdentifiersFromLVal(el));
176
- }
177
- }
178
- return ids;
179
- }
180
- function getIdentifiersFromObjectPattern(node) {
181
- const ids = [];
182
- for (let i = 0, len = node.properties.length; i < len; i++) {
183
- const el = node.properties[i];
184
- if (el) {
185
- if (el.type === "RestElement") {
186
- ids.push(...getIdentifiersFromLVal(el));
187
- } else if (t3.isLVal(el.value)) {
188
- ids.push(...getIdentifiersFromLVal(el.value));
189
- }
190
- }
191
- }
192
- return ids;
193
- }
194
- function getIdentifiersFromLVal(node) {
195
- switch (node.type) {
196
- case "Identifier":
197
- return [node.name];
198
- case "ArrayPattern":
199
- return getIdentifiersFromArrayPattern(node);
200
- case "AssignmentPattern":
201
- return getIdentifiersFromLVal(node.left);
202
- case "ObjectPattern":
203
- return getIdentifiersFromObjectPattern(node);
204
- case "RestElement":
205
- return getIdentifiersFromLVal(node.argument);
206
- default:
207
- return [];
208
- }
209
- }
210
-
211
202
  // src/utils/get-module-definition.ts
212
- var t5 = __toESM(require("@babel/types"), 1);
203
+ var t4 = __toESM(require("@babel/types"), 1);
213
204
 
214
205
  // src/utils/get-import-specifier-name.ts
215
- var t4 = __toESM(require("@babel/types"), 1);
206
+ var t3 = __toESM(require("@babel/types"), 1);
216
207
  function getImportSpecifierName(node) {
217
- if (t4.isIdentifier(node.imported)) {
208
+ if (t3.isIdentifier(node.imported)) {
218
209
  return node.imported.name;
219
210
  }
220
211
  return node.imported.value;
@@ -222,7 +213,8 @@ function getImportSpecifierName(node) {
222
213
 
223
214
  // src/utils/unwrap.ts
224
215
  function isPathValid(path2, key) {
225
- return key(path2.node);
216
+ const node = path2.node;
217
+ return node ? key(node) : false;
226
218
  }
227
219
  function isNestedExpression(node) {
228
220
  switch (node.type) {
@@ -259,7 +251,7 @@ function unwrapPath(path2, key) {
259
251
 
260
252
  // src/utils/get-module-definition.ts
261
253
  function getModuleDefinition(path2) {
262
- if (!(isPathValid(path2, t5.isImportSpecifier) || isPathValid(path2, t5.isImportDefaultSpecifier) || isPathValid(path2, t5.isImportNamespaceSpecifier))) {
254
+ if (!(isPathValid(path2, t4.isImportSpecifier) || isPathValid(path2, t4.isImportDefaultSpecifier) || isPathValid(path2, t4.isImportNamespaceSpecifier))) {
263
255
  return void 0;
264
256
  }
265
257
  const parent = path2.getStatementParent();
@@ -297,38 +289,24 @@ function getModuleDefinition(path2) {
297
289
  }
298
290
  }
299
291
 
300
- // src/utils/get-root-statement-path.ts
301
- var t6 = __toESM(require("@babel/types"), 1);
302
- function getRootStatementPath(path2) {
303
- let current = path2.parentPath;
304
- while (current) {
305
- const next = current.parentPath;
306
- if (next && t6.isProgram(next.node)) {
307
- return current;
308
- }
309
- current = next;
310
- }
311
- return path2;
312
- }
313
-
314
292
  // src/split.ts
315
293
  function moduleDefinitionToImportSpecifier(definition) {
316
294
  switch (definition.kind) {
317
295
  case "default":
318
- return t7.importDefaultSpecifier(t7.identifier(definition.local));
296
+ return t5.importDefaultSpecifier(t5.identifier(definition.local));
319
297
  case "named":
320
- return t7.importSpecifier(
321
- t7.identifier(definition.local),
322
- definition.imported ? t7.identifier(definition.imported) : t7.identifier(definition.local)
298
+ return t5.importSpecifier(
299
+ t5.identifier(definition.local),
300
+ definition.imported ? t5.identifier(definition.imported) : t5.identifier(definition.local)
323
301
  );
324
302
  case "namespace":
325
- return t7.importNamespaceSpecifier(t7.identifier(definition.local));
303
+ return t5.importNamespaceSpecifier(t5.identifier(definition.local));
326
304
  }
327
305
  }
328
306
  function moduleDefinitionToImportDeclaration(definition) {
329
- return t7.importDeclaration(
307
+ return t5.importDeclaration(
330
308
  [moduleDefinitionToImportSpecifier(definition)],
331
- t7.stringLiteral(definition.source)
309
+ t5.stringLiteral(definition.source)
332
310
  );
333
311
  }
334
312
  function moduleDefinitionsToImportDeclarations(definitions) {
@@ -338,88 +316,178 @@ function moduleDefinitionsToImportDeclarations(definitions) {
338
316
  }
339
317
  return declarations;
340
318
  }
341
- function isMutation(kind) {
342
- switch (kind) {
343
- case "let":
344
- case "var":
345
- case "param":
346
- return true;
319
+ function addLocalDependency(dependencies, target) {
320
+ if (target.constant) {
321
+ if (!dependencies.locals.includes(target)) {
322
+ dependencies.locals.push(target);
323
+ }
324
+ } else if (!dependencies.mutations.includes(target)) {
325
+ dependencies.mutations.push(target);
326
+ }
327
+ }
328
+ function extractDependenciesFromVariableDeclarator(dependencies, target, isPure) {
329
+ if (!isPathValid(target.path, t5.isVariableDeclarator)) {
330
+ return;
331
+ }
332
+ const init = unwrapPath(target.path.get("init"), t5.isExpression);
333
+ if (init) {
334
+ if (isPathValid(init, t5.isArrowFunctionExpression) || isPathValid(init, t5.isFunctionExpression)) {
335
+ return;
336
+ }
337
+ }
338
+ if (!isPure) {
339
+ addLocalDependency(dependencies, target);
340
+ }
341
+ }
342
+ function extractDependency(dependencies, target, isPure) {
343
+ switch (target.kind) {
344
+ // For module imports, we just push to them
345
+ // to the module bindings
346
+ case "module": {
347
+ const definition = getModuleDefinition(target.path);
348
+ if (definition) {
349
+ dependencies.modules.push(definition);
350
+ }
351
+ break;
352
+ }
353
+ // For params, we push them as mutable locals
354
+ case "param": {
355
+ if (!isPure) {
356
+ addLocalDependency(dependencies, target);
357
+ }
358
+ break;
359
+ }
347
360
  case "const":
361
+ case "let":
362
+ case "var": {
363
+ extractDependenciesFromVariableDeclarator(dependencies, target, isPure);
364
+ break;
365
+ }
348
366
  case "hoisted":
367
+ break;
349
368
  case "local":
350
- case "module":
351
369
  case "unknown":
352
- return false;
370
+ break;
353
371
  }
354
372
  }
355
- function registerModuleLevelBinding(ctx, modules, binding, target) {
356
- const current = ctx.bindings.get(binding);
357
- if (current) {
358
- modules.push(current);
359
- } else if (isPathValid(target.path, t7.isVariableDeclarator)) {
360
- const definitions = splitVariableDeclarator(ctx, target.path);
361
- for (let k = 0, klen = definitions.length; k < klen; k++) {
362
- modules.push(definitions[k]);
363
- ctx.bindings.set(definitions[k].local, definitions[k]);
373
+ function getBindingDependencies(path2, identifiers, isPure) {
374
+ const bindings = {
375
+ modules: [],
376
+ locals: [],
377
+ mutations: []
378
+ };
379
+ for (const identifier5 of identifiers) {
380
+ const target = path2.scope.getBinding(identifier5);
381
+ if (target) {
382
+ extractDependency(bindings, target, isPure);
383
+ } else {
364
384
  }
365
- } else if (isPathValid(target.path, t7.isFunctionDeclaration)) {
366
- const definition = splitFunctionDeclaration(ctx, target.path);
367
- modules.push(definition);
368
- ctx.bindings.set(definition.local, definition);
369
385
  }
370
- }
371
- function registerBinding(ctx, modules, locals, mutations, binding, target, pure) {
372
- if (target.kind === "module") {
373
- const result = getModuleDefinition(target.path);
374
- if (result) {
375
- modules.push(result);
386
+ return bindings;
387
+ }
388
+ function patchIdentifier(dependencies, identifier5, path2) {
389
+ const binding = path2.scope.getBinding(path2.node.name);
390
+ if (binding) {
391
+ const localsIndex = dependencies.locals.indexOf(binding);
392
+ if (localsIndex > -1) {
393
+ path2.replaceWith(
394
+ t5.memberExpression(
395
+ t5.memberExpression(identifier5, t5.identifier("l")),
396
+ t5.numericLiteral(localsIndex),
397
+ true
398
+ )
399
+ );
400
+ return;
376
401
  }
377
- } else if (target.kind === "param") {
378
- locals.push(target.identifier);
379
- } else {
380
- let blockParent = target.path.scope.getBlockParent();
381
- const programParent = target.path.scope.getProgramParent();
382
- if (blockParent.path === target.path) {
383
- blockParent = blockParent.parent;
402
+ const mutationsIndex = dependencies.mutations.indexOf(binding);
403
+ if (mutationsIndex > -1) {
404
+ path2.replaceWith(
405
+ t5.memberExpression(
406
+ t5.memberExpression(identifier5, t5.identifier("m")),
407
+ t5.numericLiteral(mutationsIndex),
408
+ true
409
+ )
410
+ );
384
411
  }
385
- if (blockParent === programParent) {
386
- registerModuleLevelBinding(ctx, modules, binding, target);
387
- } else if (!pure) {
388
- locals.push(target.identifier);
389
- if (isMutation(target.kind)) {
390
- mutations.push(target.identifier);
391
- }
412
+ }
413
+ }
414
+ function patchArrayPattern(dependencies, context, path2) {
415
+ for (const element of path2.get("elements")) {
416
+ if (isPathValid(element, t5.isLVal)) {
417
+ patchLVal(dependencies, context, element);
392
418
  }
393
419
  }
394
420
  }
395
- function extractBindings(ctx, path2, bindings, pure) {
396
- const modules = [];
397
- const locals = [];
398
- const mutations = [];
399
- for (const binding of bindings) {
400
- const target = path2.scope.getBinding(binding);
401
- if (target) {
402
- registerBinding(ctx, modules, locals, mutations, binding, target, pure);
421
+ function patchObjectPattern(dependencies, context, path2) {
422
+ for (const property of path2.get("properties")) {
423
+ if (isPathValid(property, t5.isRestElement)) {
424
+ patchLVal(dependencies, context, property.get("argument"));
425
+ } else if (isPathValid(property, t5.isObjectProperty)) {
426
+ const key = property.get("key");
427
+ if (isPathValid(key, t5.isIdentifier) && property.node.shorthand) {
428
+ patchLVal(dependencies, context, key);
429
+ return;
430
+ }
431
+ const value = property.get("value");
432
+ if (isPathValid(value, t5.isLVal)) {
433
+ patchLVal(dependencies, context, value);
434
+ }
403
435
  }
404
436
  }
405
- return {
406
- modules,
407
- locals,
408
- mutations
409
- };
437
+ }
438
+ function patchLVal(dependencies, context, path2) {
439
+ if (isPathValid(path2, t5.isArrayPattern)) {
440
+ patchArrayPattern(dependencies, context, path2);
441
+ } else if (isPathValid(path2, t5.isAssignmentPattern)) {
442
+ patchLVal(dependencies, context, path2.get("left"));
443
+ } else if (isPathValid(path2, t5.isIdentifier)) {
444
+ patchIdentifier(dependencies, context, path2);
445
+ } else if (isPathValid(path2, t5.isObjectPattern)) {
446
+ patchObjectPattern(dependencies, context, path2);
447
+ } else if (isPathValid(path2, t5.isRestElement)) {
448
+ patchLVal(dependencies, context, path2.get("argument"));
449
+ }
450
+ }
451
+ function transformInnerReferences(body, context, dependencies) {
452
+ body.traverse({
453
+ ReferencedIdentifier(path2) {
454
+ patchIdentifier(dependencies, context, path2);
455
+ },
456
+ AssignmentExpression(path2) {
457
+ const left = path2.get("left");
458
+ if (isPathValid(left, t5.isLVal)) {
459
+ patchLVal(dependencies, context, left);
460
+ }
461
+ },
462
+ ForXStatement(path2) {
463
+ const left = path2.get("left");
464
+ if (isPathValid(left, t5.isLVal)) {
465
+ patchLVal(dependencies, context, left);
466
+ }
467
+ },
468
+ UpdateExpression(path2) {
469
+ const id = unwrapPath(path2.get("argument"), t5.isIdentifier);
470
+ if (id) {
471
+ patchIdentifier(dependencies, context, id);
472
+ }
473
+ }
474
+ });
410
475
  }
411
476
  function createVirtualFileName(ctx) {
412
- return `./${ctx.path.base}?${ctx.options.key}=${ctx.virtual.count++}${ctx.path.ext}`;
477
+ return `./${ctx.path.base}?mode=${ctx.options.mode}&${ctx.options.key}=${ctx.virtual.count++}${ctx.path.ext}`;
478
+ }
479
+ function getModuleImports(modules) {
480
+ const statements = [];
481
+ for (const mod of modules) {
482
+ statements.push(moduleDefinitionToImportDeclaration(mod));
483
+ }
484
+ return statements;
413
485
  }
414
- function createRootFile(ctx, bindings, replacement) {
486
+ function createRootFile(ctx, statements) {
415
487
  const rootFile = createVirtualFileName(ctx);
416
- const rootContent = generateCode(
417
- ctx.id,
418
- t7.program([
419
- ...ctx.options.mode === "server" ? moduleDefinitionsToImportDeclarations(bindings.modules) : [],
420
- t7.exportDefaultDeclaration(replacement)
421
- ])
422
- );
488
+ const node = t5.file(t5.program(statements));
489
+ patchV8Identifier(ctx, node);
490
+ const rootContent = generateCode(ctx.id, node);
423
491
  ctx.onVirtualFile(
424
492
  rootFile,
425
493
  { code: rootContent.code, map: rootContent.map },
@@ -427,12 +495,12 @@ function createRootFile(ctx, bindings, replacement) {
427
495
  );
428
496
  return rootFile;
429
497
  }
430
- function createEntryFile(ctx, path2, rootFile, imported, idPrefix) {
498
+ function createEntryFile(ctx, type, path2, rootFile, imported, idPrefix) {
431
499
  let id = `${idPrefix || ""}${ctx.blocks.hash}-${ctx.blocks.count++}`;
432
500
  if (ctx.options.env !== "production") {
433
501
  id += `-${getDescriptiveName(path2, "anonymous")}`;
434
502
  }
435
- const entryID = generateUniqueName(path2, "entry");
503
+ const entryID = t5.identifier("entry");
436
504
  const entryImports = [
437
505
  {
438
506
  kind: imported.kind,
@@ -441,22 +509,37 @@ function createEntryFile(ctx, path2, rootFile, imported, idPrefix) {
441
509
  imported: imported.kind === "named" ? imported.name : void 0
442
510
  }
443
511
  ];
444
- const args = [t7.stringLiteral(id)];
512
+ const args = [t5.stringLiteral(id)];
445
513
  if (rootFile) {
446
- const rootID = generateUniqueName(path2, "root");
514
+ const rootID = t5.identifier("root");
447
515
  entryImports.push({
448
516
  kind: "default",
449
517
  source: rootFile,
450
518
  local: rootID.name
451
519
  });
452
- args.push(rootID);
520
+ const wrapper = t5.identifier("wrapper");
521
+ let keyword;
522
+ if (type === "block") {
523
+ keyword = "$$wrapBlock";
524
+ } else if (type === "function") {
525
+ keyword = "$$wrapFunction";
526
+ } else {
527
+ keyword = "$$wrapGenerator";
528
+ }
529
+ entryImports.push({
530
+ kind: "named",
531
+ source: ctx.options.runtime,
532
+ local: wrapper.name,
533
+ imported: keyword
534
+ });
535
+ args.push(t5.callExpression(wrapper, [rootID]));
453
536
  }
454
537
  const entryFile = createVirtualFileName(ctx);
455
538
  const entryContent = generateCode(
456
539
  ctx.id,
457
- t7.program([
540
+ t5.program([
458
541
  ...moduleDefinitionsToImportDeclarations(entryImports),
459
- t7.exportDefaultDeclaration(t7.callExpression(entryID, args))
542
+ t5.exportDefaultDeclaration(t5.callExpression(entryID, args))
460
543
  ])
461
544
  );
462
545
  ctx.onVirtualFile(
@@ -466,111 +549,54 @@ function createEntryFile(ctx, path2, rootFile, imported, idPrefix) {
466
549
  );
467
550
  return entryFile;
468
551
  }
469
- function splitFunctionDeclaration(ctx, path2) {
470
- const bindings = getForeignBindings(path2, "function");
471
- const { modules } = extractBindings(ctx, path2, bindings);
472
- const file = createVirtualFileName(ctx);
473
- const compiled = generateCode(
474
- ctx.id,
475
- t7.program([
476
- ...moduleDefinitionsToImportDeclarations(modules),
477
- t7.exportNamedDeclaration(path2.node)
478
- ])
479
- );
480
- ctx.onVirtualFile(file, { code: compiled.code, map: compiled.map }, "none");
481
- const statement = getRootStatementPath(path2);
482
- const identifier7 = path2.node.id || generateUniqueName(path2, "func");
483
- const definition = {
484
- kind: "named",
485
- local: identifier7.name,
486
- source: file
487
- };
488
- statement.insertBefore(moduleDefinitionToImportDeclaration(definition));
489
- path2.remove();
490
- return definition;
491
- }
492
- function splitVariableDeclarator(ctx, path2) {
493
- const init = unwrapPath(path2.get("init"), t7.isExpression);
494
- const modules = init ? extractBindings(
495
- ctx,
496
- path2,
497
- getForeignBindings(
498
- path2,
499
- isPathValid(init, t7.isArrowFunctionExpression) || isPathValid(init, t7.isFunctionExpression) ? "function" : "expression"
500
- )
501
- ).modules : [];
502
- const file = createVirtualFileName(ctx);
503
- const parent = path2.parentPath.node;
504
- const compiled = generateCode(
505
- ctx.id,
506
- t7.program([
507
- ...moduleDefinitionsToImportDeclarations(modules),
508
- t7.exportNamedDeclaration(t7.variableDeclaration(parent.kind, [path2.node]))
509
- ])
510
- );
511
- ctx.onVirtualFile(file, { code: compiled.code, map: compiled.map }, "none");
512
- const definitions = getIdentifiersFromLVal(
513
- path2.node.id
514
- ).map((name) => ({
515
- kind: "named",
516
- local: name,
517
- source: file
518
- }));
519
- const statement = getRootStatementPath(path2);
520
- if (statement) {
521
- statement.insertBefore(moduleDefinitionsToImportDeclarations(definitions));
522
- }
523
- path2.remove();
524
- return definitions;
525
- }
526
- var BREAK_KEY = t7.numericLiteral(0);
527
- var CONTINUE_KEY = t7.numericLiteral(1);
528
- var RETURN_KEY = t7.numericLiteral(2);
529
- var NO_HALT_KEY = t7.numericLiteral(3);
530
- var THROW_KEY = t7.numericLiteral(4);
531
- var YIELD_KEY = t7.numericLiteral(5);
552
+ var BREAK_KEY = t5.numericLiteral(0);
553
+ var CONTINUE_KEY = t5.numericLiteral(1);
554
+ var RETURN_KEY = t5.numericLiteral(2);
555
+ var NO_HALT_KEY = t5.numericLiteral(3);
556
+ var THROW_KEY = t5.numericLiteral(4);
557
+ var YIELD_KEY = t5.numericLiteral(5);
532
558
  function getGeneratorReplacementForBlock(path2, registerID, args) {
533
559
  const iterator = generateUniqueName(path2, "iterator");
534
560
  const step = generateUniqueName(path2, "step");
535
561
  const replacement = [
536
- t7.variableDeclaration("let", [
537
- t7.variableDeclarator(step),
562
+ t5.variableDeclaration("let", [
563
+ t5.variableDeclarator(step),
538
564
  // First, get the iterator by calling the generator
539
- t7.variableDeclarator(
565
+ t5.variableDeclarator(
540
566
  iterator,
541
- t7.awaitExpression(t7.callExpression(registerID, args))
567
+ t5.awaitExpression(t5.callExpression(registerID, args))
542
568
  )
543
569
  ]),
544
570
  // Create a while statement, the intent is to
545
571
  // repeatedly iterate the generator
546
- t7.whileStatement(
547
- t7.booleanLiteral(true),
548
- t7.blockStatement([
572
+ t5.whileStatement(
573
+ t5.booleanLiteral(true),
574
+ t5.blockStatement([
549
575
  // Get the next value
550
- t7.expressionStatement(
551
- t7.assignmentExpression(
576
+ t5.expressionStatement(
577
+ t5.assignmentExpression(
552
578
  "=",
553
579
  step,
554
- t7.awaitExpression(
555
- t7.callExpression(
556
- t7.memberExpression(iterator, t7.identifier("next")),
580
+ t5.awaitExpression(
581
+ t5.callExpression(
582
+ t5.memberExpression(iterator, t5.identifier("next")),
557
583
  []
558
584
  )
559
585
  )
560
586
  )
561
587
  ),
562
588
  // Check if the step is done
563
- t7.ifStatement(
564
- t7.memberExpression(step, t7.identifier("done")),
565
- t7.blockStatement([
589
+ t5.ifStatement(
590
+ t5.memberExpression(step, t5.identifier("done")),
591
+ t5.blockStatement([
566
592
  // exit the loop
567
- t7.breakStatement()
593
+ t5.breakStatement()
568
594
  ]),
569
595
  // Otherwise, yield the value
570
- t7.blockStatement([
571
- t7.expressionStatement(
572
- t7.yieldExpression(
573
- t7.memberExpression(step, t7.identifier("value"))
596
+ t5.blockStatement([
597
+ t5.expressionStatement(
598
+ t5.yieldExpression(
599
+ t5.memberExpression(step, t5.identifier("value"))
574
600
  )
575
601
  )
576
602
  ])
@@ -580,108 +606,375 @@ function getGeneratorReplacementForBlock(path2, registerID, args) {
580
606
  ];
581
607
  return [replacement, step];
582
608
  }
583
-
584
- // src/utils/get-import-identifier.ts
585
- var t8 = __toESM(require("@babel/types"), 1);
586
- function getImportIdentifier(state, path2, registration) {
587
- const name = registration.kind === "named" ? registration.name : "default";
588
- const target = `${registration.source}[${name}]`;
589
- const current = state.imports.get(target);
590
- if (current) {
591
- return current;
592
- }
593
- const programParent = path2.scope.getProgramParent();
594
- const uid = generateUniqueName(programParent.path, name);
595
- programParent.registerDeclaration(
596
- programParent.path.unshiftContainer(
597
- "body",
598
- t8.importDeclaration(
599
- [
600
- registration.kind === "named" ? t8.importSpecifier(uid, t8.identifier(registration.name)) : t8.importDefaultSpecifier(uid)
601
- ],
602
- t8.stringLiteral(registration.source)
609
+ function transformRootFunction(root, dependencies) {
610
+ if (dependencies.locals.length === 0 && dependencies.mutations.length === 0) {
611
+ return t5.exportDefaultDeclaration(root.node);
612
+ }
613
+ const path2 = root.get("body");
614
+ const context = generateUniqueName(root, "ctx");
615
+ transformInnerReferences(path2, context, dependencies);
616
+ const newStatement = isPathValid(path2, t5.isExpression) ? t5.blockStatement([t5.returnStatement(path2.node)]) : path2.node;
617
+ const statements = t5.blockStatement([
618
+ t5.variableDeclaration("const", [
619
+ t5.variableDeclarator(
620
+ context,
621
+ t5.callExpression(t5.v8IntrinsicIdentifier(DISMANTLE_CONTEXT), [])
603
622
  )
604
- )[0]
623
+ ]),
624
+ newStatement
625
+ ]);
626
+ return t5.exportDefaultDeclaration(
627
+ t5.isFunctionExpression(root.node) ? t5.functionExpression(
628
+ root.node.id,
629
+ root.node.params,
630
+ statements,
631
+ root.node.generator,
632
+ root.node.async
633
+ ) : t5.arrowFunctionExpression(
634
+ root.node.params,
635
+ statements,
636
+ root.node.async
637
+ )
605
638
  );
606
- state.imports.set(target, uid);
607
- return uid;
608
639
  }
609
-
610
- // src/split-block-directive.ts
611
- function transformBlockContent(path2, mutations) {
612
- const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
613
- const breaks = [];
614
- let breakCount = 0;
615
- const continues = [];
616
- let continueCount = 0;
617
- let hasReturn = false;
618
- let hasYield = false;
619
- const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
620
- path2.traverse({
621
- BreakStatement(child) {
622
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
623
- if (parent === target) {
624
- const replacement = [BREAK_KEY];
625
- breakCount++;
626
- if (child.node.label) {
627
- const targetName = child.node.label.name;
628
- breaks.push(targetName);
629
- replacement.push(t9.stringLiteral(targetName));
630
- } else {
631
- replacement.push(t9.nullLiteral());
632
- }
633
- if (applyMutations) {
634
- replacement.push(t9.callExpression(applyMutations, []));
635
- }
636
- child.replaceWith(t9.returnStatement(t9.arrayExpression(replacement)));
637
- child.skip();
638
- }
639
- },
640
- ContinueStatement(child) {
641
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
642
- if (parent === target) {
643
- const replacement = [CONTINUE_KEY];
644
- continueCount++;
645
- if (child.node.label) {
646
- const targetName = child.node.label.name;
647
- continues.push(targetName);
648
- replacement.push(t9.stringLiteral(targetName));
649
- } else {
650
- replacement.push(t9.nullLiteral());
651
- }
652
- if (applyMutations) {
653
- replacement.push(t9.callExpression(applyMutations, []));
654
- }
655
- child.replaceWith(t9.returnStatement(t9.arrayExpression(replacement)));
656
- child.skip();
657
- }
658
- },
659
- ReturnStatement(child) {
660
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
661
- if (parent === target) {
662
- hasReturn = true;
663
- const arg = child.get("argument");
664
- arg.replaceWith(
665
- t9.arrayExpression([
666
- RETURN_KEY,
667
- arg.node ? arg.node : t9.nullLiteral(),
668
- applyMutations ? t9.callExpression(applyMutations, []) : t9.nullLiteral()
669
- ])
670
- );
671
- }
672
- },
673
- YieldExpression(child) {
674
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
675
- if (parent === target) {
676
- hasYield = true;
677
- if (child.node.delegate) {
678
- } else {
679
- const arg = child.get("argument");
680
- arg.replaceWith(
681
- t9.arrayExpression([
682
- YIELD_KEY,
683
- arg.node ? arg.node : t9.nullLiteral(),
684
- applyMutations ? t9.callExpression(applyMutations, []) : t9.nullLiteral()
640
+ function getFunctionReplacement(ctx, path2, entryFile, dependencies) {
641
+ const rest = generateUniqueName(path2, "rest");
642
+ const returnType = generateUniqueName(path2, "type");
643
+ const returnResult = generateUniqueName(path2, "result");
644
+ const returnMutations = generateUniqueName(path2, "mutations");
645
+ const source = generateUniqueName(path2, "source");
646
+ const replacement = [];
647
+ if (path2.node.generator) {
648
+ const funcID = generateUniqueName(path2, "fn");
649
+ replacement.push(
650
+ t5.variableDeclaration("const", [
651
+ t5.variableDeclarator(
652
+ funcID,
653
+ t5.callExpression(
654
+ getImportIdentifier(ctx.imports, path2, {
655
+ ...HIDDEN_GENERATOR,
656
+ source: ctx.options.runtime
657
+ }),
658
+ [
659
+ source,
660
+ dependencies.mutations.length ? t5.arrowFunctionExpression(
661
+ [returnMutations],
662
+ t5.assignmentExpression(
663
+ "=",
664
+ t5.arrayPattern(
665
+ dependencies.mutations.map((id) => id.identifier)
666
+ ),
667
+ returnMutations
668
+ )
669
+ ) : t5.nullLiteral()
670
+ ]
671
+ )
672
+ )
673
+ ])
674
+ );
675
+ const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
676
+ t5.objectExpression([
677
+ t5.objectProperty(
678
+ t5.identifier("l"),
679
+ t5.arrayExpression(dependencies.locals.map((id) => id.identifier))
680
+ ),
681
+ t5.objectProperty(
682
+ t5.identifier("m"),
683
+ t5.arrayExpression(dependencies.mutations.map((id) => id.identifier))
684
+ )
685
+ ]),
686
+ t5.spreadElement(rest)
687
+ ]);
688
+ for (let i = 0, len = reps.length; i < len; i++) {
689
+ replacement.push(reps[i]);
690
+ }
691
+ replacement.push(
692
+ t5.variableDeclaration("const", [
693
+ t5.variableDeclarator(
694
+ t5.arrayPattern([returnType, returnResult]),
695
+ t5.memberExpression(step, t5.identifier("value"))
696
+ )
697
+ ])
698
+ );
699
+ } else {
700
+ replacement.push(
701
+ t5.variableDeclaration("const", [
702
+ t5.variableDeclarator(
703
+ t5.arrayPattern([returnType, returnResult]),
704
+ t5.awaitExpression(
705
+ t5.callExpression(
706
+ t5.callExpression(
707
+ getImportIdentifier(ctx.imports, path2, {
708
+ ...HIDDEN_FUNC,
709
+ source: ctx.options.runtime
710
+ }),
711
+ [
712
+ source,
713
+ dependencies.mutations.length ? t5.arrowFunctionExpression(
714
+ [returnMutations],
715
+ t5.assignmentExpression(
716
+ "=",
717
+ t5.arrayPattern(
718
+ dependencies.mutations.map((id) => id.identifier)
719
+ ),
720
+ returnMutations
721
+ )
722
+ ) : t5.nullLiteral()
723
+ ]
724
+ ),
725
+ [
726
+ t5.objectExpression([
727
+ t5.objectProperty(
728
+ t5.identifier("l"),
729
+ t5.arrayExpression(
730
+ dependencies.locals.map((id) => id.identifier)
731
+ )
732
+ ),
733
+ t5.objectProperty(
734
+ t5.identifier("m"),
735
+ t5.arrayExpression(
736
+ dependencies.mutations.map((id) => id.identifier)
737
+ )
738
+ )
739
+ ]),
740
+ t5.spreadElement(rest)
741
+ ]
742
+ )
743
+ )
744
+ )
745
+ ])
746
+ );
747
+ }
748
+ replacement.push(t5.returnStatement(returnResult));
749
+ return t5.arrowFunctionExpression(
750
+ [],
751
+ t5.blockStatement([
752
+ t5.variableDeclaration("const", [
753
+ t5.variableDeclarator(
754
+ source,
755
+ t5.memberExpression(
756
+ t5.awaitExpression(t5.importExpression(t5.stringLiteral(entryFile))),
757
+ t5.identifier("default")
758
+ )
759
+ )
760
+ ]),
761
+ t5.returnStatement(
762
+ isPathValid(path2, t5.isFunctionExpression) ? t5.functionExpression(
763
+ path2.node.id,
764
+ [t5.restElement(rest)],
765
+ t5.blockStatement(replacement),
766
+ path2.node.generator,
767
+ true
768
+ ) : t5.arrowFunctionExpression(
769
+ [t5.restElement(rest)],
770
+ t5.blockStatement(replacement),
771
+ true
772
+ )
773
+ )
774
+ ]),
775
+ true
776
+ );
777
+ }
778
+
779
+ // src/utils/get-foreign-bindings.ts
780
+ var t6 = __toESM(require("@babel/types"), 1);
781
+ function isForeignBinding(checker, current, name) {
782
+ if (current.hasGlobal(name)) {
783
+ return false;
784
+ }
785
+ if (current.hasOwnBinding(name)) {
786
+ if (checker.mode === "block" && current === checker.root) {
787
+ const binding = current.getOwnBinding(name);
788
+ if (binding && binding.kind === "param") {
789
+ return true;
790
+ }
791
+ }
792
+ return false;
793
+ }
794
+ if (checker.root === current) {
795
+ return true;
796
+ }
797
+ if (current.parent) {
798
+ return isForeignBinding(checker, current.parent, name);
799
+ }
800
+ return true;
801
+ }
802
+ function isInTypescript(path2) {
803
+ let parent = path2.parentPath;
804
+ while (parent) {
805
+ if (t6.isTypeScript(parent.node) && !t6.isExpression(parent.node)) {
806
+ return true;
807
+ }
808
+ parent = parent.parentPath;
809
+ }
810
+ return false;
811
+ }
812
+ function checkArrayPattern(checker, scope, node) {
813
+ for (const element of node.elements) {
814
+ if (element && t6.isLVal(element)) {
815
+ checkLVal(checker, scope, element);
816
+ }
817
+ }
818
+ }
819
+ function checkIdentifier(checker, scope, node) {
820
+ if (isForeignBinding(checker, scope, node.name)) {
821
+ checker.ids.add(node.name);
822
+ }
823
+ }
824
+ function checkObjectPattern(checker, scope, node) {
825
+ for (const property of node.properties) {
826
+ if (t6.isRestElement(property)) {
827
+ checkLVal(checker, scope, property.argument);
828
+ } else if (t6.isIdentifier(property.key)) {
829
+ if (property.shorthand) {
830
+ checkLVal(checker, scope, property.key);
831
+ } else if (t6.isLVal(property.value)) {
832
+ checkLVal(checker, scope, property.value);
833
+ }
834
+ } else if (t6.isLVal(property.value)) {
835
+ checkLVal(checker, scope, property.value);
836
+ }
837
+ }
838
+ }
839
+ function checkLVal(checker, scope, node) {
840
+ switch (node.type) {
841
+ case "ArrayPattern":
842
+ checkArrayPattern(checker, scope, node);
843
+ break;
844
+ case "AssignmentPattern":
845
+ checkLVal(checker, scope, node.left);
846
+ break;
847
+ case "Identifier":
848
+ checkIdentifier(checker, scope, node);
849
+ break;
850
+ case "MemberExpression":
851
+ break;
852
+ case "ObjectPattern":
853
+ checkObjectPattern(checker, scope, node);
854
+ break;
855
+ case "RestElement":
856
+ checkLVal(checker, scope, node.argument);
857
+ break;
858
+ case "TSAsExpression":
859
+ break;
860
+ case "TSSatisfiesExpression":
861
+ break;
862
+ case "TSNonNullExpression":
863
+ break;
864
+ case "TSTypeAssertion":
865
+ break;
866
+ case "TSParameterProperty":
867
+ break;
868
+ }
869
+ }
870
+ function getForeignBindings(path2, mode) {
871
+ const checker = {
872
+ ids: /* @__PURE__ */ new Set(),
873
+ root: path2.scope,
874
+ mode
875
+ };
876
+ path2.traverse({
877
+ ReferencedIdentifier(p) {
878
+ if (!isInTypescript(p) && isForeignBinding(checker, p.scope, p.node.name)) {
879
+ checker.ids.add(p.node.name);
880
+ }
881
+ },
882
+ AssignmentExpression(p) {
883
+ if (t6.isLVal(p.node.left)) {
884
+ checkLVal(checker, p.scope, p.node.left);
885
+ }
886
+ },
887
+ ForXStatement(p) {
888
+ if (t6.isLVal(p.node.left)) {
889
+ checkLVal(checker, p.scope, p.node.left);
890
+ }
891
+ },
892
+ UpdateExpression(p) {
893
+ const id = unwrapPath(p.get("argument"), t6.isIdentifier);
894
+ if (id) {
895
+ checkIdentifier(checker, p.scope, id.node);
896
+ }
897
+ }
898
+ });
899
+ return checker.ids;
900
+ }
901
+
902
+ // src/split-block-directive.ts
903
+ function transformBlockContent(path2, dependencies) {
904
+ const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
905
+ const breaks = [];
906
+ let breakCount = 0;
907
+ const continues = [];
908
+ let continueCount = 0;
909
+ let hasReturn = false;
910
+ let hasYield = false;
911
+ const context = generateUniqueName(path2, "ctx");
912
+ const applyMutations = dependencies.mutations.length ? t7.memberExpression(context, t7.identifier("m")) : void 0;
913
+ path2.traverse({
914
+ BreakStatement(child) {
915
+ const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
916
+ if (parent === target) {
917
+ const replacement = [BREAK_KEY];
918
+ breakCount++;
919
+ if (child.node.label) {
920
+ const targetName = child.node.label.name;
921
+ breaks.push(targetName);
922
+ replacement.push(t7.stringLiteral(targetName));
923
+ } else {
924
+ replacement.push(t7.nullLiteral());
925
+ }
926
+ if (applyMutations) {
927
+ replacement.push(applyMutations);
928
+ }
929
+ child.replaceWith(t7.returnStatement(t7.arrayExpression(replacement)));
930
+ child.skip();
931
+ }
932
+ },
933
+ ContinueStatement(child) {
934
+ const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
935
+ if (parent === target) {
936
+ const replacement = [CONTINUE_KEY];
937
+ continueCount++;
938
+ if (child.node.label) {
939
+ const targetName = child.node.label.name;
940
+ continues.push(targetName);
941
+ replacement.push(t7.stringLiteral(targetName));
942
+ } else {
943
+ replacement.push(t7.nullLiteral());
944
+ }
945
+ if (applyMutations) {
946
+ replacement.push(applyMutations);
947
+ }
948
+ child.replaceWith(t7.returnStatement(t7.arrayExpression(replacement)));
949
+ child.skip();
950
+ }
951
+ },
952
+ ReturnStatement(child) {
953
+ const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
954
+ if (parent === target) {
955
+ hasReturn = true;
956
+ const arg = child.get("argument");
957
+ arg.replaceWith(
958
+ t7.arrayExpression([
959
+ RETURN_KEY,
960
+ arg.node ? arg.node : t7.nullLiteral(),
961
+ applyMutations ? applyMutations : t7.nullLiteral()
962
+ ])
963
+ );
964
+ }
965
+ },
966
+ YieldExpression(child) {
967
+ const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
968
+ if (parent === target) {
969
+ hasYield = true;
970
+ if (child.node.delegate) {
971
+ } else {
972
+ const arg = child.get("argument");
973
+ arg.replaceWith(
974
+ t7.arrayExpression([
975
+ YIELD_KEY,
976
+ arg.node ? arg.node : t7.nullLiteral(),
977
+ applyMutations ? applyMutations : t7.nullLiteral()
685
978
  ])
686
979
  );
687
980
  }
@@ -692,56 +985,54 @@ function transformBlockContent(path2, mutations) {
692
985
  const throwResult = [THROW_KEY, error];
693
986
  const haltResult = [NO_HALT_KEY];
694
987
  if (applyMutations) {
695
- throwResult.push(t9.callExpression(applyMutations, []));
696
- haltResult.push(t9.nullLiteral());
697
- haltResult.push(t9.callExpression(applyMutations, []));
698
- }
699
- const statements = [
700
- t9.tryStatement(
701
- t9.blockStatement(path2.node.body),
702
- t9.catchClause(
703
- error,
704
- t9.blockStatement([t9.returnStatement(t9.arrayExpression(throwResult))])
705
- )
706
- ),
707
- t9.returnStatement(t9.arrayExpression(haltResult))
708
- ];
709
- if (applyMutations) {
710
- statements.unshift(
711
- t9.variableDeclaration("const", [
712
- t9.variableDeclarator(
713
- applyMutations,
714
- t9.arrowFunctionExpression(
715
- [],
716
- t9.objectExpression(
717
- mutations.map((item) => t9.objectProperty(item, item, false, true))
718
- )
719
- )
720
- )
721
- ])
722
- );
988
+ throwResult.push(applyMutations);
989
+ haltResult.push(t7.nullLiteral());
990
+ haltResult.push(applyMutations);
723
991
  }
724
- path2.node.body = statements;
725
- return { breaks, continues, hasReturn, hasYield, breakCount, continueCount };
992
+ transformInnerReferences(path2, context, dependencies);
993
+ return {
994
+ breaks,
995
+ continues,
996
+ hasReturn,
997
+ hasYield,
998
+ breakCount,
999
+ continueCount,
1000
+ block: t7.blockStatement([
1001
+ t7.variableDeclaration("const", [
1002
+ t7.variableDeclarator(
1003
+ context,
1004
+ t7.callExpression(t7.v8IntrinsicIdentifier(DISMANTLE_CONTEXT), [])
1005
+ )
1006
+ ]),
1007
+ t7.tryStatement(
1008
+ t7.blockStatement(path2.node.body),
1009
+ t7.catchClause(
1010
+ error,
1011
+ t7.blockStatement([t7.returnStatement(t7.arrayExpression(throwResult))])
1012
+ )
1013
+ ),
1014
+ t7.returnStatement(t7.arrayExpression(haltResult))
1015
+ ])
1016
+ };
726
1017
  }
727
1018
  function getBreakCheck(returnType, returnResult, breakCount, breaks, check) {
728
1019
  let current;
729
1020
  if (breakCount !== breaks.length) {
730
- current = t9.blockStatement([t9.breakStatement()]);
1021
+ current = t7.blockStatement([t7.breakStatement()]);
731
1022
  }
732
1023
  for (let i = 0, len = breaks.length; i < len; i++) {
733
1024
  const target = breaks[i];
734
- current = t9.blockStatement([
735
- t9.ifStatement(
736
- t9.binaryExpression("===", returnResult, t9.stringLiteral(target)),
737
- t9.blockStatement([t9.breakStatement(t9.identifier(target))]),
1025
+ current = t7.blockStatement([
1026
+ t7.ifStatement(
1027
+ t7.binaryExpression("===", returnResult, t7.stringLiteral(target)),
1028
+ t7.blockStatement([t7.breakStatement(t7.identifier(target))]),
738
1029
  current
739
1030
  )
740
1031
  ]);
741
1032
  }
742
1033
  if (current) {
743
- return t9.ifStatement(
744
- t9.binaryExpression("===", returnType, BREAK_KEY),
1034
+ return t7.ifStatement(
1035
+ t7.binaryExpression("===", returnType, BREAK_KEY),
745
1036
  current,
746
1037
  check
747
1038
  );
@@ -751,36 +1042,36 @@ function getBreakCheck(returnType, returnResult, breakCount, breaks, check) {
751
1042
  function getContinueCheck(returnType, returnResult, continueCount, continues, check) {
752
1043
  let current;
753
1044
  if (continueCount !== continues.length) {
754
- current = t9.blockStatement([t9.continueStatement()]);
1045
+ current = t7.blockStatement([t7.continueStatement()]);
755
1046
  }
756
1047
  for (let i = 0, len = continues.length; i < len; i++) {
757
1048
  const target = continues[i];
758
- current = t9.blockStatement([
759
- t9.ifStatement(
760
- t9.binaryExpression("===", returnResult, t9.stringLiteral(target)),
761
- t9.blockStatement([t9.continueStatement(t9.identifier(target))]),
1049
+ current = t7.blockStatement([
1050
+ t7.ifStatement(
1051
+ t7.binaryExpression("===", returnResult, t7.stringLiteral(target)),
1052
+ t7.blockStatement([t7.continueStatement(t7.identifier(target))]),
762
1053
  current
763
1054
  )
764
1055
  ]);
765
1056
  }
766
1057
  if (current) {
767
- return t9.ifStatement(
768
- t9.binaryExpression("===", returnType, CONTINUE_KEY),
1058
+ return t7.ifStatement(
1059
+ t7.binaryExpression("===", returnType, CONTINUE_KEY),
769
1060
  current,
770
1061
  check
771
1062
  );
772
1063
  }
773
1064
  return check;
774
1065
  }
775
- function getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting) {
1066
+ function getBlockDirectiveReplacement(ctx, path2, entryFile, dependencies, halting) {
776
1067
  const returnType = generateUniqueName(path2, "type");
777
1068
  const returnResult = generateUniqueName(path2, "result");
778
1069
  const returnMutations = generateUniqueName(path2, "mutations");
779
1070
  let check;
780
1071
  if (halting.hasReturn) {
781
- check = t9.ifStatement(
782
- t9.binaryExpression("===", returnType, RETURN_KEY),
783
- t9.blockStatement([t9.returnStatement(returnResult)]),
1072
+ check = t7.ifStatement(
1073
+ t7.binaryExpression("===", returnType, RETURN_KEY),
1074
+ t7.blockStatement([t7.returnStatement(returnResult)]),
784
1075
  check
785
1076
  );
786
1077
  }
@@ -806,88 +1097,100 @@ function getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting)
806
1097
  if (halting.hasYield) {
807
1098
  const blockID = generateUniqueName(path2, "block");
808
1099
  replacement.push(
809
- t9.variableDeclaration("const", [
810
- t9.variableDeclarator(
1100
+ t7.variableDeclaration("const", [
1101
+ t7.variableDeclarator(
811
1102
  blockID,
812
- t9.callExpression(
813
- getImportIdentifier(ctx, path2, {
1103
+ t7.callExpression(
1104
+ getImportIdentifier(ctx.imports, path2, {
814
1105
  ...HIDDEN_GENERATOR,
815
1106
  source: ctx.options.runtime
816
1107
  }),
817
1108
  [
818
- t9.memberExpression(
819
- t9.awaitExpression(
820
- t9.importExpression(t9.stringLiteral(entryFile))
1109
+ t7.memberExpression(
1110
+ t7.awaitExpression(
1111
+ t7.importExpression(t7.stringLiteral(entryFile))
821
1112
  ),
822
- t9.identifier("default")
1113
+ t7.identifier("default")
823
1114
  ),
824
- bindings.mutations.length ? t9.arrowFunctionExpression(
1115
+ dependencies.mutations.length ? t7.arrowFunctionExpression(
825
1116
  [returnMutations],
826
- t9.assignmentExpression(
1117
+ t7.assignmentExpression(
827
1118
  "=",
828
- t9.objectPattern(
829
- bindings.mutations.map(
830
- (item) => t9.objectProperty(item, item, false, true)
831
- )
1119
+ t7.arrayPattern(
1120
+ dependencies.mutations.map((id) => id.identifier)
832
1121
  ),
833
1122
  returnMutations
834
1123
  )
835
- ) : t9.nullLiteral()
1124
+ ) : t7.nullLiteral()
836
1125
  ]
837
1126
  )
838
1127
  )
839
1128
  ])
840
1129
  );
841
- const [reps, step] = getGeneratorReplacementForBlock(
842
- path2,
843
- blockID,
844
- bindings.locals
845
- );
1130
+ const [reps, step] = getGeneratorReplacementForBlock(path2, blockID, [
1131
+ t7.arrayExpression([
1132
+ t7.arrayExpression(dependencies.locals.map((id) => id.identifier)),
1133
+ t7.arrayExpression(dependencies.mutations.map((id) => id.identifier))
1134
+ ])
1135
+ ]);
846
1136
  for (let i = 0, len = reps.length; i < len; i++) {
847
1137
  replacement.push(reps[i]);
848
1138
  }
849
1139
  replacement.push(
850
- t9.variableDeclaration("const", [
851
- t9.variableDeclarator(
852
- t9.arrayPattern([returnType, returnResult]),
853
- t9.memberExpression(step, t9.identifier("value"))
1140
+ t7.variableDeclaration("const", [
1141
+ t7.variableDeclarator(
1142
+ t7.arrayPattern([returnType, returnResult]),
1143
+ t7.memberExpression(step, t7.identifier("value"))
854
1144
  )
855
1145
  ])
856
1146
  );
857
1147
  } else {
858
1148
  replacement.push(
859
- t9.variableDeclaration("const", [
860
- t9.variableDeclarator(
861
- t9.arrayPattern([returnType, returnResult]),
862
- t9.awaitExpression(
863
- t9.callExpression(
864
- t9.callExpression(
865
- getImportIdentifier(ctx, path2, {
1149
+ t7.variableDeclaration("const", [
1150
+ t7.variableDeclarator(
1151
+ t7.arrayPattern([returnType, returnResult]),
1152
+ t7.awaitExpression(
1153
+ t7.callExpression(
1154
+ t7.callExpression(
1155
+ getImportIdentifier(ctx.imports, path2, {
866
1156
  ...HIDDEN_FUNC,
867
1157
  source: ctx.options.runtime
868
1158
  }),
869
1159
  [
870
- t9.memberExpression(
871
- t9.awaitExpression(
872
- t9.importExpression(t9.stringLiteral(entryFile))
1160
+ t7.memberExpression(
1161
+ t7.awaitExpression(
1162
+ t7.importExpression(t7.stringLiteral(entryFile))
873
1163
  ),
874
- t9.identifier("default")
1164
+ t7.identifier("default")
875
1165
  ),
876
- bindings.mutations.length ? t9.arrowFunctionExpression(
1166
+ dependencies.mutations.length ? t7.arrowFunctionExpression(
877
1167
  [returnMutations],
878
- t9.assignmentExpression(
1168
+ t7.assignmentExpression(
879
1169
  "=",
880
- t9.objectPattern(
881
- bindings.mutations.map(
882
- (item) => t9.objectProperty(item, item, false, true)
883
- )
1170
+ t7.arrayPattern(
1171
+ dependencies.mutations.map((id) => id.identifier)
884
1172
  ),
885
1173
  returnMutations
886
1174
  )
887
- ) : t9.nullLiteral()
1175
+ ) : t7.nullLiteral()
888
1176
  ]
889
1177
  ),
890
- bindings.locals
1178
+ [
1179
+ t7.objectExpression([
1180
+ t7.objectProperty(
1181
+ t7.identifier("l"),
1182
+ t7.arrayExpression(
1183
+ dependencies.locals.map((id) => id.identifier)
1184
+ )
1185
+ ),
1186
+ t7.objectProperty(
1187
+ t7.identifier("m"),
1188
+ t7.arrayExpression(
1189
+ dependencies.mutations.map((id) => id.identifier)
1190
+ )
1191
+ )
1192
+ ])
1193
+ ]
891
1194
  )
892
1195
  )
893
1196
  )
@@ -897,78 +1200,83 @@ function getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting)
897
1200
  if (check) {
898
1201
  replacement.push(check);
899
1202
  }
900
- return t9.blockStatement(replacement);
1203
+ return replacement;
901
1204
  }
902
- function replaceBlockDirective(ctx, path2, directive, bindings) {
903
- const halting = transformBlockContent(path2, bindings.mutations);
904
- const entryFile = createEntryFile(
905
- ctx,
906
- path2,
907
- ctx.options.mode === "server" || directive.isomorphic ? createRootFile(
908
- ctx,
909
- bindings,
910
- t9.functionExpression(
1205
+ function replaceBlockDirective(ctx, path2, directive, dependencies) {
1206
+ const original = path2.node;
1207
+ path2.node = t7.cloneNode(path2.node, true, false);
1208
+ const halting = transformBlockContent(path2, dependencies);
1209
+ const statements = getModuleImports(dependencies.modules);
1210
+ statements.push(
1211
+ t7.exportDefaultDeclaration(
1212
+ t7.functionExpression(
911
1213
  void 0,
912
- bindings.locals,
913
- t9.blockStatement(path2.node.body),
1214
+ [],
1215
+ halting.block,
914
1216
  halting.hasYield,
915
1217
  true
916
1218
  )
917
- ) : void 0,
1219
+ )
1220
+ );
1221
+ const entryFile = createEntryFile(
1222
+ ctx,
1223
+ "block",
1224
+ path2,
1225
+ ctx.options.mode === "server" ? createRootFile(ctx, statements) : void 0,
918
1226
  directive.target,
919
1227
  directive.idPrefix
920
1228
  );
921
- path2.replaceWith(
922
- getBlockDirectiveReplacement(ctx, path2, entryFile, bindings, halting)
1229
+ const result = getBlockDirectiveReplacement(
1230
+ ctx,
1231
+ path2,
1232
+ entryFile,
1233
+ dependencies,
1234
+ halting
923
1235
  );
1236
+ path2.node = original;
1237
+ return result;
924
1238
  }
925
1239
  function splitBlockDirective(ctx, path2, directive) {
926
- replaceBlockDirective(
1240
+ return replaceBlockDirective(
927
1241
  ctx,
928
1242
  path2,
929
1243
  directive,
930
- extractBindings(
931
- ctx,
1244
+ getBindingDependencies(
932
1245
  path2,
933
1246
  getForeignBindings(path2, "block"),
934
- directive.pure
1247
+ !!directive.pure
935
1248
  )
936
1249
  );
937
1250
  }
938
1251
 
939
1252
  // src/utils/directive-check.ts
940
- var t10 = __toESM(require("@babel/types"), 1);
941
- function getValidDirectiveFromString(ctx, string) {
1253
+ var t8 = __toESM(require("@babel/types"), 1);
1254
+ function getValidDirectiveFromString(ctx, type, string) {
942
1255
  for (let i = 0, len = ctx.options.definitions.length; i < len; i++) {
943
1256
  const current = ctx.options.definitions[i];
944
- switch (current.type) {
945
- case "block-directive":
946
- case "function-directive": {
947
- if (current.directive === string) {
948
- return current;
949
- }
950
- break;
951
- }
1257
+ if (current.type === type && current.directive === string) {
1258
+ return current;
952
1259
  }
953
1260
  }
954
1261
  return void 0;
955
1262
  }
956
- function getDefinitionFromDirectives(ctx, path2) {
1263
+ function getDefinitionFromDirectives(ctx, type, path2) {
957
1264
  for (let i = 0, len = path2.node.directives.length; i < len; i++) {
958
1265
  const statement = path2.node.directives[i].value.value;
959
- const directive = getValidDirectiveFromString(ctx, statement);
1266
+ const directive = getValidDirectiveFromString(ctx, type, statement);
960
1267
  if (directive) {
961
1268
  return directive;
962
1269
  }
963
1270
  }
964
1271
  return void 0;
965
1272
  }
966
- function getDefinitionFromFauxDirectives(ctx, path2) {
1273
+ function getDefinitionFromFauxDirectives(ctx, type, path2) {
967
1274
  for (let i = 0, len = path2.node.body.length; i < len; i++) {
968
1275
  const statement = path2.node.body[i];
969
- if (t10.isExpressionStatement(statement) && t10.isStringLiteral(statement.expression)) {
1276
+ if (t8.isExpressionStatement(statement) && t8.isStringLiteral(statement.expression)) {
970
1277
  const directive = getValidDirectiveFromString(
971
1278
  ctx,
1279
+ type,
972
1280
  statement.expression.value
973
1281
  );
974
1282
  if (directive) {
@@ -980,14 +1288,7 @@ function getDefinitionFromFauxDirectives(ctx, path2) {
980
1288
  }
981
1289
  return void 0;
982
1290
  }
983
- function getDirectiveDefinitionFromBlock(ctx, path2) {
984
- const parent = path2.getFunctionParent();
985
- if (parent && !parent.node.async) {
986
- return void 0;
987
- }
988
- return getDefinitionFromDirectives(ctx, path2) || getDefinitionFromFauxDirectives(ctx, path2);
989
- }
990
- function cleanBlockForDirectives(path2, definition) {
1291
+ function cleanDirectives(path2, definition) {
991
1292
  const newDirectives = [];
992
1293
  for (let i = 0, len = path2.node.directives.length; i < len; i++) {
993
1294
  const current = path2.node.directives[i];
@@ -997,11 +1298,11 @@ function cleanBlockForDirectives(path2, definition) {
997
1298
  }
998
1299
  path2.node.directives = newDirectives;
999
1300
  }
1000
- function cleanBlockForFauxDirectives(path2, definition) {
1301
+ function cleanFauxDirectives(path2, definition) {
1001
1302
  const body = path2.get("body");
1002
1303
  for (let i = 0, len = body.length; i < len; i++) {
1003
1304
  const statement = body[i];
1004
- if (t10.isExpressionStatement(statement.node) && t10.isStringLiteral(statement.node.expression)) {
1305
+ if (t8.isExpressionStatement(statement.node) && t8.isStringLiteral(statement.node.expression)) {
1005
1306
  if (statement.node.expression.value === definition.directive) {
1006
1307
  statement.remove();
1007
1308
  return;
@@ -1011,347 +1312,67 @@ function cleanBlockForFauxDirectives(path2, definition) {
1011
1312
  }
1012
1313
 
1013
1314
  // src/transform-block-directive.ts
1014
- function transformBlockDirective(ctx, path2) {
1015
- const definition = getDirectiveDefinitionFromBlock(ctx, path2);
1016
- if (!definition || definition.type !== "block-directive") {
1017
- return;
1315
+ function getBlockDirectiveDefinition(ctx, path2) {
1316
+ const definition = getDefinitionFromDirectives(ctx, "block-directive", path2);
1317
+ if (definition) {
1318
+ cleanDirectives(path2, definition);
1319
+ return definition;
1018
1320
  }
1019
- cleanBlockForDirectives(path2, definition);
1020
- cleanBlockForFauxDirectives(path2, definition);
1021
- splitBlockDirective(ctx, path2, definition);
1321
+ const fauxDefinition = getDefinitionFromFauxDirectives(
1322
+ ctx,
1323
+ "block-directive",
1324
+ path2
1325
+ );
1326
+ if (fauxDefinition) {
1327
+ cleanFauxDirectives(path2, fauxDefinition);
1328
+ return fauxDefinition;
1329
+ }
1330
+ return void 0;
1022
1331
  }
1023
-
1024
- // src/transform-call.ts
1025
- var t12 = __toESM(require("@babel/types"), 1);
1026
-
1027
- // src/split-call.ts
1028
- var t11 = __toESM(require("@babel/types"), 1);
1029
-
1030
- // src/utils/assert.ts
1031
- function assert(cond, error) {
1032
- if (!cond) {
1033
- throw new Error(error);
1332
+ function transformBlockDirective(ctx, path2) {
1333
+ const definition = getBlockDirectiveDefinition(ctx, path2);
1334
+ if (definition) {
1335
+ path2.node.body = splitBlockDirective(ctx, path2, definition);
1336
+ path2.scope.crawl();
1034
1337
  }
1035
1338
  }
1036
1339
 
1037
- // src/split-call.ts
1038
- function transformFunctionContent(path2, mutations) {
1039
- const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
1040
- const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
1041
- path2.traverse({
1042
- ReturnStatement(child) {
1043
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
1044
- if (parent === target) {
1045
- const replacement = [RETURN_KEY];
1046
- if (child.node.argument) {
1047
- replacement.push(child.node.argument);
1048
- } else {
1049
- replacement.push(t11.nullLiteral());
1050
- }
1051
- if (applyMutations) {
1052
- replacement.push(t11.callExpression(applyMutations, []));
1053
- }
1054
- child.replaceWith(t11.returnStatement(t11.arrayExpression(replacement)));
1055
- child.skip();
1056
- }
1057
- }
1058
- });
1059
- const error = generateUniqueName(path2, "error");
1060
- const throwResult = [THROW_KEY, error];
1061
- const haltResult = [NO_HALT_KEY];
1062
- if (applyMutations) {
1063
- throwResult.push(t11.callExpression(applyMutations, []));
1064
- haltResult.push(t11.nullLiteral());
1065
- haltResult.push(t11.callExpression(applyMutations, []));
1066
- }
1067
- const statements = [
1068
- t11.tryStatement(
1069
- t11.blockStatement(path2.node.body),
1070
- t11.catchClause(
1071
- error,
1072
- t11.blockStatement([t11.returnStatement(t11.arrayExpression(throwResult))])
1073
- )
1074
- ),
1075
- t11.returnStatement(t11.arrayExpression(haltResult))
1076
- ];
1077
- if (applyMutations) {
1078
- statements.unshift(
1079
- t11.variableDeclaration("const", [
1080
- t11.variableDeclarator(
1081
- applyMutations,
1082
- t11.arrowFunctionExpression(
1083
- [],
1084
- t11.objectExpression(
1085
- mutations.map((item) => t11.objectProperty(item, item, false, true))
1086
- )
1087
- )
1088
- )
1089
- ])
1090
- );
1091
- }
1092
- path2.node.body = statements;
1093
- }
1094
- function getFunctionReplacement(ctx, path2, entryFile, bindings) {
1095
- const rest = generateUniqueName(path2, "rest");
1096
- const returnType = generateUniqueName(path2, "type");
1097
- const returnResult = generateUniqueName(path2, "result");
1098
- const returnMutations = generateUniqueName(path2, "mutations");
1099
- const source = generateUniqueName(path2, "source");
1100
- const replacement = [];
1101
- if (path2.node.generator) {
1102
- const funcID = generateUniqueName(path2, "fn");
1103
- replacement.push(
1104
- t11.variableDeclaration("const", [
1105
- t11.variableDeclarator(
1106
- funcID,
1107
- t11.callExpression(
1108
- getImportIdentifier(ctx, path2, {
1109
- ...HIDDEN_GENERATOR,
1110
- source: ctx.options.runtime
1111
- }),
1112
- [
1113
- source,
1114
- bindings.mutations.length ? t11.arrowFunctionExpression(
1115
- [returnMutations],
1116
- t11.assignmentExpression(
1117
- "=",
1118
- t11.objectPattern(
1119
- bindings.mutations.map(
1120
- (item) => t11.objectProperty(item, item, false, true)
1121
- )
1122
- ),
1123
- returnMutations
1124
- )
1125
- ) : t11.nullLiteral()
1126
- ]
1127
- )
1128
- )
1129
- ])
1130
- );
1131
- const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
1132
- t11.arrayExpression(bindings.locals),
1133
- t11.spreadElement(rest)
1134
- ]);
1135
- for (let i = 0, len = reps.length; i < len; i++) {
1136
- replacement.push(reps[i]);
1137
- }
1138
- replacement.push(
1139
- t11.variableDeclaration("const", [
1140
- t11.variableDeclarator(
1141
- t11.arrayPattern([returnType, returnResult]),
1142
- t11.memberExpression(step, t11.identifier("value"))
1143
- )
1144
- ])
1145
- );
1146
- } else {
1147
- replacement.push(
1148
- t11.variableDeclaration("const", [
1149
- t11.variableDeclarator(
1150
- t11.arrayPattern([returnType, returnResult]),
1151
- t11.awaitExpression(
1152
- t11.callExpression(
1153
- t11.callExpression(
1154
- getImportIdentifier(ctx, path2, {
1155
- ...HIDDEN_FUNC,
1156
- source: ctx.options.runtime
1157
- }),
1158
- [
1159
- source,
1160
- bindings.mutations.length ? t11.arrowFunctionExpression(
1161
- [returnMutations],
1162
- t11.assignmentExpression(
1163
- "=",
1164
- t11.objectPattern(
1165
- bindings.mutations.map(
1166
- (item) => t11.objectProperty(item, item, false, true)
1167
- )
1168
- ),
1169
- returnMutations
1170
- )
1171
- ) : t11.nullLiteral()
1172
- ]
1173
- ),
1174
- [t11.arrayExpression(bindings.locals), t11.spreadElement(rest)]
1175
- )
1176
- )
1177
- )
1178
- ])
1179
- );
1180
- }
1181
- replacement.push(t11.returnStatement(returnResult));
1182
- return t11.arrowFunctionExpression(
1183
- [],
1184
- t11.blockStatement([
1185
- t11.variableDeclaration("const", [
1186
- t11.variableDeclarator(
1187
- source,
1188
- t11.memberExpression(
1189
- t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1190
- t11.identifier("default")
1191
- )
1192
- )
1193
- ]),
1194
- t11.returnStatement(
1195
- isPathValid(path2, t11.isFunctionExpression) ? t11.functionExpression(
1196
- path2.node.id,
1197
- [t11.restElement(rest)],
1198
- t11.blockStatement(replacement),
1199
- path2.node.generator,
1200
- true
1201
- ) : t11.arrowFunctionExpression(
1202
- [t11.restElement(rest)],
1203
- t11.blockStatement(replacement),
1204
- true
1205
- )
1206
- )
1207
- ]),
1208
- true
1209
- );
1210
- }
1211
- function replaceIsomorphicFunction(ctx, path2, func, bindings) {
1212
- const body = path2.get("body");
1213
- if (isPathValid(body, t11.isExpression)) {
1214
- body.replaceWith(t11.blockStatement([t11.returnStatement(body.node)]));
1215
- }
1216
- assert(isPathValid(body, t11.isBlockStatement), "invariant");
1217
- const entryFile = createEntryFile(
1218
- ctx,
1219
- path2,
1220
- createRootFile(
1221
- ctx,
1222
- bindings,
1223
- t11.isFunctionExpression(path2.node) ? t11.functionExpression(
1224
- path2.node.id,
1225
- [t11.arrayPattern(bindings.locals), ...path2.node.params],
1226
- path2.node.body,
1227
- path2.node.generator,
1228
- path2.node.async
1229
- ) : t11.arrowFunctionExpression(
1230
- [t11.arrayPattern(bindings.locals), ...path2.node.params],
1231
- path2.node.body,
1232
- path2.node.async
1233
- )
1234
- ),
1235
- func.target,
1236
- func.idPrefix
1237
- );
1238
- const source = generateUniqueName(path2, "source");
1239
- const rest = generateUniqueName(path2, "rest");
1240
- return t11.arrowFunctionExpression(
1241
- [],
1242
- t11.blockStatement([
1243
- t11.variableDeclaration("const", [
1244
- t11.variableDeclarator(
1245
- source,
1246
- t11.memberExpression(
1247
- t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1248
- t11.identifier("default")
1249
- )
1250
- )
1251
- ]),
1252
- t11.returnStatement(
1253
- t11.arrowFunctionExpression(
1254
- [t11.restElement(rest)],
1255
- t11.callExpression(source, [
1256
- t11.arrayExpression(bindings.locals),
1257
- t11.spreadElement(rest)
1258
- ]),
1259
- path2.node.async
1260
- )
1261
- )
1262
- ]),
1263
- true
1264
- );
1265
- }
1266
- function replaceFunctionFromCall(ctx, path2, func, bindings) {
1267
- const body = path2.get("body");
1268
- if (isPathValid(body, t11.isExpression)) {
1269
- body.replaceWith(t11.blockStatement([t11.returnStatement(body.node)]));
1270
- }
1271
- assert(isPathValid(body, t11.isBlockStatement), "invariant");
1272
- transformFunctionContent(body, bindings.mutations);
1273
- const entryFile = createEntryFile(
1274
- ctx,
1275
- path2,
1276
- ctx.options.mode === "server" || func.isomorphic ? createRootFile(
1277
- ctx,
1278
- bindings,
1279
- t11.isFunctionExpression(path2.node) ? t11.functionExpression(
1280
- path2.node.id,
1281
- [t11.arrayPattern(bindings.locals), ...path2.node.params],
1282
- path2.node.body,
1283
- path2.node.generator,
1284
- path2.node.async
1285
- ) : t11.arrowFunctionExpression(
1286
- [t11.arrayPattern(bindings.locals), ...path2.node.params],
1287
- path2.node.body,
1288
- path2.node.async
1289
- )
1290
- ) : void 0,
1291
- func.target,
1292
- func.idPrefix
1293
- );
1294
- return getFunctionReplacement(ctx, path2, entryFile, bindings);
1295
- }
1296
- function splitFunctionFromCall(ctx, path2, func) {
1297
- const bindings = extractBindings(
1298
- ctx,
1299
- path2,
1300
- getForeignBindings(path2, "function"),
1301
- func.pure
1302
- );
1303
- if (func.isomorphic) {
1304
- return replaceIsomorphicFunction(ctx, path2, func, bindings);
1305
- }
1306
- return replaceFunctionFromCall(ctx, path2, func, bindings);
1307
- }
1308
- function replaceExpressionFromCall(ctx, path2, func, bindings) {
1340
+ // src/transform-call.ts
1341
+ var t10 = __toESM(require("@babel/types"), 1);
1342
+
1343
+ // src/split-function.ts
1344
+ function replaceFunction(ctx, path2, definition, dependencies) {
1345
+ const statements = getModuleImports(dependencies.modules);
1346
+ statements.push(transformRootFunction(path2, dependencies));
1309
1347
  const entryFile = createEntryFile(
1310
1348
  ctx,
1349
+ path2.node.generator ? "generator" : "function",
1311
1350
  path2,
1312
- ctx.options.mode === "server" || func.isomorphic ? createRootFile(ctx, bindings, path2.node) : void 0,
1313
- func.target,
1314
- func.idPrefix
1315
- );
1316
- const rest = generateUniqueName(path2, "rest");
1317
- const source = generateUniqueName(path2, "source");
1318
- return t11.arrowFunctionExpression(
1319
- [],
1320
- t11.blockStatement([
1321
- t11.variableDeclaration("const", [
1322
- t11.variableDeclarator(
1323
- source,
1324
- t11.memberExpression(
1325
- t11.awaitExpression(t11.importExpression(t11.stringLiteral(entryFile))),
1326
- t11.identifier("default")
1327
- )
1328
- )
1329
- ]),
1330
- t11.returnStatement(
1331
- t11.arrowFunctionExpression(
1332
- [t11.restElement(rest)],
1333
- t11.callExpression(source, [t11.spreadElement(rest)]),
1334
- true
1335
- )
1336
- )
1337
- ]),
1338
- true
1351
+ ctx.options.mode === "server" ? createRootFile(ctx, statements) : void 0,
1352
+ definition.target,
1353
+ definition.idPrefix
1339
1354
  );
1355
+ return getFunctionReplacement(ctx, path2, entryFile, dependencies);
1340
1356
  }
1341
- function splitExpressionFromCall(ctx, path2, func) {
1342
- return replaceExpressionFromCall(
1357
+ function splitFunction(ctx, path2, definition) {
1358
+ return replaceFunction(
1343
1359
  ctx,
1344
1360
  path2,
1345
- func,
1346
- extractBindings(
1347
- ctx,
1361
+ definition,
1362
+ getBindingDependencies(
1348
1363
  path2,
1349
- getForeignBindings(path2, "expression"),
1350
- func.pure
1364
+ getForeignBindings(path2, "function"),
1365
+ !!definition.pure
1351
1366
  )
1352
1367
  );
1353
1368
  }
1354
1369
 
1370
+ // src/utils/is-valid-function.ts
1371
+ var t9 = __toESM(require("@babel/types"), 1);
1372
+ function isValidFunction(node) {
1373
+ return t9.isArrowFunctionExpression(node) || t9.isFunctionExpression(node);
1374
+ }
1375
+
1355
1376
  // src/transform-call.ts
1356
1377
  function getFunctionDefinitionFromPropName(definitions, propName) {
1357
1378
  for (let i = 0, len = definitions.length; i < len; i++) {
@@ -1367,7 +1388,7 @@ function getFunctionDefinitionFromPropName(definitions, propName) {
1367
1388
  }
1368
1389
  function getFunctionDefinitionFromCallee(ctx, path2) {
1369
1390
  const callee = path2.node.callee;
1370
- const id = unwrapNode(callee, t12.isIdentifier);
1391
+ const id = unwrapNode(callee, t10.isIdentifier);
1371
1392
  if (id) {
1372
1393
  const binding = path2.scope.getBindingIdentifier(id.name);
1373
1394
  if (binding) {
@@ -1375,9 +1396,9 @@ function getFunctionDefinitionFromCallee(ctx, path2) {
1375
1396
  }
1376
1397
  return void 0;
1377
1398
  }
1378
- const memberExpr = unwrapNode(callee, t12.isMemberExpression);
1379
- if (memberExpr && !memberExpr.computed && t12.isIdentifier(memberExpr.property)) {
1380
- const object = unwrapNode(memberExpr.object, t12.isIdentifier);
1399
+ const memberExpr = unwrapNode(callee, t10.isMemberExpression);
1400
+ if (memberExpr && !memberExpr.computed && t10.isIdentifier(memberExpr.property)) {
1401
+ const object = unwrapNode(memberExpr.object, t10.isIdentifier);
1381
1402
  if (object) {
1382
1403
  const binding = path2.scope.getBindingIdentifier(object.name);
1383
1404
  if (binding) {
@@ -1393,19 +1414,6 @@ function getFunctionDefinitionFromCallee(ctx, path2) {
1393
1414
  }
1394
1415
  return void 0;
1395
1416
  }
1396
- function isValidFunction(node) {
1397
- return t12.isArrowFunctionExpression(node) || t12.isFunctionExpression(node);
1398
- }
1399
- function isSkippableFunction(node) {
1400
- if (node.leadingComments) {
1401
- for (let i = 0, len = node.leadingComments.length; i < len; i++) {
1402
- if (/^@dismantle skip$/.test(node.leadingComments[i].value)) {
1403
- return true;
1404
- }
1405
- }
1406
- }
1407
- return false;
1408
- }
1409
1417
  function transformCall(ctx, path2) {
1410
1418
  const definition = getFunctionDefinitionFromCallee(ctx, path2);
1411
1419
  if (!definition) {
@@ -1413,369 +1421,107 @@ function transformCall(ctx, path2) {
1413
1421
  }
1414
1422
  const args = path2.get("arguments");
1415
1423
  const expr = args[0];
1416
- if (isPathValid(expr, t12.isExpression) && !isSkippableFunction(expr.node)) {
1417
- const replacement = isPathValid(expr, isValidFunction) ? splitFunctionFromCall(ctx, expr, definition) : splitExpressionFromCall(ctx, expr, definition);
1424
+ if (isPathValid(expr, isValidFunction)) {
1425
+ const replacement = splitFunction(ctx, expr, definition);
1426
+ path2.scope.crawl();
1418
1427
  path2.replaceWith(
1419
- t12.callExpression(getImportIdentifier(ctx, path2, definition.handle), [
1420
- t12.addComment(replacement, "leading", "@dismantle skip")
1421
- ])
1428
+ t10.callExpression(
1429
+ getImportIdentifier(ctx.imports, path2, definition.handle),
1430
+ [replacement]
1431
+ )
1422
1432
  );
1423
1433
  }
1424
1434
  }
1425
1435
 
1426
1436
  // src/transform-function-directive.ts
1427
- var t14 = __toESM(require("@babel/types"), 1);
1428
-
1429
- // src/split-function-directive.ts
1430
- var t13 = __toESM(require("@babel/types"), 1);
1431
- function transformFunctionContent2(path2, mutations) {
1432
- const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
1433
- const applyMutations = mutations.length ? generateUniqueName(path2, "mutate") : void 0;
1434
- path2.traverse({
1435
- ReturnStatement(child) {
1436
- const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
1437
- if (parent === target) {
1438
- const replacement = [RETURN_KEY];
1439
- if (child.node.argument) {
1440
- replacement.push(child.node.argument);
1441
- } else {
1442
- replacement.push(t13.nullLiteral());
1443
- }
1444
- if (applyMutations) {
1445
- replacement.push(t13.callExpression(applyMutations, []));
1446
- }
1447
- child.replaceWith(t13.returnStatement(t13.arrayExpression(replacement)));
1448
- child.skip();
1449
- }
1450
- }
1451
- });
1452
- const error = generateUniqueName(path2, "error");
1453
- const throwResult = [THROW_KEY, error];
1454
- const haltResult = [NO_HALT_KEY];
1455
- if (applyMutations) {
1456
- throwResult.push(t13.callExpression(applyMutations, []));
1457
- haltResult.push(t13.nullLiteral());
1458
- haltResult.push(t13.callExpression(applyMutations, []));
1459
- }
1460
- const statements = [
1461
- t13.tryStatement(
1462
- t13.blockStatement(path2.node.body),
1463
- t13.catchClause(
1464
- error,
1465
- t13.blockStatement([t13.returnStatement(t13.arrayExpression(throwResult))])
1466
- )
1467
- ),
1468
- t13.returnStatement(t13.arrayExpression(haltResult))
1469
- ];
1470
- if (applyMutations) {
1471
- statements.unshift(
1472
- t13.variableDeclaration("const", [
1473
- t13.variableDeclarator(
1474
- applyMutations,
1475
- t13.arrowFunctionExpression(
1476
- [],
1477
- t13.objectExpression(
1478
- mutations.map((item) => t13.objectProperty(item, item, false, true))
1479
- )
1480
- )
1481
- )
1482
- ])
1483
- );
1484
- }
1485
- path2.node.body = statements;
1486
- }
1487
- function getFunctionReplacement2(ctx, path2, entryFile, bindings) {
1488
- const rest = generateUniqueName(path2, "rest");
1489
- const returnType = generateUniqueName(path2, "type");
1490
- const returnResult = generateUniqueName(path2, "result");
1491
- const returnMutations = generateUniqueName(path2, "mutations");
1492
- const source = generateUniqueName(path2, "source");
1493
- const replacement = [];
1494
- if (path2.node.generator) {
1495
- const funcID = generateUniqueName(path2, "fn");
1496
- replacement.push(
1497
- t13.variableDeclaration("const", [
1498
- t13.variableDeclarator(
1499
- funcID,
1500
- t13.callExpression(
1501
- getImportIdentifier(ctx, path2, {
1502
- ...HIDDEN_GENERATOR,
1503
- source: ctx.options.runtime
1504
- }),
1505
- [
1506
- source,
1507
- bindings.mutations.length ? t13.arrowFunctionExpression(
1508
- [returnMutations],
1509
- t13.assignmentExpression(
1510
- "=",
1511
- t13.objectPattern(
1512
- bindings.mutations.map(
1513
- (item) => t13.objectProperty(item, item, false, true)
1514
- )
1515
- ),
1516
- returnMutations
1517
- )
1518
- ) : t13.nullLiteral()
1519
- ]
1520
- )
1521
- )
1522
- ])
1523
- );
1524
- const [reps, step] = getGeneratorReplacementForBlock(path2, funcID, [
1525
- t13.arrayExpression(bindings.locals),
1526
- t13.spreadElement(rest)
1527
- ]);
1528
- for (let i = 0, len = reps.length; i < len; i++) {
1529
- replacement.push(reps[i]);
1530
- }
1531
- replacement.push(
1532
- t13.variableDeclaration("const", [
1533
- t13.variableDeclarator(
1534
- t13.arrayPattern([returnType, returnResult]),
1535
- t13.memberExpression(step, t13.identifier("value"))
1536
- )
1537
- ])
1538
- );
1539
- } else {
1540
- replacement.push(
1541
- t13.variableDeclaration("const", [
1542
- t13.variableDeclarator(
1543
- t13.arrayPattern([returnType, returnResult]),
1544
- t13.awaitExpression(
1545
- t13.callExpression(
1546
- t13.callExpression(
1547
- getImportIdentifier(ctx, path2, {
1548
- ...HIDDEN_FUNC,
1549
- source: ctx.options.runtime
1550
- }),
1551
- [
1552
- source,
1553
- bindings.mutations.length ? t13.arrowFunctionExpression(
1554
- [returnMutations],
1555
- t13.assignmentExpression(
1556
- "=",
1557
- t13.objectPattern(
1558
- bindings.mutations.map(
1559
- (item) => t13.objectProperty(item, item, false, true)
1560
- )
1561
- ),
1562
- returnMutations
1563
- )
1564
- ) : t13.nullLiteral()
1565
- ]
1566
- ),
1567
- [t13.arrayExpression(bindings.locals), t13.spreadElement(rest)]
1568
- )
1569
- )
1570
- )
1571
- ])
1572
- );
1573
- }
1574
- replacement.push(t13.returnStatement(returnResult));
1575
- return t13.arrowFunctionExpression(
1576
- [],
1577
- t13.blockStatement([
1578
- t13.variableDeclaration("const", [
1579
- t13.variableDeclarator(
1580
- source,
1581
- t13.memberExpression(
1582
- t13.awaitExpression(t13.importExpression(t13.stringLiteral(entryFile))),
1583
- t13.identifier("default")
1584
- )
1585
- )
1586
- ]),
1587
- t13.returnStatement(
1588
- isPathValid(path2, t13.isFunctionExpression) ? t13.functionExpression(
1589
- path2.node.id,
1590
- [t13.restElement(rest)],
1591
- t13.blockStatement(replacement),
1592
- path2.node.generator,
1593
- true
1594
- ) : t13.arrowFunctionExpression(
1595
- [t13.restElement(rest)],
1596
- t13.blockStatement(replacement),
1597
- true
1598
- )
1599
- )
1600
- ]),
1601
- true
1602
- );
1603
- }
1604
- function replaceIsomorphicFunction2(ctx, path2, directive, bindings) {
1605
- const body = path2.get("body");
1606
- if (isPathValid(body, t13.isExpression)) {
1607
- body.replaceWith(t13.blockStatement([t13.returnStatement(body.node)]));
1608
- }
1609
- assert(isPathValid(body, t13.isBlockStatement), "invariant");
1610
- const entryFile = createEntryFile(
1437
+ var t11 = __toESM(require("@babel/types"), 1);
1438
+ function getFunctionDirectiveDefinition(ctx, path2) {
1439
+ const definition = getDefinitionFromDirectives(
1611
1440
  ctx,
1612
- path2,
1613
- createRootFile(
1614
- ctx,
1615
- bindings,
1616
- t13.isFunctionExpression(path2.node) ? t13.functionExpression(
1617
- path2.node.id,
1618
- [t13.arrayPattern(bindings.locals), ...path2.node.params],
1619
- path2.node.body,
1620
- path2.node.generator,
1621
- path2.node.async
1622
- ) : t13.arrowFunctionExpression(
1623
- [t13.arrayPattern(bindings.locals), ...path2.node.params],
1624
- path2.node.body,
1625
- path2.node.async
1626
- )
1627
- ),
1628
- directive.target,
1629
- directive.idPrefix
1630
- );
1631
- const source = generateUniqueName(path2, "source");
1632
- const rest = generateUniqueName(path2, "rest");
1633
- return t13.arrowFunctionExpression(
1634
- [],
1635
- t13.blockStatement([
1636
- t13.variableDeclaration("const", [
1637
- t13.variableDeclarator(
1638
- source,
1639
- t13.memberExpression(
1640
- t13.awaitExpression(t13.importExpression(t13.stringLiteral(entryFile))),
1641
- t13.identifier("default")
1642
- )
1643
- )
1644
- ]),
1645
- t13.returnStatement(
1646
- t13.arrowFunctionExpression(
1647
- [t13.restElement(rest)],
1648
- t13.callExpression(source, [
1649
- t13.arrayExpression(bindings.locals),
1650
- t13.spreadElement(rest)
1651
- ]),
1652
- path2.node.async
1653
- )
1654
- )
1655
- ]),
1656
- true
1441
+ "function-directive",
1442
+ path2
1657
1443
  );
1658
- }
1659
- function replaceFunctionDirective(ctx, path2, directive, bindings) {
1660
- const body = path2.get("body");
1661
- if (isPathValid(body, t13.isExpression)) {
1662
- body.replaceWith(t13.blockStatement([t13.returnStatement(body.node)]));
1444
+ if (definition) {
1445
+ cleanDirectives(path2, definition);
1446
+ return definition;
1663
1447
  }
1664
- assert(isPathValid(body, t13.isBlockStatement), "invariant");
1665
- transformFunctionContent2(body, bindings.mutations);
1666
- const entryFile = createEntryFile(
1667
- ctx,
1668
- path2,
1669
- ctx.options.mode === "server" || directive.isomorphic ? createRootFile(
1670
- ctx,
1671
- bindings,
1672
- t13.isFunctionExpression(path2.node) ? t13.functionExpression(
1673
- path2.node.id,
1674
- [t13.arrayPattern(bindings.locals), ...path2.node.params],
1675
- path2.node.body,
1676
- path2.node.generator,
1677
- path2.node.async
1678
- ) : t13.arrowFunctionExpression(
1679
- [t13.arrayPattern(bindings.locals), ...path2.node.params],
1680
- path2.node.body,
1681
- path2.node.async
1682
- )
1683
- ) : void 0,
1684
- directive.target,
1685
- directive.idPrefix
1686
- );
1687
- return getFunctionReplacement2(ctx, path2, entryFile, bindings);
1688
- }
1689
- function splitFunctionDirective(ctx, path2, directive) {
1690
- const bindings = extractBindings(
1448
+ const fauxDefinition = getDefinitionFromFauxDirectives(
1691
1449
  ctx,
1692
- path2,
1693
- getForeignBindings(path2, "function"),
1694
- directive.pure
1450
+ "function-directive",
1451
+ path2
1695
1452
  );
1696
- if (directive.isomorphic) {
1697
- return replaceIsomorphicFunction2(ctx, path2, directive, bindings);
1453
+ if (fauxDefinition) {
1454
+ cleanFauxDirectives(path2, fauxDefinition);
1455
+ return fauxDefinition;
1698
1456
  }
1699
- return replaceFunctionDirective(ctx, path2, directive, bindings);
1457
+ return void 0;
1700
1458
  }
1701
-
1702
- // src/transform-function-directive.ts
1703
1459
  function transformFunctionDirective(ctx, path2) {
1704
1460
  const body = path2.get("body");
1705
- if (isPathValid(body, t14.isBlockStatement)) {
1706
- const definition = getDirectiveDefinitionFromBlock(ctx, body);
1707
- if (!definition || definition.type !== "function-directive") {
1708
- return;
1461
+ if (isPathValid(body, t11.isBlockStatement)) {
1462
+ const definition = getFunctionDirectiveDefinition(ctx, body);
1463
+ if (definition) {
1464
+ const replacement = splitFunction(ctx, path2, definition);
1465
+ path2.scope.crawl();
1466
+ path2.replaceWith(
1467
+ t11.callExpression(
1468
+ getImportIdentifier(ctx.imports, path2, definition.handle),
1469
+ [replacement]
1470
+ )
1471
+ );
1709
1472
  }
1710
- cleanBlockForDirectives(body, definition);
1711
- cleanBlockForFauxDirectives(body, definition);
1712
- const replacement = splitFunctionDirective(ctx, path2, definition);
1713
- path2.replaceWith(
1714
- t14.callExpression(getImportIdentifier(ctx, path2, definition.handle), [
1715
- replacement
1716
- ])
1717
- );
1718
- }
1719
- }
1720
-
1721
- // src/utils/bubble-function-declaration.ts
1722
- var t15 = __toESM(require("@babel/types"), 1);
1723
-
1724
- // src/utils/is-statement-top-level.ts
1725
- function isStatementTopLevel(path2) {
1726
- let blockParent = path2.scope.getBlockParent();
1727
- const programParent = path2.scope.getProgramParent();
1728
- if (blockParent.path === path2) {
1729
- blockParent = blockParent.parent;
1730
1473
  }
1731
- return programParent === blockParent;
1732
1474
  }
1733
1475
 
1734
1476
  // src/utils/bubble-function-declaration.ts
1735
- function bubbleFunctionDeclaration(ctx, program2, path2) {
1736
- if (isStatementTopLevel(path2)) {
1737
- const decl = path2.node;
1738
- if (decl.id) {
1739
- const definition = getDirectiveDefinitionFromBlock(ctx, path2.get("body"));
1740
- if (!definition || definition.type !== "function-directive") {
1741
- return;
1742
- }
1743
- const [tmp] = program2.unshiftContainer(
1744
- "body",
1745
- t15.variableDeclaration("const", [
1746
- t15.variableDeclarator(
1477
+ var t12 = __toESM(require("@babel/types"), 1);
1478
+ function bubbleFunctionDeclaration(ctx, path2) {
1479
+ const decl = path2.node;
1480
+ if (decl.id) {
1481
+ const definition = getDefinitionFromDirectives(
1482
+ ctx,
1483
+ "function-directive",
1484
+ path2.get("body")
1485
+ );
1486
+ if (!definition) {
1487
+ return;
1488
+ }
1489
+ const block = path2.findParent((current) => current.isBlockStatement()) || path2.scope.getProgramParent().path;
1490
+ const [tmp] = block.unshiftContainer(
1491
+ "body",
1492
+ t12.variableDeclaration("const", [
1493
+ t12.variableDeclarator(
1494
+ decl.id,
1495
+ t12.functionExpression(
1747
1496
  decl.id,
1748
- t15.functionExpression(
1749
- decl.id,
1750
- decl.params,
1751
- decl.body,
1752
- decl.generator,
1753
- decl.async
1754
- )
1497
+ decl.params,
1498
+ decl.body,
1499
+ decl.generator,
1500
+ decl.async
1755
1501
  )
1502
+ )
1503
+ ])
1504
+ );
1505
+ path2.scope.registerDeclaration(tmp);
1506
+ tmp.skip();
1507
+ if (path2.parentPath.isExportNamedDeclaration()) {
1508
+ path2.parentPath.replaceWith(
1509
+ t12.exportNamedDeclaration(void 0, [
1510
+ t12.exportSpecifier(decl.id, decl.id)
1756
1511
  ])
1757
1512
  );
1758
- program2.scope.registerDeclaration(tmp);
1759
- tmp.skip();
1760
- if (path2.parentPath.isExportNamedDeclaration()) {
1761
- path2.parentPath.replaceWith(
1762
- t15.exportNamedDeclaration(void 0, [
1763
- t15.exportSpecifier(decl.id, decl.id)
1764
- ])
1765
- );
1766
- } else if (path2.parentPath.isExportDefaultDeclaration()) {
1767
- path2.replaceWith(decl.id);
1768
- } else {
1769
- path2.remove();
1770
- }
1513
+ } else if (path2.parentPath.isExportDefaultDeclaration()) {
1514
+ path2.replaceWith(decl.id);
1515
+ } else {
1516
+ path2.remove();
1771
1517
  }
1772
1518
  }
1773
1519
  }
1774
1520
 
1775
1521
  // src/utils/register-import-specifiers.ts
1776
- var t16 = __toESM(require("@babel/types"), 1);
1522
+ var t13 = __toESM(require("@babel/types"), 1);
1777
1523
  function registerImportSpecifier(ctx, node, definition) {
1778
- if (t16.isImportSpecifier(node)) {
1524
+ if (t13.isImportSpecifier(node)) {
1779
1525
  if (node.importKind === "type" || node.importKind === "typeof") {
1780
1526
  return;
1781
1527
  }
@@ -1784,10 +1530,10 @@ function registerImportSpecifier(ctx, node, definition) {
1784
1530
  ctx.registrations.identifiers.set(node.local, definition);
1785
1531
  }
1786
1532
  }
1787
- if (t16.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
1533
+ if (t13.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
1788
1534
  ctx.registrations.identifiers.set(node.local, definition);
1789
1535
  }
1790
- if (t16.isImportNamespaceSpecifier(node)) {
1536
+ if (t13.isImportNamespaceSpecifier(node)) {
1791
1537
  let current = ctx.registrations.namespaces.get(node.local);
1792
1538
  if (!current) {
1793
1539
  current = [];
@@ -1823,32 +1569,52 @@ function registerImportSpecifiers(ctx, programPath) {
1823
1569
  }
1824
1570
 
1825
1571
  // src/plugin.ts
1826
- function plugin() {
1827
- return {
1828
- name: "dismantle",
1829
- visitor: {
1830
- Program(programPath, ctx) {
1831
- registerImportSpecifiers(ctx.opts, programPath);
1832
- programPath.traverse({
1833
- FunctionDeclaration(path2) {
1834
- bubbleFunctionDeclaration(ctx.opts, programPath, path2);
1835
- }
1836
- });
1837
- },
1838
- ArrowFunctionExpression(path2, ctx) {
1572
+ var FUNCTION_BUBBLE = {
1573
+ FunctionDeclaration(path2, ctx) {
1574
+ bubbleFunctionDeclaration(ctx.opts, path2);
1575
+ }
1576
+ };
1577
+ var PLUGIN = {
1578
+ name: "dismantle",
1579
+ visitor: {
1580
+ Program: {
1581
+ enter(program2, ctx) {
1582
+ registerImportSpecifiers(ctx.opts, program2);
1583
+ program2.traverse(FUNCTION_BUBBLE, ctx);
1584
+ program2.scope.crawl();
1585
+ }
1586
+ },
1587
+ ArrowFunctionExpression: {
1588
+ exit(path2, ctx) {
1839
1589
  transformFunctionDirective(ctx.opts, path2);
1840
- },
1841
- FunctionExpression(path2, ctx) {
1590
+ }
1591
+ },
1592
+ FunctionExpression: {
1593
+ exit(path2, ctx) {
1842
1594
  transformFunctionDirective(ctx.opts, path2);
1843
- },
1844
- BlockStatement(path2, ctx) {
1595
+ }
1596
+ },
1597
+ BlockStatement: {
1598
+ exit(path2, ctx) {
1845
1599
  transformBlockDirective(ctx.opts, path2);
1846
- },
1847
- CallExpression(path2, ctx) {
1600
+ }
1601
+ },
1602
+ CallExpression: {
1603
+ exit(path2, ctx) {
1848
1604
  transformCall(ctx.opts, path2);
1849
1605
  }
1850
1606
  }
1851
- };
1607
+ }
1608
+ };
1609
+ function plugin() {
1610
+ return PLUGIN;
1611
+ }
1612
+
1613
+ // src/utils/assert.ts
1614
+ function assert(cond, error) {
1615
+ if (!cond) {
1616
+ throw new Error(error);
1617
+ }
1852
1618
  }
1853
1619
 
1854
1620
  // src/utils/xxhash32.ts