fix(types): PKG-CHAPTER-04 preserve CJS ESM and legacy consumer contracts

This commit is contained in:
Harvey Zhao committed 2026-09-10 23:29:06 +08:00
1 parent 48660c4e12
commit bab80ff886
41 files changed
+1087 -119

No files matched your search

+5 -4
View File
@@ -1,6 +1,7 @@
import assert from 'node:assert/strict'
import { execFileSync } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import ts from 'typescript'
@@ -8,7 +9,7 @@ import { ensureArchive, hash, readMember, refactorDir } from './releases.mjs'
const root = path.resolve(refactorDir, '..')
const fixtures = path.join(refactorDir, 'fixtures/consumers')
const parent = path.join(refactorDir, '.cache/consumers')
const parent = os.tmpdir()
const reportPath = path.join(refactorDir, 'baselines/consumers.json')
const fixtureNames = ['runtime.cjs', 'public.ts', 'language.ts', 'optional-chapter.ts', 'legacy-plugin.ts']
export const runtimeChecks = [
@@ -22,7 +23,7 @@ export const runtimeChecks = [
export async function runConsumers() {
const releases = JSON.parse(fs.readFileSync(path.join(refactorDir, 'baselines/releases.json'), 'utf8')).releases
fs.mkdirSync(parent, { recursive: true })
const dir = fs.mkdtempSync(path.join(parent, 'published-'))
const dir = fs.mkdtempSync(path.join(parent, 'artplayer-published-'))
try {
const provenance = []
for (const release of releases) {
@@ -68,8 +69,8 @@ export async function runConsumers() {
fixtureHashAlgorithm: 'sha256-lf', fixtures: Object.fromEntries(fixtureNames.map(name => [name, hash(fs.readFileSync(path.join(fixtures, name), 'utf8').replaceAll('\r\n', '\n'))])), runtime, types }
}
finally {
const resolved = path.resolve(dir)
assert(resolved.startsWith(path.resolve(parent) + path.sep) && path.basename(resolved).startsWith('published-'), 'Unsafe consumer cleanup')
const resolved = fs.realpathSync(dir)
assert(path.dirname(resolved) === fs.realpathSync(parent) && path.basename(resolved).startsWith('artplayer-published-'), 'Unsafe consumer cleanup')
fs.rmSync(resolved, { recursive: true, force: true })
}
}
+15
View File
@@ -46,3 +46,18 @@ test('Chapter optional factory parameters preserve strict update and data types
const diagnostics = checkConsumer(ts, 'bundler-esm', unguarded)
assert.equal(diagnostics.length, 3, 'Invalid update, title and null option must each fail')
})
test('Chapter named types and legacy aliases remain strict in modern and old consumers', () => {
const source = fs.readFileSync(path.join(refactorDir, '../test/types/chapter-exports.ts'), 'utf8')
const language = fs.readFileSync(path.join(refactorDir, '../test/types/language-value.ts'), 'utf8')
for (const [compiler, mode] of [[ts, 'nodenext-esm'], [ts, 'nodenext-cjs'], [compat, 'node10-commonjs']]) {
assert.deepEqual(checkConsumer(compiler, mode, source), [])
const invalid = source.replaceAll(/\/\/ @ts-expect-error[^\n]*\n/g, '')
assert.equal(checkConsumer(compiler, mode, invalid).length, 3)
assert.deepEqual(checkConsumer(compiler, mode, language), [])
assert.equal(checkConsumer(compiler, mode, language.replaceAll(/\/\/ @ts-expect-error[^\n]*\n/g, '')).length, 2)
}
const commonjs = fs.readFileSync(path.join(refactorDir, '../test/types/commonjs.cts'), 'utf8')
assert.deepEqual(checkConsumer(ts, 'nodenext-cjs', commonjs), [])
assert.equal(checkConsumer(ts, 'nodenext-cjs', commonjs.replaceAll(/\/\/ @ts-expect-error[^\n]*\n/g, '')).length, 1)
})