transform.spec.ts 31 KB

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