async-browser-context 0.2.3 → 0.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.
Files changed (140) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +45 -30
  3. package/dist/babel/async-generator-helpers.d.ts +6 -6
  4. package/dist/babel/async-generator-helpers.d.ts.map +1 -1
  5. package/dist/babel/async-generator-helpers.js +33 -22
  6. package/dist/babel/async-generator-helpers.js.map +1 -1
  7. package/dist/babel/async-generator-resources.d.ts +14 -0
  8. package/dist/babel/async-generator-resources.d.ts.map +1 -0
  9. package/dist/babel/async-generator-resources.js +38 -0
  10. package/dist/babel/async-generator-resources.js.map +1 -0
  11. package/dist/babel/bind-generators.d.ts +1 -1
  12. package/dist/babel/bind-generators.d.ts.map +1 -1
  13. package/dist/babel/bind-generators.js +88 -44
  14. package/dist/babel/bind-generators.js.map +1 -1
  15. package/dist/babel/name-coroutines.d.ts +14 -8
  16. package/dist/babel/name-coroutines.d.ts.map +1 -1
  17. package/dist/babel/name-coroutines.js +160 -39
  18. package/dist/babel/name-coroutines.js.map +1 -1
  19. package/dist/babel/preset.d.ts +24 -13
  20. package/dist/babel/preset.d.ts.map +1 -1
  21. package/dist/babel/preset.js +37 -9
  22. package/dist/babel/preset.js.map +1 -1
  23. package/dist/babel/runtime-calls.d.ts +22 -0
  24. package/dist/babel/runtime-calls.d.ts.map +1 -0
  25. package/dist/babel/runtime-calls.js +69 -0
  26. package/dist/babel/runtime-calls.js.map +1 -0
  27. package/dist/babel/skip-runtime.d.ts +18 -11
  28. package/dist/babel/skip-runtime.d.ts.map +1 -1
  29. package/dist/babel/skip-runtime.js +52 -16
  30. package/dist/babel/skip-runtime.js.map +1 -1
  31. package/dist/babel/static-members.d.ts +4 -0
  32. package/dist/babel/static-members.d.ts.map +1 -0
  33. package/dist/babel/static-members.js +112 -0
  34. package/dist/babel/static-members.js.map +1 -0
  35. package/dist/core/async-generator.d.ts +51 -13
  36. package/dist/core/async-generator.d.ts.map +1 -1
  37. package/dist/core/async-generator.js +255 -58
  38. package/dist/core/async-generator.js.map +1 -1
  39. package/dist/core/async-local-storage.d.ts +3 -0
  40. package/dist/core/async-local-storage.d.ts.map +1 -1
  41. package/dist/core/async-local-storage.js +3 -0
  42. package/dist/core/async-local-storage.js.map +1 -1
  43. package/dist/core/callback-hook.d.ts +3 -3
  44. package/dist/core/coroutine.d.ts +6 -2
  45. package/dist/core/coroutine.d.ts.map +1 -1
  46. package/dist/core/coroutine.js +106 -44
  47. package/dist/core/coroutine.js.map +1 -1
  48. package/dist/core/function-name.d.ts +9 -0
  49. package/dist/core/function-name.d.ts.map +1 -0
  50. package/dist/core/function-name.js +12 -0
  51. package/dist/core/function-name.js.map +1 -0
  52. package/dist/core/hooks.d.ts +2 -1
  53. package/dist/core/hooks.d.ts.map +1 -1
  54. package/dist/core/hooks.js +21 -6
  55. package/dist/core/hooks.js.map +1 -1
  56. package/dist/core/snapshot.d.ts +2 -1
  57. package/dist/core/snapshot.d.ts.map +1 -1
  58. package/dist/core/snapshot.js +7 -3
  59. package/dist/core/snapshot.js.map +1 -1
  60. package/dist/core/store.d.ts +28 -0
  61. package/dist/core/store.d.ts.map +1 -1
  62. package/dist/core/store.js +90 -20
  63. package/dist/core/store.js.map +1 -1
  64. package/dist/core/variable.d.ts +2 -1
  65. package/dist/core/variable.d.ts.map +1 -1
  66. package/dist/core/variable.js +5 -2
  67. package/dist/core/variable.js.map +1 -1
  68. package/dist/node/index.d.ts +10 -4
  69. package/dist/node/index.d.ts.map +1 -1
  70. package/dist/node/index.js +51 -16
  71. package/dist/node/index.js.map +1 -1
  72. package/dist/node/runtime.d.ts +7 -6
  73. package/dist/node/runtime.d.ts.map +1 -1
  74. package/dist/node/runtime.js +29 -16
  75. package/dist/node/runtime.js.map +1 -1
  76. package/dist/opentelemetry/context-manager.d.ts.map +1 -1
  77. package/dist/opentelemetry/context-manager.js +172 -13
  78. package/dist/opentelemetry/context-manager.js.map +1 -1
  79. package/dist/patches/callbacks.d.ts.map +1 -1
  80. package/dist/patches/callbacks.js +84 -29
  81. package/dist/patches/callbacks.js.map +1 -1
  82. package/dist/patches/events.d.ts.map +1 -1
  83. package/dist/patches/events.js +203 -91
  84. package/dist/patches/events.js.map +1 -1
  85. package/dist/patches/handler-interfaces.d.ts +3 -2
  86. package/dist/patches/handler-interfaces.d.ts.map +1 -1
  87. package/dist/patches/handler-interfaces.js +3 -2
  88. package/dist/patches/handler-interfaces.js.map +1 -1
  89. package/dist/patches/promise.d.ts +3 -0
  90. package/dist/patches/promise.d.ts.map +1 -1
  91. package/dist/patches/promise.js +13 -5
  92. package/dist/patches/promise.js.map +1 -1
  93. package/dist/patches/streams.d.ts.map +1 -1
  94. package/dist/patches/streams.js +36 -16
  95. package/dist/patches/streams.js.map +1 -1
  96. package/dist/patches/timers.d.ts.map +1 -1
  97. package/dist/patches/timers.js +17 -5
  98. package/dist/patches/timers.js.map +1 -1
  99. package/dist/patches/to-string.d.ts +4 -0
  100. package/dist/patches/to-string.d.ts.map +1 -1
  101. package/dist/patches/to-string.js +12 -6
  102. package/dist/patches/to-string.js.map +1 -1
  103. package/dist/runtime.d.ts +3 -3
  104. package/dist/runtime.d.ts.map +1 -1
  105. package/dist/runtime.js +3 -3
  106. package/dist/runtime.js.map +1 -1
  107. package/dist/vite/plugin.d.ts +7 -2
  108. package/dist/vite/plugin.d.ts.map +1 -1
  109. package/dist/vite/plugin.js +30 -6
  110. package/dist/vite/plugin.js.map +1 -1
  111. package/package.json +49 -25
  112. package/src/babel/async-generator-helpers.ts +34 -23
  113. package/src/babel/async-generator-resources.ts +40 -0
  114. package/src/babel/bind-generators.ts +91 -47
  115. package/src/babel/name-coroutines.ts +170 -41
  116. package/src/babel/preset.ts +47 -15
  117. package/src/babel/runtime-calls.ts +73 -0
  118. package/src/babel/skip-runtime.ts +58 -18
  119. package/src/babel/static-members.ts +117 -0
  120. package/src/core/async-generator.ts +273 -62
  121. package/src/core/async-local-storage.ts +3 -0
  122. package/src/core/callback-hook.ts +3 -3
  123. package/src/core/coroutine.ts +104 -43
  124. package/src/core/function-name.ts +11 -0
  125. package/src/core/hooks.ts +23 -6
  126. package/src/core/snapshot.ts +8 -3
  127. package/src/core/store.ts +107 -21
  128. package/src/core/variable.ts +6 -2
  129. package/src/node/index.ts +62 -19
  130. package/src/node/runtime.ts +33 -15
  131. package/src/opentelemetry/context-manager.ts +188 -13
  132. package/src/patches/callbacks.ts +87 -30
  133. package/src/patches/events.ts +249 -103
  134. package/src/patches/handler-interfaces.ts +3 -2
  135. package/src/patches/promise.ts +17 -13
  136. package/src/patches/streams.ts +36 -16
  137. package/src/patches/timers.ts +17 -5
  138. package/src/patches/to-string.ts +12 -6
  139. package/src/runtime.ts +3 -3
  140. package/src/vite/plugin.ts +38 -8
@@ -3,15 +3,16 @@ export { METADATA_KEY } from "./bind-generators.js";
3
3
  /** The options of the preset. */
4
4
  export interface PresetOptions {
5
5
  /**
6
- * The module that exports the runtime functions `coroutine` and
7
- * `bindGenerator`. The default is `async-browser-context/runtime`.
6
+ * The module that exports the six names of the runtime: `coroutine`,
7
+ * `bindGenerator`, `asyncGenerator`, `OverloadYield`, `awaitAsyncGenerator`
8
+ * and `asyncGeneratorDelegate`. The default is `async-browser-context/runtime`.
8
9
  */
9
10
  readonly runtime?: string | undefined;
10
11
  /**
11
- * Give the name of each async function to `coroutine`, for the callback
12
- * hooks. The default is `true`. With `false`, the hooks get no name for the
13
- * steps after an `await`. The code is then smaller by 0.1 to 1.6 percent
14
- * of its gzip size (`pnpm bench:size`).
12
+ * Give the name of each async function and each async generator to the
13
+ * runtime, for the callback hooks. The default is `true`. With `false`, the
14
+ * hooks get no name for the steps after an `await`. The code is then smaller
15
+ * by 0.1 to 1.8 percent of its gzip size (`pnpm bench:size`).
15
16
  */
16
17
  readonly names?: boolean | undefined;
17
18
  }
@@ -27,17 +28,27 @@ export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
27
28
  * 2. A plugin of this library binds each sync generator to the context of
28
29
  * its creation.
29
30
  * 3. A plugin of this library gives the name of each async function to
30
- * `coroutine`, for the callback hooks. The option `names: false` removes
31
- * this step.
32
- * 4. A plugin of this library gives the async generators to `asyncGenerator`
33
- * of the runtime, in place of the Babel helper.
34
- * 5. `@babel/plugin-transform-async-generator-functions` changes async
35
- * generators and `for await` loops.
36
- * 6. `@babel/plugin-transform-async-to-generator` changes each async function
31
+ * `coroutine`, and of each async generator to `asyncGenerator`, for the
32
+ * callback hooks. The option `names: false` removes this step.
33
+ * 4. A plugin of this library moves the static class members with async arrow
34
+ * functions into static private methods. Thus `this` and `super` stay
35
+ * correct (static-members.ts).
36
+ * 5. A plugin of this library gives the async generators to the helpers of the
37
+ * runtime, in place of the Babel helpers.
38
+ * 6. `@babel/plugin-transform-explicit-resource-management` changes `using` and
39
+ * `await using` declarations, with Babel 7.23.9 and later.
40
+ * 7. A plugin of this library starts the plugin of step 6 on each async
41
+ * generator, before step 8 changes it (async-generator-resources.ts).
42
+ * 8. `@babel/plugin-transform-async-generator-functions` changes async
43
+ * generators and `for await` loops. Its own traversal also skips the
44
+ * functions of the runtime (skip-runtime.ts).
45
+ * 9. `@babel/plugin-transform-async-to-generator` changes each async function
37
46
  * into a generator that the `coroutine` function of the runtime operates.
38
47
  *
39
48
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
40
49
  * in the `presets` list: Babel uses the presets in reverse order.
41
50
  */
42
51
  export default function asyncBrowserContextPreset(api: PresetAPI, options?: PresetOptions): PresetObject;
52
+ /** This function gives `true` for a version of Babel that the plugin of explicit resource management accepts: 7.23.9 or later. */
53
+ export declare function supportsResourceManagement(version: string | undefined): boolean;
43
54
  //# sourceMappingURL=preset.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAQvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAc9G"}
1
+ {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAWvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAQD,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAmB9G;AAED,kIAAkI;AAClI,wBAAgB,0BAA0B,CAAC,OAAO,EAAG,MAAM,GAAG,SAAS,GAAI,OAAO,CAMjF"}
@@ -1,10 +1,17 @@
1
1
  import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
2
2
  import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
3
+ import explicitResourceManagement from "@babel/plugin-transform-explicit-resource-management";
3
4
  import { asyncGeneratorHelpersPlugin } from "./async-generator-helpers.js";
5
+ import { asyncGeneratorResources } from "./async-generator-resources.js";
4
6
  import { bindGeneratorsPlugin } from "./bind-generators.js";
5
7
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
6
- import { skipRuntimePlugin } from "./skip-runtime.js";
8
+ import { skipRuntimePlugin, withRuntimeSkipped } from "./skip-runtime.js";
9
+ import { staticMembersPlugin } from "./static-members.js";
7
10
  export { METADATA_KEY } from "./bind-generators.js";
11
+ /** The plugin of Babel for async generators, without a change of the functions of the runtime (skip-runtime.ts). */
12
+ const asyncGeneratorFunctionsOutsideRuntime = withRuntimeSkipped(asyncGeneratorFunctions);
13
+ /** The plugin of `using` declarations for async generators, before the plugin of Babel for async generators (async-generator-resources.ts). */
14
+ const asyncGeneratorResourcesPlugin = asyncGeneratorResources(explicitResourceManagement);
8
15
  /** The default runtime module. */
9
16
  export const DEFAULT_RUNTIME = "async-browser-context/runtime";
10
17
  /**
@@ -17,13 +24,21 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
17
24
  * 2. A plugin of this library binds each sync generator to the context of
18
25
  * its creation.
19
26
  * 3. A plugin of this library gives the name of each async function to
20
- * `coroutine`, for the callback hooks. The option `names: false` removes
21
- * this step.
22
- * 4. A plugin of this library gives the async generators to `asyncGenerator`
23
- * of the runtime, in place of the Babel helper.
24
- * 5. `@babel/plugin-transform-async-generator-functions` changes async
25
- * generators and `for await` loops.
26
- * 6. `@babel/plugin-transform-async-to-generator` changes each async function
27
+ * `coroutine`, and of each async generator to `asyncGenerator`, for the
28
+ * callback hooks. The option `names: false` removes this step.
29
+ * 4. A plugin of this library moves the static class members with async arrow
30
+ * functions into static private methods. Thus `this` and `super` stay
31
+ * correct (static-members.ts).
32
+ * 5. A plugin of this library gives the async generators to the helpers of the
33
+ * runtime, in place of the Babel helpers.
34
+ * 6. `@babel/plugin-transform-explicit-resource-management` changes `using` and
35
+ * `await using` declarations, with Babel 7.23.9 and later.
36
+ * 7. A plugin of this library starts the plugin of step 6 on each async
37
+ * generator, before step 8 changes it (async-generator-resources.ts).
38
+ * 8. `@babel/plugin-transform-async-generator-functions` changes async
39
+ * generators and `for await` loops. Its own traversal also skips the
40
+ * functions of the runtime (skip-runtime.ts).
41
+ * 9. `@babel/plugin-transform-async-to-generator` changes each async function
27
42
  * into a generator that the `coroutine` function of the runtime operates.
28
43
  *
29
44
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -32,16 +47,29 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
32
47
  export default function asyncBrowserContextPreset(api, options = {}) {
33
48
  api.assertVersion("^7.22.0 || ^8.0.0");
34
49
  const runtime = options.runtime ?? DEFAULT_RUNTIME;
50
+ // Stryker disable next-line ObjectLiteral: the plugin also finds the local names that the preset gives, for example _coroutine, without the runtime
35
51
  const names = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
52
+ // The async transform keeps an `await using` declaration, which is a SyntaxError in a generator
53
+ const resources = supportsResourceManagement(api.version) ? [explicitResourceManagement, asyncGeneratorResourcesPlugin] : [];
36
54
  return {
37
55
  plugins: [
38
56
  skipRuntimePlugin,
39
57
  [bindGeneratorsPlugin, { runtime }],
40
58
  ...names,
59
+ staticMembersPlugin,
41
60
  [asyncGeneratorHelpersPlugin, { runtime }],
42
- asyncGeneratorFunctions,
61
+ ...resources,
62
+ asyncGeneratorFunctionsOutsideRuntime,
43
63
  [asyncToGenerator, { module: runtime, method: "coroutine" }],
44
64
  ],
45
65
  };
46
66
  }
67
+ /** This function gives `true` for a version of Babel that the plugin of explicit resource management accepts: 7.23.9 or later. */
68
+ export function supportsResourceManagement(version) {
69
+ if (typeof version !== "string") {
70
+ return false;
71
+ }
72
+ const [major = 0, minor = 0, patch = 0] = version.split(/[.+-]/).map(Number);
73
+ return major > 7 || (major === 7 && (minor > 23 || (minor === 23 && patch >= 9)));
74
+ }
47
75
  //# sourceMappingURL=preset.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,eAAe,CAAC;IACnD,MAAM,KAAK,GAAkB,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAClG,OAAO;QACH,OAAO,EAAG;YACN,iBAAiB;YACjB,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC;YACnC,GAAG,KAAK;YACR,CAAC,2BAA2B,EAAE,EAAE,OAAO,EAAE,CAAC;YAC1C,uBAAuB;YACvB,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC"}
1
+ {"version":3,"file":"preset.js","sourceRoot":"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GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,0BAA0B,EAAE,6BAA6B,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5I,OAAO;QACH,OAAO,EAAG;YACN,iBAAiB;YACjB,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC;YACnC,GAAG,KAAK;YACR,mBAAmB;YACnB,CAAC,2BAA2B,EAAE,EAAE,OAAO,EAAE,CAAC;YAC1C,GAAG,SAAS;YACZ,qCAAqC;YACrC,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC;AAED,kIAAkI;AAClI,MAAM,UAAU,0BAA0B,CAAC,OAA4B;IACnE,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC7E,OAAO,KAAK,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,EAAE,IAAI,CAAC,KAAK,KAAK,EAAE,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACtF,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { types, type NodePath } from "@babel/core";
2
+ /**
3
+ * The directive that marks a native async function of the runtime. If the
4
+ * preset changes that function, the function uses `coroutine` to operate
5
+ * `coroutine`, without end. A directive stays in the code after TypeScript,
6
+ * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
7
+ * webpack, but a comment does not.
8
+ */
9
+ export declare const RUNTIME_DIRECTIVE = "async-browser-context runtime";
10
+ /** This function gives `true` if `node` is a function with the directive of the runtime. */
11
+ export declare function isRuntimeFunction(node: types.Node): boolean;
12
+ /**
13
+ * This function gives `true` if `callee` is the function `name` of the runtime. It accepts the
14
+ * forms of the output of the preset:
15
+ *
16
+ * - an import: `_coroutine` from `import { coroutine as _coroutine } from runtime`
17
+ * - CommonJS: `_runtime.coroutine` or `(0, _runtime.coroutine)` with `_runtime = require(runtime)`
18
+ * - CommonJS of a script: `_coroutine` with `var _coroutine = require(runtime).coroutine`
19
+ * - the local name that Babel gives, `_coroutine` and `_coroutine2`, for the output of another tool.
20
+ */
21
+ export declare function isRuntimeCallee(callee: NodePath, runtime: string, name: string): boolean;
22
+ //# sourceMappingURL=runtime-calls.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime-calls.d.ts","sourceRoot":"","sources":["../../src/babel/runtime-calls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEnD;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,kCAAkC,CAAC;AAEjE,4FAA4F;AAC5F,wBAAgB,iBAAiB,CAAC,IAAI,EAAG,KAAK,CAAC,IAAI,GAAI,OAAO,CAI7D;AA0BD;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAG,QAAQ,EAAE,OAAO,EAAG,MAAM,EAAE,IAAI,EAAG,MAAM,GAAI,OAAO,CAqB5F"}
@@ -0,0 +1,69 @@
1
+ import { types } from "@babel/core";
2
+ /**
3
+ * The directive that marks a native async function of the runtime. If the
4
+ * preset changes that function, the function uses `coroutine` to operate
5
+ * `coroutine`, without end. A directive stays in the code after TypeScript,
6
+ * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
7
+ * webpack, but a comment does not.
8
+ */
9
+ export const RUNTIME_DIRECTIVE = "async-browser-context runtime";
10
+ /** This function gives `true` if `node` is a function with the directive of the runtime. */
11
+ export function isRuntimeFunction(node) {
12
+ return types.isFunction(node)
13
+ && node.body.type === "BlockStatement"
14
+ && node.body.directives.some((directive) => directive.value.value === RUNTIME_DIRECTIVE);
15
+ }
16
+ /**
17
+ * This function gives `true` if `node` is `require(runtime)`, also inside at most three helper calls or members,
18
+ * for example `_interopRequireWildcard(require(runtime))`. It gives `false` for `null` and `undefined`.
19
+ */
20
+ function containsRequireOf(node, runtime, depth = 0) {
21
+ if (depth > 3) {
22
+ return false;
23
+ }
24
+ if (types.isCallExpression(node)) {
25
+ if (types.isIdentifier(node.callee, { name: "require" }) && types.isStringLiteral(node.arguments[0], { value: runtime })) {
26
+ return true;
27
+ }
28
+ return node.arguments.some((argument) => containsRequireOf(argument, runtime, depth + 1));
29
+ }
30
+ // Stryker disable next-line ConditionalExpression: of the other nodes, only an optional member has an object, and the tools do not give one here
31
+ return types.isMemberExpression(node) && containsRequireOf(node.object, runtime, depth + 1);
32
+ }
33
+ /** This function gives the initial value of the variable that `identifier` names, if a variable declaration gives it. */
34
+ function initOf(identifier) {
35
+ const binding = identifier.scope.getBinding(identifier.node.name);
36
+ return binding?.path.isVariableDeclarator() === true ? binding.path.node.init : undefined;
37
+ }
38
+ /**
39
+ * This function gives `true` if `callee` is the function `name` of the runtime. It accepts the
40
+ * forms of the output of the preset:
41
+ *
42
+ * - an import: `_coroutine` from `import { coroutine as _coroutine } from runtime`
43
+ * - CommonJS: `_runtime.coroutine` or `(0, _runtime.coroutine)` with `_runtime = require(runtime)`
44
+ * - CommonJS of a script: `_coroutine` with `var _coroutine = require(runtime).coroutine`
45
+ * - the local name that Babel gives, `_coroutine` and `_coroutine2`, for the output of another tool.
46
+ */
47
+ export function isRuntimeCallee(callee, runtime, name) {
48
+ let target = callee;
49
+ if (target.isSequenceExpression()) {
50
+ const expressions = target.get("expressions");
51
+ target = expressions[expressions.length - 1];
52
+ }
53
+ if (target.isIdentifier()) {
54
+ if (target.referencesImport(runtime, name)) {
55
+ return true;
56
+ }
57
+ const init = initOf(target);
58
+ if (types.isMemberExpression(init) && types.isIdentifier(init.property, { name }) && containsRequireOf(init.object, runtime)) {
59
+ return true;
60
+ }
61
+ return new RegExp(`^_${name}\\d*$`).test(target.node.name);
62
+ }
63
+ if (target.isMemberExpression() && !target.node.computed && types.isIdentifier(target.node.property, { name })) {
64
+ const object = target.get("object");
65
+ return object.isIdentifier() ? containsRequireOf(initOf(object), runtime) : containsRequireOf(object.node, runtime);
66
+ }
67
+ return false;
68
+ }
69
+ //# sourceMappingURL=runtime-calls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime-calls.js","sourceRoot":"","sources":["../../src/babel/runtime-calls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAiB,MAAM,aAAa,CAAC;AAEnD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,+BAA+B,CAAC;AAEjE,4FAA4F;AAC5F,MAAM,UAAU,iBAAiB,CAAC,IAAiB;IAC/C,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC;WACtB,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,gBAAgB;WACnC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,KAAK,iBAAiB,CAAC,CAAC;AACjG,CAAC;AAED;;;GAGG;AACH,SAAS,iBAAiB,CAAC,IAAoC,EAAE,OAAgB,EAAE,KAAK,GAAG,CAAC;IACxF,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACZ,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,KAAK,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,IAAI,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,IAAI,EAAG,SAAS,EAAE,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,EAAG,OAAO,EAAE,CAAC,EAAE,CAAC;YACzH,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C,kBAAkB,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,IAAI,iBAAiB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;YAC3H,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,OAAO,IAAI,MAAM,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/D,CAAC;IACD,IAAI,MAAM,CAAC,kBAAkB,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,IAAI,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;QAC7G,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACpC,OAAO,MAAM,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACxH,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -1,16 +1,23 @@
1
- import type { PluginObject, PluginPass } from "@babel/core";
1
+ import type { PluginAPI, PluginObject, PluginPass } from "@babel/core";
2
+ export { RUNTIME_DIRECTIVE } from "./runtime-calls.js";
2
3
  /**
3
- * The directive that marks a native async function of the runtime. If the
4
- * preset changes that function, the function uses `coroutine` to operate
5
- * `coroutine`, without end. A directive stays in the code after TypeScript,
6
- * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
7
- * webpack, but a comment does not.
4
+ * The first plugin of the preset. It skips each function with the directive of the runtime
5
+ * (runtime-calls.ts), so that the plugins of the main traversal do not change it. The plugins
6
+ * of the preset with their own traversal also skip it (refer to `withRuntimeSkipped`). Bundlers that
7
+ * apply the preset to `node_modules` thus do not change the runtime.
8
8
  */
9
- export declare const RUNTIME_DIRECTIVE = "async-browser-context runtime";
9
+ export declare function skipRuntimePlugin(): PluginObject<PluginPass>;
10
+ /** A Babel plugin: a function that gets the API of Babel, the options and the folder. */
11
+ export type PluginFactory = (api: PluginAPI, options: object, dirname: string) => PluginObject<PluginPass>;
10
12
  /**
11
- * The first plugin of the preset. It skips each function with the directive,
12
- * so that no other plugin of the preset changes it. Bundlers that apply the
13
- * preset to `node_modules` thus do not change the runtime.
13
+ * The plugin of Babel for async generators changes the async generators and the `for await`
14
+ * loops in its own traversal. It starts that traversal in its `Program` visitor. The skip of the main
15
+ * traversal does not apply to that traversal. This function gives a plugin that operates as
16
+ * `factory`. But it puts the visitor of `skipRuntimePlugin` in front of each traversal that the `Program` visitor
17
+ * starts.
18
+ *
19
+ * The function uses the traversal of the Babel that loads the plugin (`api.traverse`).
20
+ * If the plugin has no `Program` function, it stays as it is.
14
21
  */
15
- export declare function skipRuntimePlugin(): PluginObject<PluginPass>;
22
+ export declare function withRuntimeSkipped(factory: PluginFactory): PluginFactory;
16
23
  //# sourceMappingURL=skip-runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"skip-runtime.d.ts","sourceRoot":"","sources":["../../src/babel/skip-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,YAAY,EAAE,UAAU,EAAS,MAAM,aAAa,CAAC;AAE7E;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,kCAAkC,CAAC;AAMjE;;;;GAIG;AACH,wBAAgB,iBAAiB,IAAK,YAAY,CAAC,UAAU,CAAC,CAW7D"}
1
+ {"version":3,"file":"skip-runtime.d.ts","sourceRoot":"","sources":["../../src/babel/skip-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,SAAS,EAAE,YAAY,EAAE,UAAU,EAAkB,MAAM,aAAa,CAAC;AAGjG,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAkBvD;;;;;GAKG;AACH,wBAAgB,iBAAiB,IAAK,YAAY,CAAC,UAAU,CAAC,CAK7D;AAED,yFAAyF;AACzF,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,EAAG,MAAM,EAAE,OAAO,EAAG,MAAM,KAAK,YAAY,CAAC,UAAU,CAAC,CAAC;AAE9G;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAG,aAAa,GAAI,aAAa,CAwB1E"}
@@ -1,29 +1,65 @@
1
+ import { isRuntimeFunction } from "./runtime-calls.js";
2
+ export { RUNTIME_DIRECTIVE } from "./runtime-calls.js";
3
+ /** This function skips a function with the directive of the runtime. */
4
+ function skipRuntimeFunction(functionPath) {
5
+ if (isRuntimeFunction(functionPath.node)) {
6
+ functionPath.skip();
7
+ }
8
+ }
1
9
  /**
2
- * The directive that marks a native async function of the runtime. If the
3
- * preset changes that function, the function uses `coroutine` to operate
4
- * `coroutine`, without end. A directive stays in the code after TypeScript,
5
- * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
6
- * webpack, but a comment does not.
10
+ * This function gives a new visitor that skips each function with the directive of the runtime.
11
+ * Babel changes a visitor object when it prepares it (explode). Thus each use gets its own object,
12
+ * and two copies of Babel in one process do not share it.
7
13
  */
8
- export const RUNTIME_DIRECTIVE = "async-browser-context runtime";
9
- function isRuntimeFunction(node) {
10
- return node.body.type === "BlockStatement" && node.body.directives.some((directive) => directive.value.value === RUNTIME_DIRECTIVE);
14
+ function skipRuntimeVisitor() {
15
+ return { Function: skipRuntimeFunction };
11
16
  }
12
17
  /**
13
- * The first plugin of the preset. It skips each function with the directive,
14
- * so that no other plugin of the preset changes it. Bundlers that apply the
15
- * preset to `node_modules` thus do not change the runtime.
18
+ * The first plugin of the preset. It skips each function with the directive of the runtime
19
+ * (runtime-calls.ts), so that the plugins of the main traversal do not change it. The plugins
20
+ * of the preset with their own traversal also skip it (refer to `withRuntimeSkipped`). Bundlers that
21
+ * apply the preset to `node_modules` thus do not change the runtime.
16
22
  */
17
23
  export function skipRuntimePlugin() {
18
24
  return {
19
25
  name: "async-browser-context/skip-runtime",
20
- visitor: {
21
- Function(functionPath) {
22
- if (isRuntimeFunction(functionPath.node)) {
23
- functionPath.skip();
26
+ visitor: skipRuntimeVisitor(),
27
+ };
28
+ }
29
+ /**
30
+ * The plugin of Babel for async generators changes the async generators and the `for await`
31
+ * loops in its own traversal. It starts that traversal in its `Program` visitor. The skip of the main
32
+ * traversal does not apply to that traversal. This function gives a plugin that operates as
33
+ * `factory`. But it puts the visitor of `skipRuntimePlugin` in front of each traversal that the `Program` visitor
34
+ * starts.
35
+ *
36
+ * The function uses the traversal of the Babel that loads the plugin (`api.traverse`).
37
+ * If the plugin has no `Program` function, it stays as it is.
38
+ */
39
+ export function withRuntimeSkipped(factory) {
40
+ return function runtimeSkipped(api, options, dirname) {
41
+ const plugin = factory(api, options, dirname);
42
+ const program = plugin.visitor?.Program;
43
+ if (typeof program !== "function") {
44
+ return plugin;
45
+ }
46
+ const visitor = {
47
+ ...plugin.visitor,
48
+ Program(path, state) {
49
+ const ownTraverse = path.traverse;
50
+ // An own property of this path object hides the method of the prototype during the visitor
51
+ path.traverse = function (inner, innerState) {
52
+ Reflect.apply(ownTraverse, this, [api.traverse.visitors.merge([skipRuntimeVisitor(), inner]), innerState]);
53
+ };
54
+ try {
55
+ Reflect.apply(program, this, [path, state]);
56
+ }
57
+ finally {
58
+ delete path.traverse;
24
59
  }
25
60
  },
26
- },
61
+ };
62
+ return { ...plugin, visitor };
27
63
  };
28
64
  }
29
65
  //# sourceMappingURL=skip-runtime.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"skip-runtime.js","sourceRoot":"","sources":["../../src/babel/skip-runtime.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,+BAA+B,CAAC;AAEjE,SAAS,iB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AAM,GAAG,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC9C,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC;QACxC,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;YAChC,OAAO,MAAM,CAAC;QAClB,CAAC;QACD,MAAM,OAAO,GAAG;YACZ,GAAG,MAAM,CAAC,OAAO;YACjB,OAAO,CAAoB,IAA8B,EAAE,KAAkB;gBACzE,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC;gBAClC,2FAA2F;gBAC3F,IAAI,CAAC,QAAQ,GAAG,UAA2B,KAAwB,EAAE,UAAqB;oBACtF,OAAO,CAAC,KAAK,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,kBAAkB,EAAsB,EAAE,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC;gBACnI,CAAyB,CAAC;gBAC1B,IAAI,CAAC;oBACD,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;gBAChD,CAAC;wBAAS,CAAC;oBACP,OAAQ,IAAgC,CAAC,QAAQ,CAAC;gBACtD,CAAC;YACL,CAAC;SACmB,CAAC;QACzB,OAAO,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,CAAC;IAClC,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { type PluginObject, type PluginPass } from "@babel/core";
2
+ /** The Babel plugin that moves static class members with async arrow functions into static private methods. */
3
+ export declare function staticMembersPlugin(): PluginObject<PluginPass>;
4
+ //# sourceMappingURL=static-members.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"static-members.d.ts","sourceRoot":"","sources":["../../src/babel/static-members.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAoFvF,+GAA+G;AAC/G,wBAAgB,mBAAmB,IAAK,YAAY,CAAC,UAAU,CAAC,CA+B/D"}
@@ -0,0 +1,112 @@
1
+ import { types } from "@babel/core";
2
+ import { keyName } from "./name-coroutines.js";
3
+ /**
4
+ * `@babel/plugin-transform-async-to-generator` keeps `this` and `super` of an async arrow
5
+ * function in the nearest function or non-static class property. It does not stop at a static
6
+ * class property or a static block. Thus, in
7
+ *
8
+ * ```js
9
+ * class A { static load = async () => { await null; return this; }; }
10
+ * ```
11
+ *
12
+ * the transform gives `var _this = this` at the top of the module, and `A.load()` gives
13
+ * `undefined`. With `super`, the output is a SyntaxError. The plain async transform of Babel
14
+ * changes such code only when it also changes the class fields. This preset changes async
15
+ * functions in modern code, so it must change the class first.
16
+ *
17
+ * This plugin moves such an initializer or static block into a static private method, which has
18
+ * the same `this` and `super`:
19
+ *
20
+ * ```js
21
+ * class A { static load = this.#asyncContext1(); static #asyncContext1() { return { "load": async () => {...} }["load"]; } }
22
+ * ```
23
+ *
24
+ * The object property keeps the name of the function (`A.load.name` is "load").
25
+ */
26
+ /**
27
+ * This function gives `true` if `element` is or contains an async arrow function whose `this` is the `this` of the class element.
28
+ * It gives `false` for a path without a node, for example the value of `static x;`.
29
+ */
30
+ function hasAsyncArrowOfElement(element) {
31
+ if (element.isArrowFunctionExpression() && element.node.async) {
32
+ return true;
33
+ }
34
+ let found = false;
35
+ element.traverse({
36
+ Function(path) {
37
+ if (!path.isArrowFunctionExpression()) {
38
+ // A function that is not an arrow function has its own this
39
+ path.skip();
40
+ return;
41
+ }
42
+ if (path.node.async) {
43
+ found = true;
44
+ // Stryker disable next-line CallExpression: the stop only ends the search early, and the result stays the same
45
+ path.stop();
46
+ }
47
+ },
48
+ Class(path) {
49
+ path.skip();
50
+ },
51
+ });
52
+ return found;
53
+ }
54
+ /** This function gives the name of a static class element for NamedEvaluation, or `undefined` for a computed key that is not a literal. */
55
+ function staticName(node) {
56
+ // A private property has no computed field
57
+ return keyName(node.key, node.computed === true);
58
+ }
59
+ /** This function gives a private name that the class does not use. */
60
+ function uniquePrivateName(body) {
61
+ const used = new Set();
62
+ for (const member of body.body) {
63
+ if ("key" in member && types.isPrivateName(member.key)) {
64
+ used.add(member.key.id.name);
65
+ }
66
+ }
67
+ let index = 1;
68
+ while (used.has(`asyncContext${index}`)) {
69
+ index++;
70
+ }
71
+ return types.privateName(types.identifier(`asyncContext${index}`));
72
+ }
73
+ /** This function adds a static private method with `statements` to the class, and gives `this.#method()`. */
74
+ function addStaticMethod(body, statements) {
75
+ const name = uniquePrivateName(body.node);
76
+ body.pushContainer("body", types.classPrivateMethod("method", name, [], types.blockStatement(statements), true));
77
+ return types.callExpression(types.memberExpression(types.thisExpression(), types.cloneNode(name)), []);
78
+ }
79
+ /** The Babel plugin that moves static class members with async arrow functions into static private methods. */
80
+ export function staticMembersPlugin() {
81
+ return {
82
+ name: "async-browser-context/static-members",
83
+ visitor: {
84
+ Program(programPath) {
85
+ programPath.traverse({
86
+ "ClassProperty|ClassPrivateProperty|ClassAccessorProperty"(path) {
87
+ const node = path.node;
88
+ if (node.static !== true || !hasAsyncArrowOfElement(path.get("value"))) {
89
+ return;
90
+ }
91
+ // A field without a value has no async arrow function
92
+ const value = node.value;
93
+ const name = staticName(node);
94
+ // An object property keeps the name of an anonymous function, as the static property did
95
+ const expression = name === undefined
96
+ ? value
97
+ : types.memberExpression(types.objectExpression([types.objectProperty(types.stringLiteral(name), value)]), types.stringLiteral(name), true);
98
+ node.value = addStaticMethod(path.parentPath, [types.returnStatement(expression)]);
99
+ },
100
+ StaticBlock(path) {
101
+ if (!hasAsyncArrowOfElement(path)) {
102
+ return;
103
+ }
104
+ const call = addStaticMethod(path.parentPath, path.node.body);
105
+ path.node.body = [types.expressionStatement(call)];
106
+ },
107
+ });
108
+ },
109
+ },
110
+ };
111
+ }
112
+ //# sourceMappingURL=static-members.js.map
@@ -0,0 +1 @@
1
+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O,CAAa,CAAC,EAAE,CAAC;4BACjF,OAAO;wBACX,CAAC;wBACD,sDAAsD;wBACtD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAyB,CAAC;wBAC7C,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;wBAC9B,yFAAyF;wBACzF,MAAM,UAAU,GAAG,IAAI,KAAK,SAAS;4BACjC,CAAC,CAAC,KAAK;4BACP,CAAC,CAAC,KAAK,CAAC,gBAAgB,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;wBAChJ,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,UAAuC,EAAE,CAAC,KAAK,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;oBACpH,CAAC;oBACD,WAAW,CAAC,IAAI;wBACZ,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,EAAE,CAAC;4BAChC,OAAO;wBACX,CAAC;wBACD,MAAM,IAAI,GAAG,eAAe,CAAC,IAAI,CAAC,UAAuC,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;wBAC3F,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC;oBACvD,CAAC;iBACJ,CAAC,CAAC;YACP,CAAC;SACJ;KACJ,CAAC;AACN,CAAC"}
@@ -2,35 +2,73 @@
2
2
  * The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
3
3
  * changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
4
4
  * The helper continued each step in a `then` callback, and a `then` callback is slow in
5
- * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
6
- * helper `OverloadYield` with the class of this module. The driver in this module then
7
- * continues each step with a native `await`.
5
+ * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime. It also takes
6
+ * the helpers `OverloadYield`, `awaitAsyncGenerator` and `asyncGeneratorDelegate` from this
7
+ * module. The driver in this module then continues each step with a native `await`.
8
8
  *
9
9
  * The protocol is the protocol of the Babel 8 helper: the body gives
10
10
  * `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
11
- * step of `yield*`. Each other value is a `yield`.
11
+ * step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
12
+ * generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
13
+ * value first, and a request on a complete generator settles at once. The delegate of this
14
+ * module also gives kind 2 for a step of `yield*` after `return()`.
12
15
  */
13
16
  /**
14
- * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
- * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
17
+ * The value that the body gives for an `await` (kind 0), for a step of `yield*` (kind 1), or
18
+ * for a step of `yield*` after `return()` (kind 2). The helpers `awaitAsyncGenerator` and
19
+ * `asyncGeneratorDelegate` make it with `new`.
16
20
  */
17
21
  export declare class OverloadYield {
18
- v: unknown;
19
- readonly k: 0 | 1;
20
- constructor(value: unknown, kind: 0 | 1);
22
+ readonly v: unknown;
23
+ readonly k: 0 | 1 | 2;
24
+ constructor(value: unknown, kind: 0 | 1 | 2);
21
25
  }
26
+ /**
27
+ * The helper `awaitAsyncGenerator` of Babel: the body gives its value for an `await`. The preset
28
+ * gives this function in place of the helper of Babel. Thus the marker is always the class of this
29
+ * module, also when `@babel/plugin-transform-runtime` gives the other helpers.
30
+ */
31
+ export declare function awaitAsyncGenerator(value: unknown): OverloadYield;
32
+ /**
33
+ * The helper `asyncGeneratorDelegate` of Babel 8: a sync iterator for `yield*` in an async
34
+ * generator. Each step of the inner async iterator becomes `new OverloadYield(promise, 1)`, or
35
+ * kind 2 after `return()`. The driver awaits the promise, and gives a copy of its result back to
36
+ * the iterator. After kind 2, the driver gives it back with `return()`. Thus a complete inner
37
+ * iterator ends the body, as with a native `yield*`.
38
+ */
39
+ export declare function asyncGeneratorDelegate(inner: AsyncIterator<unknown, unknown, unknown>): Iterator<unknown, unknown, unknown>;
40
+ /**
41
+ * A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
42
+ * awaited object one time, in the context of the body. When the object has no `then` method,
43
+ * the driver awaits the box. Thus the engine does not read `then` a second time, and the
44
+ * `await` takes the same microtask turns.
45
+ */
46
+ export declare class PlainValue {
47
+ readonly value: unknown;
48
+ constructor(value: unknown);
49
+ }
50
+ /** This function gives the value in a box, or the value itself. */
51
+ export declare function unbox(value: unknown): unknown;
52
+ /** This function gives `true` for a value that can have properties: an object or a function. */
53
+ export declare function isObject(value: unknown): value is object;
22
54
  /** The methods of a generator that a request of the consumer starts. */
23
55
  export type ResumeKey = "next" | "throw" | "return";
24
56
  /**
25
57
  * This function does one step of the body: `generator[key](argument)`. The browser runtime sets
26
58
  * the context of the generator around the step. `resumed` is `true` for a step after an
27
- * `await` of the driver.
59
+ * `await` of the driver. The function gives the result of the generator without a change.
28
60
  */
29
61
  export type AsyncGeneratorStep = (key: ResumeKey, argument: unknown, resumed: boolean) => IteratorResult<unknown, unknown>;
62
+ /**
63
+ * This function gives the value that the driver awaits in place of `value`. The browser runtime
64
+ * reads the `then` property in the context of the generator. The Node.js runtime gives the
65
+ * value without a change.
66
+ */
67
+ export type Prepare = (value: unknown) => unknown;
30
68
  /**
31
69
  * This function makes an async generator object. The object queues the requests of the
32
- * consumer, and starts one request at a time, as the Babel helper does. `enter` starts the
33
- * driver: the Node.js runtime starts it in the context of the generator.
70
+ * consumer, and starts one request at a time, as a native async generator does. `enter`
71
+ * starts the driver: the Node.js runtime starts it in the context of the generator.
34
72
  */
35
- export declare function createAsyncGenerator(step: AsyncGeneratorStep, NativePromise: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>): AsyncGenerator<unknown, unknown, unknown>;
73
+ export declare function createAsyncGenerator(step: AsyncGeneratorStep, PromiseConstructor: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>, prepare?: Prepare): AsyncGenerator<unknown, unknown, unknown>;
36
74
  //# sourceMappingURL=async-generator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"async-generator.d.ts","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH;;;GAGG;AACH,qBAAa,aAAa;IACtB,CAAC,EAAG,OAAO,CAAC;IACZ,QAAQ,CAAC,CAAC,EAAG,CAAC,GAAG,CAAC,CAAC;gBACP,KAAK,EAAG,OAAO,EAAE,IAAI,EAAG,CAAC,GAAG,CAAC;CAI5C;AAED,wEAAwE;AACxE,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEpD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAG,SAAS,EAAE,QAAQ,EAAG,OAAO,EAAE,OAAO,EAAG,OAAO,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAwC9H;;;;GAIG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAG,kBAAkB,EACzB,aAAa,EAAG,kBAAkB,EAClC,KAAK,GAAG,CAAC,KAAK,EAAG,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAsB,GAC3E,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CA2E5C"}
1
+ {"version":3,"file":"async-generator.d.ts","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH;;;;GAIG;AACH,qBAAa,aAAa;IACtB,QAAQ,CAAC,CAAC,EAAG,OAAO,CAAC;IACrB,QAAQ,CAAC,CAAC,EAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBACX,KAAK,EAAG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