|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Published-declaration gate. |
| 4 | + * |
| 5 | + * A library's emitted `.d.ts` is public contract: it must compile for a consumer |
| 6 | + * on any supported peer version and any reasonable tsconfig, never forcing a |
| 7 | + * setting (skipLibCheck, a specific @types version) to dodge errors that |
| 8 | + * originate in the library's OWN types. The type-contract suite only runs against |
| 9 | + * the pinned `@types/react` 18, so a name that exists there but not on a newer |
| 10 | + * major slips straight into `dist` -- `propTypes?: React.WeakValidationMap<P>` |
| 11 | + * compiled clean on 18 and shipped a `TS2694` into every strict React 19 |
| 12 | + * consumer's build (`WeakValidationMap` was removed on `@types/react` 19). |
| 13 | + * |
| 14 | + * This checks the built declarations under `skipLibCheck: false` against the |
| 15 | + * minimum and maximum supported `@types/react` majors, failing on any diagnostic |
| 16 | + * located in the package's own `dist/`. Errors inside dependency `.d.ts` |
| 17 | + * (react-native, node/dom global collisions) are filtered out: they are |
| 18 | + * pre-existing ecosystem conflicts, not this library's contract, and letting them |
| 19 | + * through would both mask a real break and manufacture false ones. `skipLibCheck: |
| 20 | + * true` is NOT a valid gate here -- it suppresses the very declaration errors this |
| 21 | + * catches (verified: the WeakValidationMap break reports zero errors under it). |
| 22 | + * |
| 23 | + * Isolation matters, and is why the peer majors are NOT project devDependencies. |
| 24 | + * The repo pins `@types/react` 18; adding a second copy under `node_modules/@types` |
| 25 | + * would make TypeScript auto-include BOTH majors' ambient globals into every normal |
| 26 | + * type-check at once (the repo leaves `types` unset), so "which React is in scope" |
| 27 | + * would be both, and only `skipLibCheck: true` would hide the resulting global |
| 28 | + * collisions. Instead the min resolves from the project's own pinned copy and the |
| 29 | + * max is installed into a gitignored, path-scoped folder that no other tsconfig |
| 30 | + * sees. Each run compiles against EXACTLY ONE major, chosen by explicit `paths` |
| 31 | + * with `types: ['node']` so no `@types/react` is ambiently included, and prints the |
| 32 | + * resolved version so the log always says what was tested. |
| 33 | + * |
| 34 | + * Requires `pnpm build` first. |
| 35 | + */ |
| 36 | +import { execFileSync } from 'node:child_process'; |
| 37 | +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 38 | +import { createRequire } from 'node:module'; |
| 39 | +import { dirname, join, relative, resolve } from 'node:path'; |
| 40 | +import { fileURLToPath } from 'node:url'; |
| 41 | + |
| 42 | +const require = createRequire(import.meta.url); |
| 43 | +const pkgRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| 44 | +const distDir = join(pkgRoot, 'dist'); |
| 45 | +const workDir = join(pkgRoot, '.type-check-dist'); |
| 46 | +const tscPath = require.resolve('typescript/lib/tsc.js'); |
| 47 | + |
| 48 | +if (!existsSync(join(distDir, 'index.d.ts'))) { |
| 49 | + console.error('type-check-dist: dist/index.d.ts missing -- run `pnpm build` first.'); |
| 50 | + process.exit(1); |
| 51 | +} |
| 52 | + |
| 53 | +// The maximum-major @types to test, pinned so a run is deterministic and the log |
| 54 | +// says exactly what was checked. Bump deliberately when raising the supported |
| 55 | +// ceiling; the minimum comes from the project's own pinned `@types/react`. |
| 56 | +const MAX = { react: '@types/[email protected]', reactDom: '@types/[email protected]' }; |
| 57 | + |
| 58 | +const pkgDir = spec => dirname(require.resolve(`${spec}/package.json`)); |
| 59 | +const versionAt = dir => JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).version; |
| 60 | + |
| 61 | +/** Install the max-major @types into a path-scoped folder no other tsconfig sees. */ |
| 62 | +function isolatedMax() { |
| 63 | + const root = join(workDir, 'max'); |
| 64 | + if (!existsSync(join(root, 'node_modules', '@types', 'react', 'package.json'))) { |
| 65 | + mkdirSync(root, { recursive: true }); |
| 66 | + writeFileSync(join(root, 'package.json'), '{"private":true}\n'); |
| 67 | + execFileSync('npm', ['install', '--no-save', '--no-package-lock', MAX.react, MAX.reactDom], { |
| 68 | + cwd: root, |
| 69 | + stdio: 'ignore', |
| 70 | + }); |
| 71 | + } |
| 72 | + const nm = join(root, 'node_modules', '@types'); |
| 73 | + return { react: join(nm, 'react'), reactDom: join(nm, 'react-dom') }; |
| 74 | +} |
| 75 | + |
| 76 | +// Only react/react-dom are pinned per target; csstype, @types/node, stylis and |
| 77 | +// react-native resolve the normal way so the two runs differ by exactly the |
| 78 | +// surface under test. `types: ['node']` keeps @types/node's ambient globals |
| 79 | +// (dist references the NodeJS namespace) while excluding every @types/react from |
| 80 | +// the ambient set, so the React in scope is purely what `paths` names. |
| 81 | +function configFor(react, reactDom) { |
| 82 | + return { |
| 83 | + compilerOptions: { |
| 84 | + baseUrl: pkgRoot, |
| 85 | + jsx: 'react-jsx', |
| 86 | + lib: ['ES2021', 'DOM'], |
| 87 | + module: 'ESNext', |
| 88 | + moduleResolution: 'bundler', |
| 89 | + noEmit: true, |
| 90 | + paths: { |
| 91 | + react: [react], |
| 92 | + 'react/jsx-runtime': [join(react, 'jsx-runtime')], |
| 93 | + 'react/jsx-dev-runtime': [join(react, 'jsx-dev-runtime')], |
| 94 | + 'react-dom': [reactDom], |
| 95 | + 'react-dom/server': [join(reactDom, 'server')], |
| 96 | + 'react-dom/client': [join(reactDom, 'client')], |
| 97 | + }, |
| 98 | + skipLibCheck: false, |
| 99 | + strict: true, |
| 100 | + target: 'ES2020', |
| 101 | + types: ['node'], |
| 102 | + }, |
| 103 | + // Both published entry points; the native entry is what pulls react-native in. |
| 104 | + files: [join(distDir, 'index.d.ts'), join(distDir, 'native', 'index.d.ts')], |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +/** Lines like `path/to/file.d.ts(12,5): error TS2694: ...`; keep only own-dist ones. */ |
| 109 | +function ownDistErrors(output) { |
| 110 | + const out = []; |
| 111 | + for (const line of output.split('\n')) { |
| 112 | + const m = /^(.*\.d\.ts)\(\d+,\d+\): error TS/.exec(line); |
| 113 | + if (!m) continue; |
| 114 | + const abs = resolve(pkgRoot, m[1]); |
| 115 | + if (abs.startsWith(distDir)) out.push(` ${relative(pkgRoot, abs)}${line.slice(m[1].length)}`); |
| 116 | + } |
| 117 | + return out; |
| 118 | +} |
| 119 | + |
| 120 | +mkdirSync(workDir, { recursive: true }); |
| 121 | + |
| 122 | +const min = { react: pkgDir('@types/react'), reactDom: pkgDir('@types/react-dom') }; |
| 123 | +const max = isolatedMax(); |
| 124 | +const targets = [ |
| 125 | + { kind: 'min', ...min }, |
| 126 | + { kind: 'max', ...max }, |
| 127 | +]; |
| 128 | + |
| 129 | +let failed = false; |
| 130 | +for (const target of targets) { |
| 131 | + const cfg = join(workDir, `tsconfig.${target.kind}.json`); |
| 132 | + writeFileSync(cfg, `${JSON.stringify(configFor(target.react, target.reactDom), null, 2)}\n`); |
| 133 | + |
| 134 | + let output = ''; |
| 135 | + try { |
| 136 | + execFileSync(process.execPath, [tscPath, '-p', cfg], { encoding: 'utf8' }); |
| 137 | + } catch (error) { |
| 138 | + output = `${error.stdout ?? ''}${error.stderr ?? ''}`; |
| 139 | + } |
| 140 | + |
| 141 | + const label = `@types/react ${versionAt(target.react)}`; |
| 142 | + const errors = ownDistErrors(output); |
| 143 | + if (errors.length) { |
| 144 | + failed = true; |
| 145 | + console.error(`type-check-dist: ${label} -- ${errors.length} error(s) in dist/:`); |
| 146 | + console.error(errors.join('\n')); |
| 147 | + } else { |
| 148 | + console.log(`type-check-dist: ${label} -- dist declarations compile clean`); |
| 149 | + } |
| 150 | + rmSync(cfg, { force: true }); |
| 151 | +} |
| 152 | + |
| 153 | +process.exit(failed ? 1 : 0); |
0 commit comments