transform.spec.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**
  2. * Semantic check of the worker module transform (`src/compile/transform.ts`): what the
  3. * emitted CommonJS body looks like for each module form, how suspension points
  4. * are rewritten, that line numbers survive, which forms are refused, and that
  5. * every covered trap form stays fixed.
  6. *
  7. * Scope boundary: this file checks the transform itself; the pack-time loop
  8. * around it is covered end-to-end by the packer's `image-loadable.spec.ts`.
  9. * Emitted-code assertions are deliberately written against substrings
  10. * of the real output rather than whole-file goldens: a golden would fail on every
  11. * helper reordering, which is not the contract. The contract is the observable
  12. * one — the code parses as script, publishes the right bindings, keeps line
  13. * count, and routes suspension through `__als`.
  14. *
  15. * The trap cases are module forms that break a boot when the transform
  16. * mishandles them. Five traps cannot recur while the AST pass is the parser,
  17. * but they stay checked because a future parser swap could reintroduce them.
  18. */
  19. import { expect, test } from 'vitest'
  20. import { parse } from 'acorn'
  21. import { lowerModuleSource } from '../../src/compile/transform.ts'
  22. import { LOWERING_VERSION, WRAPPER_PARAMS } from '../../src/image-layout.ts'
  23. /**
  24. * Lower one probe module the way the packer does — the transform's only caller.
  25. * @param source - Module source under test.
  26. * @param path - Path the diagnostics name.
  27. * @returns The emitted body.
  28. */
  29. const transformModule = (source: string, path = 'probe.js'): string =>
  30. lowerModuleSource({ filename: path, source }).code
  31. /** Register one comparison as its own case, serialized at call time. */
  32. const check = (label: string, actual: unknown, expected: unknown): void => {
  33. const [seen, wanted] = [JSON.stringify(actual), JSON.stringify(expected)]
  34. test(label, () => { expect(seen).toBe(wanted) })
  35. }
  36. /** Assert a substring is present in an emitted body. */
  37. const contains = (label: string, code: string, needle: string): void => {
  38. test(label, () => { expect(code).toContain(needle) })
  39. }
  40. /** Assert a substring is absent (used for "must survive untouched" cases). */
  41. const lacks = (label: string, code: string, needle: string): void => {
  42. test(label, () => { expect(code).not.toContain(needle) })
  43. }
  44. /** @returns The error message of a refused transform, or undefined when it succeeded. */
  45. const refusal = (source: string, path = 'probe.js'): string | undefined => {
  46. try {
  47. transformModule(source, path)
  48. return undefined
  49. } catch (reason) {
  50. return (reason as Error).message
  51. }
  52. }
  53. /** Assert the transform refuses a source and names the reason. */
  54. const refuses = (label: string, source: string, fragment: string): void => {
  55. const message = refusal(source)
  56. test(label, () => { expect(message).toContain(fragment) })
  57. }
  58. /**
  59. * The wrapper contract, applied for real: compile the body with the declared
  60. * parameters and run it. This is the same `new Function` shape the loader uses
  61. * (module-loader.ts), so a body that compiles here compiles there.
  62. * @param code - Emitted CommonJS body.
  63. * @param require - Module resolver the body's `require` calls reach.
  64. * @param als - Suspension runtime bound to `__als`.
  65. * @returns The populated `exports` object.
  66. */
  67. function runBody(
  68. code: string,
  69. require: (specifier: string) => unknown = () => ({}),
  70. als?: unknown,
  71. ): Record<string, unknown> {
  72. const exports: Record<string, unknown> = {}
  73. const module = { exports }
  74. // eslint-disable-next-line @typescript-eslint/no-implied-eval -- the wrapper contract under test is a `new Function` body
  75. const factory = new Function(...WRAPPER_PARAMS, code) as (...args: unknown[]) => void
  76. factory(exports, require, module, '/vfs/probe.js', '/vfs', { url: 'file:///vfs/probe.js' }, als)
  77. return exports
  78. }
  79. /** Every emitted body must parse as a script — the transform's own exit gate, re-checked here. */
  80. const parsesAsScript = (label: string, code: string): void => {
  81. test(label, () => {
  82. expect(() => parse(code, { ecmaVersion: 'latest', sourceType: 'script', allowAwaitOutsideFunction: false })).not.toThrow()
  83. })
  84. }
  85. // ---------------------------------------------------------------------------
  86. // 1. The published contract: the three names the packer and loader share.
  87. // ---------------------------------------------------------------------------
  88. check('LOWERING_VERSION is a non-empty string', typeof LOWERING_VERSION === 'string' && LOWERING_VERSION.length > 0, true)
  89. check('WRAPPER_PARAMS is the frozen 7-parameter shape', [...WRAPPER_PARAMS], [
  90. 'exports', 'require', 'module', '__filename', '__dirname', '__dsh$meta', '__als',
  91. ])
  92. // The wrapper signature is a contract with the loader's `new Function`, so the
  93. // parameters must be valid identifiers in that position.
  94. check(
  95. 'every wrapper parameter is a usable identifier',
  96. (() => {
  97. try {
  98. // eslint-disable-next-line @typescript-eslint/no-implied-eval -- proves the parameter names compile where the loader uses them
  99. new Function(...WRAPPER_PARAMS, 'return 0')
  100. return true
  101. } catch {
  102. return false
  103. }
  104. })(),
  105. true,
  106. )
  107. // ---------------------------------------------------------------------------
  108. // 2. lowerModuleSource: the packer face. `lowered` is the pack-time decision.
  109. // ---------------------------------------------------------------------------
  110. {
  111. const esm = lowerModuleSource({ filename: 'node_modules/p/index.js', source: 'export const a = 1\n' })
  112. check('lowered=true for a module that needed rewriting', esm.lowered, true)
  113. check('lowered code differs from source', esm.code !== 'export const a = 1\n', true)
  114. // Plain CommonJS with no suspension point is the "pack as-is" case: the
  115. // collector relies on this to leave 1693-odd entries untouched.
  116. const plain = 'module.exports = 1\n'
  117. const cjs = lowerModuleSource({ filename: 'node_modules/p/legacy.cjs', source: plain })
  118. check('lowered=false for plain CommonJS', cjs.lowered, false)
  119. check('unlowered code is the input verbatim', cjs.code, plain)
  120. // A CommonJS body that still contains a suspension point must be rewritten:
  121. // `await` inside a function is the ALS protocol's business even with no ESM.
  122. const cjsAwait = lowerModuleSource({
  123. filename: 'node_modules/p/async.cjs',
  124. source: 'module.exports = async () => { await 1 }\n',
  125. })
  126. check('lowered=true for CommonJS carrying a suspension point', cjsAwait.lowered, true)
  127. contains('CommonJS await still routes through __als', cjsAwait.code, '__als.pause(')
  128. // `lowered` must agree with the code/source comparison by construction.
  129. check('lowered mirrors code !== source', cjsAwait.lowered, cjsAwait.code !== 'module.exports = async () => { await 1 }\n')
  130. }
  131. {
  132. const direct = lowerModuleSource({
  133. filename: 'node_modules/p/direct.js',
  134. source: "import { createRequire } from 'node:module'\ncreateRequire(import.meta.url)('external-package')\n",
  135. })
  136. check('literal createRequire call is a module request', direct.moduleRequests, ['node:module', 'external-package'])
  137. const aliased = lowerModuleSource({
  138. filename: 'node_modules/p/aliased.js',
  139. source: "makeRequire(import.meta.url)('aliased-package')\nimport { createRequire as makeRequire } from 'node:module'\n",
  140. })
  141. check('aliased createRequire import is indexed before traversal', aliased.moduleRequests, ['aliased-package', 'node:module'])
  142. const runtimeOnly = lowerModuleSource({
  143. filename: 'node_modules/p/runtime-only.js',
  144. source: [
  145. "import { createRequire } from 'node:module'",
  146. 'const localRequire = createRequire(import.meta.url)',
  147. "localRequire('stored')",
  148. "createRequire(new URL('./other.js', import.meta.url))('rebased')",
  149. "{ const createRequire = () => () => undefined; createRequire(import.meta.url)('block-shadowed') }",
  150. "function load(createRequire) { createRequire(import.meta.url)('parameter-shadowed') }",
  151. "for (const createRequire of []) createRequire(import.meta.url)('for-of-shadowed')",
  152. "switch (0) { case 0: const createRequire = () => () => undefined; createRequire(import.meta.url)('switch-shadowed') }",
  153. ].join('\n'),
  154. })
  155. check('stored, rebased, and shadowed createRequire calls stay runtime-only', runtimeOnly.moduleRequests, ['node:module'])
  156. }
  157. // ---------------------------------------------------------------------------
  158. // 3. Import forms.
  159. // ---------------------------------------------------------------------------
  160. {
  161. // Side-effect import: a bare require, nothing bound.
  162. const code = transformModule("import './side-effect.js'\n", 'probe.js')
  163. contains('side-effect import becomes a bare require', code, 'require("./side-effect.js")')
  164. parsesAsScript('side-effect import', code)
  165. const requested: string[] = []
  166. runBody(code, (specifier) => {
  167. requested.push(specifier)
  168. return {}
  169. })
  170. check('side-effect import actually requires at run time', requested, ['./side-effect.js'])
  171. }
  172. {
  173. // Named imports are snapshots (CommonJS destructuring semantics), which is the
  174. // documented, accepted divergence from ESM live bindings on the import side.
  175. const code = transformModule("import { a, b as c } from 'p'\nexport const out = [a, c]\n", 'probe.js')
  176. parsesAsScript('named imports', code)
  177. const exports = runBody(code, () => ({ a: 1, b: 2 }))
  178. check('named import binds by imported name, honouring the alias', exports.out, [1, 2])
  179. }
  180. {
  181. // Default and namespace imports go through the two interop helpers, which must
  182. // agree with `Loader.unwrapExports` on the `__esModule` convention.
  183. const code = transformModule("import d from 'p'\nimport * as ns from 'q'\nexport const seen = [d, ns.x, ns.default]\n", 'probe.js')
  184. parsesAsScript('default and namespace imports', code)
  185. // An `__esModule` module: default comes from `.default`, namespace passes through.
  186. const esModule = { __esModule: true, default: 'D', x: 'X' }
  187. const withEsm = runBody(code, () => esModule)
  188. check('default import of an __esModule module reads .default', (withEsm.seen as unknown[])[0], 'D')
  189. // A plain CommonJS module: the module object *is* the default, and the
  190. // namespace gains a `default` key pointing at it.
  191. const plain = { x: 'X' }
  192. const withCjs = runBody(code, () => plain)
  193. check('default import of plain CommonJS is the module object', (withCjs.seen as unknown[])[0], plain)
  194. check('namespace of plain CommonJS keeps the named key', (withCjs.seen as unknown[])[1], 'X')
  195. check('namespace of plain CommonJS synthesizes default', (withCjs.seen as unknown[])[2], plain)
  196. }
  197. // ---------------------------------------------------------------------------
  198. // 4. Export forms, including the live-binding contract.
  199. // ---------------------------------------------------------------------------
  200. {
  201. const code = transformModule('export const a = 1\nexport function f() {}\nexport class K {}\n', 'probe.js')
  202. parsesAsScript('exported declarations', code)
  203. contains('module bodies get the __esModule marker', code, '__esModule')
  204. contains('use strict is part of the prologue', code, '"use strict"')
  205. const exports = runBody(code)
  206. check('exported const is published', exports.a, 1)
  207. check('exported function is published', typeof exports.f, 'function')
  208. check('exported class is published', typeof exports.K, 'function')
  209. }
  210. {
  211. // Local exports are getters, so a later assignment is observable through
  212. // `exports` — the ESM live-binding property.
  213. const code = transformModule('export let counter = 0\nexport function bump() { counter += 1 }\n', 'probe.js')
  214. parsesAsScript('live binding', code)
  215. const exports = runBody(code)
  216. check('live binding starts at its initializer', exports.counter, 0)
  217. ;(exports.bump as () => void)()
  218. check('live binding observes a later assignment', exports.counter, 1)
  219. // A getter, not a data property: this is what makes the above work.
  220. check(
  221. 'exported local is an accessor',
  222. typeof Object.getOwnPropertyDescriptor(exports, 'counter')?.get,
  223. 'function',
  224. )
  225. }
  226. {
  227. // Trap 4: a multi-declarator export publishes every binding.
  228. const code = transformModule('export const a = 1, b = 2\n', 'probe.js')
  229. parsesAsScript('multi-declarator export', code)
  230. const exports = runBody(code)
  231. check('multi-declarator export publishes every binding', [exports.a, exports.b], [1, 2])
  232. }
  233. {
  234. // Destructuring exports exercise the pattern walker (object, array, rest,
  235. // default) — every branch of `declaredBindings`.
  236. const code = transformModule(
  237. 'export const { p, q: renamed, ...restObj } = { p: 1, q: 2, z: 3 }\n'
  238. + 'export const [first, , third = 30, ...restArr] = [10, 20, undefined, 40, 50]\n',
  239. 'probe.js',
  240. )
  241. parsesAsScript('destructuring exports', code)
  242. const exports = runBody(code)
  243. check('object pattern export', [exports.p, exports.renamed], [1, 2])
  244. check('object rest export', exports.restObj, { z: 3 })
  245. check('array pattern export with hole', [exports.first, exports.third], [10, 30])
  246. check('array rest export', exports.restArr, [40, 50])
  247. // The renamed target is what is published; the source key is not a binding.
  248. check('object pattern publishes the local name, not the source key', 'q' in exports, false)
  249. }
  250. {
  251. const code = transformModule('const x = 1\nexport { x as y }\n', 'probe.js')
  252. parsesAsScript('local export clause', code)
  253. const exports = runBody(code)
  254. check('local export clause publishes under the exported name', exports.y, 1)
  255. check('local export clause does not publish the local name', 'x' in exports, false)
  256. }
  257. {
  258. // Re-export clause: a getter onto the required module, so it also stays live.
  259. const module: Record<string, unknown> = { a: 1 }
  260. const code = transformModule("export { a, a as aliased } from 'p'\n", 'probe.js')
  261. parsesAsScript('re-export clause', code)
  262. const exports = runBody(code, () => module)
  263. check('re-export publishes the name', exports.a, 1)
  264. check('re-export publishes the alias', exports.aliased, 1)
  265. module.a = 2
  266. check('re-export is live against the source module', exports.a, 2)
  267. }
  268. {
  269. // `export *` copies enumerable keys, skips `default`, and must not clobber an
  270. // existing local export.
  271. const code = transformModule("export const own = 'local'\nexport * from 'p'\n", 'probe.js')
  272. parsesAsScript('export all', code)
  273. const exports = runBody(code, () => ({ extra: 'E', default: 'D', own: 'theirs' }))
  274. check('export * copies named keys', exports.extra, 'E')
  275. check('export * skips default', 'default' in exports, false)
  276. check('export * does not overwrite an existing export', exports.own, 'local')
  277. }
  278. {
  279. const code = transformModule("export * as ns from 'p'\n", 'probe.js')
  280. parsesAsScript('export all as namespace', code)
  281. const exports = runBody(code, () => ({ x: 1 }))
  282. check('export * as ns publishes a namespace object', (exports.ns as Record<string, unknown>).x, 1)
  283. }
  284. {
  285. const code = transformModule('export default 42\n', 'probe.js')
  286. parsesAsScript('default export value', code)
  287. check('default export lands on exports.default', runBody(code).default, 42)
  288. }
  289. {
  290. // Documented cost: the function name stops being a module-scope binding, but
  291. // the named function expression can still refer to itself.
  292. const code = transformModule('export default function self(n) { return n <= 0 ? 0 : self(n - 1) }\n', 'probe.js')
  293. parsesAsScript('default export function', code)
  294. const fn = runBody(code).default as (n: number) => number
  295. check('default-exported function keeps self-reference', fn(3), 0)
  296. }
  297. {
  298. const code = transformModule("export { x as default } from 'p'\n", 'probe.js')
  299. parsesAsScript('re-export as default', code)
  300. check('re-export as default publishes default', runBody(code, () => ({ x: 'D' })).default, 'D')
  301. }
  302. // ---------------------------------------------------------------------------
  303. // 5. import.meta and dynamic import.
  304. // ---------------------------------------------------------------------------
  305. {
  306. const code = transformModule('export const here = import.meta.url\n', 'probe.js')
  307. parsesAsScript('import.meta', code)
  308. contains('import.meta becomes the wrapper parameter', code, '__dsh$meta')
  309. check('import.meta.url resolves through the wrapper', runBody(code).here, 'file:///vfs/probe.js')
  310. }
  311. {
  312. // Dynamic import routes through the same require chain (which is what makes
  313. // typert-loader's absolute-path `import()` land on the VFS resolver), and the
  314. // result is namespace-shaped.
  315. const code = transformModule("export const load = () => import('p')\n", 'probe.js')
  316. parsesAsScript('dynamic import', code)
  317. contains('dynamic import becomes the helper call', code, '__dsh$dynImport')
  318. const load = runBody(code, () => ({ x: 1 })).load as () => Promise<Record<string, unknown>>
  319. const namespace = await load()
  320. check('dynamic import resolves to a namespace object', namespace.x, 1)
  321. check('dynamic import namespace has a default', 'default' in namespace, true)
  322. }
  323. // ---------------------------------------------------------------------------
  324. // 6. Suspension points. Behaviour is checked against a recording runtime, so
  325. // these assert the protocol shape rather than re-testing als-runtime.
  326. // ---------------------------------------------------------------------------
  327. /** A recording stand-in for the ALS runtime: proves the emitted calls happen in order. */
  328. function recordingAls(): { als: Record<string, unknown>; calls: string[] } {
  329. const calls: string[] = []
  330. const als = {
  331. pause: (value: unknown) => {
  332. calls.push('pause')
  333. return Promise.resolve(value).then(
  334. settled => ({ ok: true, value: settled, snapshot: 'S' }),
  335. (error: unknown) => ({ ok: false, error, snapshot: 'S' }),
  336. )
  337. },
  338. resume: (token: { ok: boolean; value?: unknown; error?: unknown }) => {
  339. calls.push('resume')
  340. if (token.ok) return token.value
  341. throw token.error
  342. },
  343. snapshot: () => {
  344. calls.push('snapshot')
  345. return 'S'
  346. },
  347. afterYield: (_snapshot: unknown, sent: unknown) => {
  348. calls.push('afterYield')
  349. return sent
  350. },
  351. iterator: (value: unknown) => {
  352. calls.push('iterator')
  353. const source = value as Record<PropertyKey, unknown>
  354. const asyncFactory = source[Symbol.asyncIterator] as (() => AsyncIterator<unknown>) | undefined
  355. if (typeof asyncFactory === 'function') return asyncFactory.call(source)
  356. const syncFactory = source[Symbol.iterator] as () => Iterator<unknown, unknown>
  357. const inner = syncFactory.call(source)
  358. return {
  359. next: async (...args: unknown[]) => {
  360. const step = inner.next(...args as [unknown])
  361. return { done: step.done ?? false, value: await step.value }
  362. },
  363. return: async (sent?: unknown) => {
  364. const step = inner.return?.(sent) ?? { done: true, value: undefined }
  365. return { done: step.done ?? true, value: await step.value }
  366. },
  367. }
  368. },
  369. close: async (iterator: AsyncIterator<unknown>) => {
  370. calls.push('close')
  371. return iterator.return?.(undefined)
  372. },
  373. }
  374. return { als, calls }
  375. }
  376. {
  377. const code = transformModule('export const run = async () => await 7\n', 'probe.js')
  378. parsesAsScript('await rewrite', code)
  379. contains('await is wrapped in resume(await pause(', code, '__als.resume(await __als.pause(')
  380. const { als, calls } = recordingAls()
  381. const run = runBody(code, () => ({}), als).run as () => Promise<number>
  382. check('await still yields its value', await run(), 7)
  383. check('await goes pause-then-resume', calls, ['pause', 'resume'])
  384. }
  385. {
  386. // The rejection path is the half that a naive "snapshot on success" rewrite
  387. // gets wrong, so it is checked as its own case.
  388. const code = transformModule(
  389. "export const run = async () => { try { await Promise.reject(new Error('boom')) } catch (reason) { return `caught:${reason.message}` } }\n",
  390. 'probe.js',
  391. )
  392. parsesAsScript('await rejection', code)
  393. const { als, calls } = recordingAls()
  394. const run = runBody(code, () => ({}), als).run as () => Promise<string>
  395. check('rejection surfaces through resume', await run(), 'caught:boom')
  396. check('rejection path also goes pause-then-resume', calls, ['pause', 'resume'])
  397. }
  398. {
  399. // for-await desugars to an explicit loop; `return()` must run only on abrupt
  400. // completion, which is the language rule. The two
  401. // completion paths need two different loop bodies, so they are separate cases.
  402. const plain = 'export const run = async (src) => { const seen = []\n'
  403. + 'for await (const item of src) { seen.push(item) }\n'
  404. + 'return seen }\n'
  405. const code = transformModule(plain, 'probe.js')
  406. parsesAsScript('for-await', code)
  407. contains('for-await uses the iterator helper', code, '__als.iterator(')
  408. contains('for-await closes on abrupt completion', code, '__als.close(')
  409. /** An async iterable counting up to `n`, rebuilt per case so state cannot leak. */
  410. const counting = (n: number): unknown => ({
  411. [Symbol.asyncIterator]: () => {
  412. let emitted = 0
  413. return {
  414. next: () => Promise.resolve(
  415. emitted < n ? { done: false, value: ++emitted } : { done: true, value: undefined },
  416. ),
  417. }
  418. },
  419. })
  420. // Normal completion: the iterator is exhausted, so `return()` must NOT run.
  421. const { als, calls } = recordingAls()
  422. const run = runBody(code, () => ({}), als).run as (src: unknown) => Promise<number[]>
  423. check('for-await over an async source collects values', await run(counting(2)), [1, 2])
  424. check('normal completion does not close the iterator', calls.includes('close'), false)
  425. // Abrupt completion (break), and a sync source whose values are promises
  426. // (async-from-sync): close must run exactly once.
  427. const breaking = 'export const run = async (src) => { const seen = []\n'
  428. + 'for await (const item of src) { seen.push(item); if (item === 2) break }\n'
  429. + 'return seen }\n'
  430. const breakingCode = transformModule(breaking, 'probe.js')
  431. parsesAsScript('for-await with break', breakingCode)
  432. const { als: als2, calls: calls2 } = recordingAls()
  433. const run2 = runBody(breakingCode, () => ({}), als2).run as (src: unknown) => Promise<number[]>
  434. const syncSource = {
  435. [Symbol.iterator]: () => [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)][Symbol.iterator](),
  436. }
  437. check('for-await accepts a sync source of promises', await run2(syncSource), [1, 2])
  438. check('break closes the iterator exactly once', calls2.filter(name => name === 'close').length, 1)
  439. }
  440. {
  441. // Destructuring in the loop head goes through the same binding path.
  442. const code = transformModule(
  443. 'export const run = async (src) => { const seen = []\nfor await (const { v } of src) seen.push(v)\nreturn seen }\n',
  444. 'probe.js',
  445. )
  446. parsesAsScript('for-await destructuring', code)
  447. const { als } = recordingAls()
  448. const run = runBody(code, () => ({}), als).run as (src: unknown) => Promise<number[]>
  449. check('for-await destructures each step', await run([{ v: 1 }, { v: 2 }]), [1, 2])
  450. }
  451. {
  452. // A non-block body must still be wrapped, or the emitted loop would swallow
  453. // the following statement.
  454. const code = transformModule(
  455. 'export const run = async (src) => { let sum = 0\nfor await (const n of src) sum += n\nreturn sum }\n',
  456. 'probe.js',
  457. )
  458. parsesAsScript('for-await single-statement body', code)
  459. const { als } = recordingAls()
  460. const run = runBody(code, () => ({}), als).run as (src: unknown) => Promise<number>
  461. check('for-await with a non-block body runs correctly', await run([1, 2, 3]), 6)
  462. }
  463. {
  464. // `yield` in an async generator: the snapshot is taken before suspending and
  465. // the consumer's sent value comes back through afterYield.
  466. const code = transformModule(
  467. 'export async function* gen() { const got = yield 1\nyield got * 2 }\n',
  468. 'probe.js',
  469. )
  470. parsesAsScript('yield rewrite', code)
  471. contains('yield is wrapped in afterYield(snapshot(), yield ...)', code, '__als.afterYield(__als.snapshot(),yield ')
  472. const { als, calls } = recordingAls()
  473. const gen = runBody(code, () => ({}), als).gen as () => AsyncGenerator<number, void, number>
  474. const iterator = gen()
  475. check('first yield produces its value', (await iterator.next(0)).value, 1)
  476. check('sent value returns through afterYield', (await iterator.next(21)).value, 42)
  477. check('yield recorded snapshot and afterYield', calls.filter(name => name === 'afterYield').length >= 1, true)
  478. }
  479. {
  480. // Statement-position `yield*` desugars into a forwarding loop.
  481. const code = transformModule(
  482. 'export async function* outer(inner) { yield* inner\nyield "tail" }\n',
  483. 'probe.js',
  484. )
  485. parsesAsScript('yield* rewrite', code)
  486. const { als } = recordingAls()
  487. const outer = runBody(code, () => ({}), als).outer as (inner: unknown) => AsyncGenerator<unknown, void, unknown>
  488. const collected: unknown[] = []
  489. for await (const value of outer(['a', 'b'])) collected.push(value)
  490. check('yield* forwards inner values then continues', collected, ['a', 'b', 'tail'])
  491. }
  492. // ---------------------------------------------------------------------------
  493. // 7. Line numbers. The debugging contract: a stack frame in a transformed body
  494. // points at the same line as the artifact it came from.
  495. // ---------------------------------------------------------------------------
  496. /** @returns Line count of a string, counting a trailing newline's line as the last. */
  497. const lineCount = (text: string): number => text.split('\n').length
  498. {
  499. // The prologue is emitted without a trailing newline, so a transformed body
  500. // has exactly as many lines as its source. Anything else is line drift.
  501. const cases: Array<{ readonly label: string; readonly source: string }> = [
  502. { label: 'imports and exports', source: "import { a } from 'p'\n\nexport const b = a\n\nexport default b\n" },
  503. { label: 'await in a function', source: 'export const f = async () => {\n const v = await g()\n return v\n}\n' },
  504. {
  505. label: 'for-await (body re-emitted)',
  506. source: 'export const f = async (src) => {\n for await (const x of src) {\n use(x)\n }\n done()\n}\n',
  507. },
  508. {
  509. label: 'yield* (statement desugared)',
  510. source: 'export async function* f(inner) {\n yield* inner\n after()\n}\n',
  511. },
  512. { label: 'export * with following lines', source: "export * from 'p'\nconst tail = 1\nexport { tail }\n" },
  513. { label: 'multi-line import clause', source: "import {\n a,\n b,\n} from 'p'\nexport const out = [a, b]\n" },
  514. ]
  515. for (const { label, source } of cases) {
  516. const code = transformModule(source, 'probe.js')
  517. check(`line count survives: ${label}`, lineCount(code), lineCount(source))
  518. }
  519. }
  520. // ---------------------------------------------------------------------------
  521. // 8. Refusals. Every one of these is a form the transform must reject loudly
  522. // rather than emit something that breaks later.
  523. // ---------------------------------------------------------------------------
  524. refuses('top-level await is refused', 'export const a = 1\nawait boot()\n', 'top-level await')
  525. refuses('top-level for-await is refused', 'for await (const x of src) use(x)\n', 'top-level for-await')
  526. refuses(
  527. 'labeled for-await is refused',
  528. 'export const f = async (src) => { outer: for await (const x of src) { break outer } }\n',
  529. 'labeled for-await',
  530. )
  531. refuses(
  532. 'import attributes are refused',
  533. "import data from './d.json' with { type: 'json' }\n",
  534. 'import attributes',
  535. )
  536. refuses(
  537. 'value-position yield* is refused',
  538. 'export async function* f(inner) { const v = yield* inner\nuse(v) }\n',
  539. 'yield* is only supported as a statement',
  540. )
  541. refuses(
  542. 'assignment around yield* is refused, never silently dropped',
  543. 'export async function* f(inner) { let v\nv = yield* inner\nuse(v) }\n',
  544. 'yield* is only supported as the whole statement expression',
  545. )
  546. refuses(
  547. 'a call around yield* is refused, never silently dropped',
  548. 'export async function* f(inner) { use(yield* inner) }\n',
  549. 'yield* is only supported as the whole statement expression',
  550. )
  551. refuses(
  552. 'already-lowered source is refused',
  553. 'const x = __als.pause(1)\n',
  554. 'already lowered',
  555. )
  556. refuses('unparseable source is refused', 'export const = \n', 'parse failed')
  557. {
  558. // A refusal must name the file and the line, which is what makes a build
  559. // failure actionable.
  560. const message = refusal('export const a = 1\n\n\nawait boot()\n', 'node_modules/p/index.js')
  561. check('refusal names the file', message?.includes('node_modules/p/index.js'), true)
  562. check('refusal names the offending line', message?.includes(':4'), true)
  563. }
  564. // ---------------------------------------------------------------------------
  565. // 9. Trap regressions. Each case is a module form that breaks a boot when the
  566. // transform mishandles it; the AST pass must keep them fixed.
  567. // ---------------------------------------------------------------------------
  568. {
  569. // Trap 1: a file with no module syntax can still contain a dynamic import. A
  570. // transform that skips such files would leave it unrewritten, and it would
  571. // escape to the host engine's parser.
  572. const code = transformModule("module.exports = () => import('./x.js')\n", 'probe.js')
  573. contains('trap 1: dynamic import in a CommonJS file is still rewritten', code, '__dsh$dynImport')
  574. parsesAsScript('trap 1', code)
  575. }
  576. {
  577. // Trap 2: `export {}` is a bundler module marker and must be removed before
  578. // `new Function` parses the body. The needle is the keyword in statement
  579. // position, since `exports.` in the prologue legitimately contains the same
  580. // letters.
  581. const code = transformModule('export {};\n', 'probe.js')
  582. lacks('trap 2: bare export {} is removed', code, 'export {')
  583. lacks('trap 2: no export keyword survives', code, 'export;')
  584. parsesAsScript('trap 2', code)
  585. check('trap 2: emitted body still marks __esModule', '__esModule' in runBody(code), true)
  586. }
  587. {
  588. // Trap 3/4: every declarator is published, including declarations without an
  589. // initializer.
  590. const code = transformModule('export let x, y\nexport const set = () => { x = 1; y = 2 }\n', 'probe.js')
  591. parsesAsScript('trap 3', code)
  592. const exports = runBody(code)
  593. ;(exports.set as () => void)()
  594. check('trap 3: every declarator is exported, initializer or not', [exports.x, exports.y], [1, 2])
  595. }
  596. {
  597. // Trap 6: a block comment before a class member named `import` must not be
  598. // treated as a dynamic import. Renaming `EntryTree.prototype.import` breaks
  599. // the loading chain at `Entry._init` with
  600. // "this.parent.tree.import is not a function".
  601. const source = 'export class A {\n /** doc */ import(name) { return name }\n}\n'
  602. const code = transformModule(source, 'probe.js')
  603. lacks('trap 6: a method named import is not rewritten', code, '__dsh$dynImport')
  604. parsesAsScript('trap 6', code)
  605. const A = runBody(code).A as new () => { import: (name: string) => string }
  606. check('trap 6: the method is still callable under its own name', new A().import('kept'), 'kept')
  607. }
  608. {
  609. // Trap 7: a comment between `export` and the declaration keyword must not
  610. // hide the declaration; refusing zod's
  611. // `export /*@__NO_SIDE_EFFECTS__*/ function` takes 30-odd roster rows down
  612. // with it.
  613. const code = transformModule('export /*@__NO_SIDE_EFFECTS__*/ function $constructor(x) { return x }\n', 'probe.js')
  614. parsesAsScript('trap 7', code)
  615. check('trap 7: export with an interposed comment still publishes', typeof runBody(code).$constructor, 'function')
  616. }
  617. {
  618. // `new.target` is also a MetaProperty. Replacing every MetaProperty would
  619. // make `new.target === Cls` permanently false, silently disabling
  620. // abstract-seam guards in `jobs` and `llm`.
  621. const source = 'export class Base {\n constructor() { this.direct = new.target === Base }\n}\n'
  622. const code = transformModule(source, 'probe.js')
  623. contains('trap 8: new.target survives verbatim', code, 'new.target')
  624. lacks('trap 8: new.target is not replaced by the meta parameter', code, '__dsh$meta')
  625. parsesAsScript('trap 8', code)
  626. const Base = runBody(code).Base as new () => { direct: boolean }
  627. class Derived extends Base {}
  628. check('trap 8: new.target compares true for a direct construction', new Base().direct, true)
  629. check('trap 8: new.target compares false for a subclass', new Derived().direct, false)
  630. }
  631. {
  632. // Shebang handling: `#!` is only legal at offset 0, which the prologue
  633. // occupies. It is commented out in place so both offsets and the line count
  634. // stay put.
  635. const source = '#!/usr/bin/env node\nexport const main = 1\n'
  636. const code = transformModule(source, 'probe.js')
  637. lacks('shebang is not left in the emitted body', code, '#!')
  638. parsesAsScript('shebang', code)
  639. check('shebang: line count still survives', lineCount(code), lineCount(source))
  640. check('shebang: the module still works', runBody(code).main, 1)
  641. }
  642. {
  643. // The exit gate itself: the transform re-parses its own output as a script.
  644. // Any leftover module syntax or mis-spliced interval fails there, not at load.
  645. // Re-checked here over a source that exercises several edits at once.
  646. const source = "import a from 'p'\nexport * from 'q'\nexport const f = async () => { for await (const x of a) { await x } }\n"
  647. parsesAsScript('exit gate over combined edits', transformModule(source, 'probe.js'))
  648. }
  649. // ---------------------------------------------------------------------------
  650. // 10. Caching: the transform memoizes by source text, and the cache must not
  651. // leak a different file's result.
  652. // ---------------------------------------------------------------------------
  653. {
  654. const source = 'export const cached = 1\n'
  655. const first = transformModule(source, 'a.js')
  656. const second = transformModule(source, 'b.js')
  657. check('identical sources return the identical cached body', first === second, true)
  658. // Distinct sources must not collide.
  659. check(
  660. 'distinct sources produce distinct bodies',
  661. transformModule('export const other = 2\n', 'c.js') !== first,
  662. true,
  663. )
  664. }