* build: typecheck the test suites Tests were in no TypeScript program: no tsconfig included tests/** or packages/*/tests/**, and bun strips types without checking them, so a fixture could drop a required field and keep passing until something read it. @types/bun moves to the root because it was installed per package only, and #cli-tests/* joins the paths the root config already carries. * test: fix the type errors the test suites were hiding Typechecking the tests turned up 1123 errors. Most were ordinary strictness, but some were real: `NodeChange` bound to Figma's plugin typings rather than the Kiwi codec in thirteen .fig tests, materializeInstance was called with six arguments against five so the blobs and source children were dropped, CanvasKit pixels were written to a plain object that never reached WASM, and assertions were made through accessors that do not exist, so they asserted nothing. Fixtures that had quietly lost a required field now carry it, nullable results are narrowed through the existing expectDefined helper rather than assumed, and stand-ins for CanvasKit and the editor go through one named helper instead of an unexplained cast at each site. No test was deleted, skipped, or weakened, and no `any`, non-null assertion, or ts-expect-error was introduced. * docs: record what typechecking the tests established Pins the app program's global types with an assertion rather than a note, since an unpinned types list lets any root @types package decide which platform src/** is judged against. The two environment faults that look like code regressions — Vite's dependency pre-bundle outliving a package rebuild, and heavy .fig suites failing under load — go to the development docs, where an explanation belongs. * fix: align @types/bun and keep node types resolvable when extended The root manifest declared a newer @types/bun than every package, which check:monorepo rejects, and pinning the app program's types left them unresolvable from a config that extends this one out of tree. * fix: fail the test typecheck when the compiler itself fails The gate matched diagnostics by substring, so a compiler or config failure that named no test file printed a pass while having checked nothing. Diagnostics are now split by whether they name a file: an unscoped one is the run failing and stops the gate, a test file's is a finding, and a source file's stays out by design. Also drops the parameter planComponentConstruction never read, and makes the inner-shadow verification script exit non-zero when it renders no image instead of logging and succeeding. * chore: merge master into tests-typecheck
171 lines
6.2 KiB
TypeScript
171 lines
6.2 KiB
TypeScript
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'
|
|
import { guidToString } from '@open-pencil/kiwi/fig/guid'
|
|
|
|
import { parseFigBuffer } from '../archive'
|
|
import { createOccurrenceInterpreter } from '../instance-overrides/interpret'
|
|
import type { InterpretInstanceOptions } from '../instance-overrides/occurrence/types'
|
|
import {
|
|
bySavedPosition,
|
|
createSourceIndex,
|
|
type SourceIndex
|
|
} from '../instance-overrides/source-index'
|
|
import { symbolOverridesOf } from '../instance-overrides/types'
|
|
import { applyStyleRefsToFields } from '../node-change/style/refs'
|
|
import {
|
|
resolveDocumentBindingReferences,
|
|
type BindingReferenceDiagnostic
|
|
} from './bindings/references'
|
|
import { planComponentConstruction } from './components'
|
|
import { collectSceneDependencies } from './dependency-closure'
|
|
import { inheritComponentPropertyDefinitions } from './property-inheritance'
|
|
|
|
/** Indexed source document. Resources remain separate from scene occurrences. */
|
|
export function createDocumentReader(source: readonly NodeChange[], pageIds?: ReadonlySet<string>) {
|
|
return createReader(source, 'copy', pageIds)
|
|
}
|
|
|
|
/** Parse into exclusively owned records; callers never receive the mutable source index. */
|
|
export function createArchiveDocumentReader(bytes: ArrayBuffer, pageIds?: ReadonlySet<string>) {
|
|
const parsed = parseFigBuffer(bytes)
|
|
return {
|
|
figKiwiVersion: parsed.figKiwiVersion,
|
|
figSchemaDeflated: parsed.figSchemaDeflated,
|
|
reader: createReader(parsed.nodeChanges, 'transfer', pageIds),
|
|
blobs: parsed.blobs,
|
|
images: parsed.images
|
|
}
|
|
}
|
|
|
|
function createReader(
|
|
source: readonly NodeChange[],
|
|
ownership: 'copy' | 'transfer',
|
|
pageIds?: ReadonlySet<string>
|
|
) {
|
|
const bindingDiagnostics: BindingReferenceDiagnostic[] = []
|
|
const liveSource = source.filter((node) => node.phase !== 'REMOVED')
|
|
const changes = resolveDocumentBindingReferences(
|
|
liveSource,
|
|
(diagnostic) => bindingDiagnostics.push(diagnostic),
|
|
ownership
|
|
)
|
|
// One index over these records serves inheritance, style lookup, the dependency closure
|
|
// and component planning.
|
|
const index = createSourceIndex(changes)
|
|
inheritComponentPropertyDefinitions(changes, index.sources)
|
|
const styles = index.sources
|
|
const assets = new Map<string, string>()
|
|
for (const node of changes)
|
|
if (node.guid && typeof node.key === 'string') {
|
|
const id = guidToString(node.guid)
|
|
assets.set(node.key, id)
|
|
if (typeof node.version === 'string') assets.set(`${node.key}@${node.version}`, id)
|
|
}
|
|
const resolveStyles = (node: NodeChange): void => {
|
|
applyStyleRefsToFields(styles, node, assets)
|
|
for (const override of symbolOverridesOf(node)) resolveStyles(override as NodeChange)
|
|
}
|
|
for (const node of changes) resolveStyles(node)
|
|
return createScopedReader(
|
|
changes,
|
|
bindingDiagnostics,
|
|
pageIds,
|
|
createSharedReaderState(changes, index)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Everything a scoped reader needs that does not depend on which pages are selected. Only
|
|
* the page's own subset varies, so the whole-document work happens once per document.
|
|
*/
|
|
interface SharedReaderState {
|
|
index: SourceIndex
|
|
sourceInterpreter: ReturnType<typeof createOccurrenceInterpreter>
|
|
}
|
|
|
|
function createSharedReaderState(changes: readonly NodeChange[], index: SourceIndex) {
|
|
let sourceInterpreter: SharedReaderState['sourceInterpreter'] | undefined
|
|
return {
|
|
index,
|
|
get sourceInterpreter() {
|
|
sourceInterpreter ??= createOccurrenceInterpreter(
|
|
changes.filter((change) => change.type !== 'VARIABLE' && change.type !== 'VARIABLE_SET')
|
|
)
|
|
return sourceInterpreter
|
|
}
|
|
}
|
|
}
|
|
|
|
function createScopedReader(
|
|
changes: NodeChange[],
|
|
bindingDiagnostics: BindingReferenceDiagnostic[],
|
|
pageIds: ReadonlySet<string> | undefined,
|
|
shared: SharedReaderState
|
|
) {
|
|
const resources = changes.filter(
|
|
(change) => change.type === 'VARIABLE' || change.type === 'VARIABLE_SET'
|
|
)
|
|
const closure = collectSceneDependencies(changes, pageIds, shared.index)
|
|
// Deleted components are interpreted per instance; broken hierarchy is not recoverable.
|
|
if (closure.missingIds.size)
|
|
throw new Error(`Missing reachable sources: ${[...closure.missingIds].join(', ')}`)
|
|
const sceneChanges = changes.filter(
|
|
(change) =>
|
|
change.type !== 'VARIABLE' &&
|
|
change.type !== 'VARIABLE_SET' &&
|
|
(change.type === 'CANVAS' ||
|
|
(change.guid &&
|
|
(closure.contentIds.has(guidToString(change.guid)) ||
|
|
closure.ancestorIds.has(guidToString(change.guid)))))
|
|
)
|
|
const sourceInterpreter = shared.sourceInterpreter
|
|
const interpreter = createOccurrenceInterpreter(sceneChanges)
|
|
const pages = changes
|
|
.filter((change) => change.type === 'CANVAS')
|
|
.toSorted(bySavedPosition)
|
|
.map((page) => {
|
|
if (!page.guid) throw new Error('Page has no GUID')
|
|
return {
|
|
id: guidToString(page.guid),
|
|
name: page.name ?? '',
|
|
position: page.parentIndex?.position ?? null,
|
|
internalOnly: page.internalOnly === true
|
|
}
|
|
})
|
|
const knownPageIds = new Set(pages.map((page) => page.id))
|
|
return {
|
|
selectPages(ids: ReadonlySet<string>) {
|
|
return createScopedReader(changes, bindingDiagnostics, ids, shared)
|
|
},
|
|
get sourceRecords() {
|
|
return structuredClone(changes)
|
|
},
|
|
get documentRecord() {
|
|
return structuredClone(changes.find((change) => change.type === 'DOCUMENT'))
|
|
},
|
|
dependencyClosure: closure,
|
|
pages,
|
|
get resources() {
|
|
return structuredClone(resources)
|
|
},
|
|
bindingDiagnostics,
|
|
readPage(id: string, options: InterpretInstanceOptions = {}) {
|
|
if (!knownPageIds.has(id)) throw new Error(`Unknown page ${id}`)
|
|
const page = interpreter.page(id, options)
|
|
// A slot content frame is read through the instance assigning it, never as a layer.
|
|
page.children = page.children.filter((child) => child.properties.isSlotContent !== true)
|
|
return page
|
|
},
|
|
planComponents(
|
|
roots: readonly ReturnType<typeof interpreter.page>[],
|
|
options: InterpretInstanceOptions = {}
|
|
) {
|
|
return planComponentConstruction(
|
|
roots,
|
|
(id) => sourceInterpreter.component(id, options),
|
|
shared.index.sources
|
|
)
|
|
},
|
|
readComponent: sourceInterpreter.component
|
|
}
|
|
}
|