|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Boot-path contract: importing @objectstack/lint must NOT load the TypeScript |
| 4 | +// compiler. The package sits on the kernel boot path, and `typescript` is |
| 5 | +// ~9 MB (and has been pruned from production images before — see |
| 6 | +// validate-react-page-props.ts); it must load lazily, on the first validated |
| 7 | +// `kind:'react'` page. |
| 8 | +// |
| 9 | +// Guarded at three levels because vitest inlines static imports through its |
| 10 | +// transform (they never hit the native require cache), so an in-worker |
| 11 | +// require.cache probe alone CANNOT catch a reintroduced eager import: |
| 12 | +// 1. structural — no src file may eagerly `import ... from 'typescript'` |
| 13 | +// (only `import type`, which erases at build); |
| 14 | +// 2. built dist — child `node` processes import both dist formats and prove |
| 15 | +// the compiler is absent until a react page is validated (skipped when |
| 16 | +// dist has not been built); |
| 17 | +// 3. behavioral — the lazy path really loads the compiler on demand and the |
| 18 | +// gate still produces findings. |
| 19 | +import { execFileSync } from 'node:child_process'; |
| 20 | +import { existsSync, readFileSync, readdirSync } from 'node:fs'; |
| 21 | +import { createRequire } from 'node:module'; |
| 22 | +import { dirname, join } from 'node:path'; |
| 23 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 24 | +import { describe, it, expect } from 'vitest'; |
| 25 | + |
| 26 | +const srcDir = dirname(fileURLToPath(import.meta.url)); |
| 27 | +const distDir = join(srcDir, '..', 'dist'); |
| 28 | + |
| 29 | +describe('lazy typescript loading (kernel boot-path contract)', () => { |
| 30 | + it('no src file eagerly imports typescript (import type only)', () => { |
| 31 | + // Static `import`/`export ... from 'typescript'` executes at module init in |
| 32 | + // both dist formats; `import type` erases. Dynamic import()/createRequire |
| 33 | + // inside functions are the sanctioned lazy forms and are not matched. |
| 34 | + const eagerTsImport = |
| 35 | + /^\s*(?:import\s+['"]typescript['"]|(?:import|export)\s+(?!type[\s{])[^;]*?from\s*['"]typescript['"]|import\s+\w+\s*=\s*require\(\s*['"]typescript['"]\s*\))/m; |
| 36 | + const offenders = readdirSync(srcDir) |
| 37 | + .filter((f) => f.endsWith('.ts') && !f.endsWith('.test.ts')) |
| 38 | + .filter((f) => eagerTsImport.test(readFileSync(join(srcDir, f), 'utf8'))); |
| 39 | + expect(offenders).toEqual([]); |
| 40 | + }); |
| 41 | + |
| 42 | + const reactStack = `{ pages: [{ name: 'r', kind: 'react', source: 'function Page(){ return <ObjectForm mode="edit" />; }' }] }`; |
| 43 | + const childBody = ` |
| 44 | + const probe = require('node:module').createRequire(process.cwd() + '/probe.js'); |
| 45 | + const tsLoaded = () => Object.keys(probe.cache ?? {}).some((p) => /[/\\\\]node_modules[/\\\\]typescript[/\\\\]/.test(p)); |
| 46 | + const fail = (msg) => { console.error(msg); process.exit(1); }; |
| 47 | + const check = (mod) => { |
| 48 | + if (tsLoaded()) fail('typescript was loaded eagerly, at import time'); |
| 49 | + const findings = mod.validateReactPageProps(${reactStack}); |
| 50 | + if (!tsLoaded()) fail('typescript was not loaded by a react-page validation'); |
| 51 | + if (!findings.some((f) => f.rule === 'react-prop-missing-required')) fail('gate produced no finding'); |
| 52 | + console.log('OK'); |
| 53 | + }; |
| 54 | + `; |
| 55 | + |
| 56 | + it.skipIf(!existsSync(join(distDir, 'index.cjs')))('built CJS dist does not load typescript until a react page is validated', () => { |
| 57 | + const out = execFileSync( |
| 58 | + process.execPath, |
| 59 | + ['-e', `${childBody}; check(require(${JSON.stringify(join(distDir, 'index.cjs'))}));`], |
| 60 | + { encoding: 'utf8' }, |
| 61 | + ); |
| 62 | + expect(out).toContain('OK'); |
| 63 | + }); |
| 64 | + |
| 65 | + it.skipIf(!existsSync(join(distDir, 'index.js')))('built ESM dist does not load typescript until a react page is validated', () => { |
| 66 | + const out = execFileSync( |
| 67 | + process.execPath, |
| 68 | + [ |
| 69 | + '--input-type=module', |
| 70 | + '-e', |
| 71 | + `import { createRequire } from 'node:module'; |
| 72 | + const require = createRequire(process.cwd() + '/probe.js'); |
| 73 | + ${childBody}; |
| 74 | + check(await import(${JSON.stringify(pathToFileURL(join(distDir, 'index.js')).href)}));`, |
| 75 | + ], |
| 76 | + { encoding: 'utf8' }, |
| 77 | + ); |
| 78 | + expect(out).toContain('OK'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('loads the compiler lazily in-process and the gate still works', async () => { |
| 82 | + const req = createRequire(import.meta.url); |
| 83 | + const tsLoaded = () => Object.keys(req.cache ?? {}).some((p) => /[/\\]node_modules[/\\]typescript[/\\]/.test(p)); |
| 84 | + const { validateReactPageProps } = await import('./index.js'); |
| 85 | + |
| 86 | + // Stacks without a react-source page never touch the compiler. |
| 87 | + expect(validateReactPageProps({ pages: [{ name: 'p', kind: 'object' }] })).toEqual([]); |
| 88 | + expect(validateReactPageProps({ pages: [{ name: 'r', kind: 'react', source: ' ' }] })).toEqual([]); |
| 89 | + expect(tsLoaded()).toBe(false); |
| 90 | + |
| 91 | + // The first react page with source pays the cost — and the gate still works. |
| 92 | + const findings = validateReactPageProps({ |
| 93 | + pages: [{ name: 'r', kind: 'react', source: 'function Page(){ return <ObjectForm mode="edit" />; }' }], |
| 94 | + }); |
| 95 | + expect(tsLoaded()).toBe(true); |
| 96 | + expect(findings.some((f) => f.rule === 'react-prop-missing-required' && /objectName/.test(f.message))).toBe(true); |
| 97 | + }); |
| 98 | +}); |
0 commit comments