@unframework.dev/ir
v0.5.0
Published
The Unframework intermediate representation: plain-data types, builders, visitors and the JSON Schema that every IR snapshot is validated against.
Readme
@unframework.dev/ir
The Unframework intermediate representation: what the analyser knows about a .uf.tsx module, as
plain, versioned, schema-validated JSON (plan §5.3).
- Types:
UfModule(its components, exports and the local type declarations their props, events and setup use),UfComponent(props, the props parameter, the eventsdefineEmitsdeclares, bindings, the setup and a render root that is an element or a rootFragment), the render nodes (Element,Text,Interpolation,If,For) and the attribute kinds (Static,Bound,Class,Style,Spread, the listeners and template refs,EventandRef, and an element's markup,RawHtml, ADR-0097). A binding is a prop, a loop variable, aref(state), acomputed(derived), a template ref, a setupconst, function orlet, oremit, by itsname@offsetid. The setup is a list of items in source order (State,Derived,TemplateRef,Id,Const,Variable,Function,Watch,WatchEffect,Lifecycle, ADR-0045, andDevCheck, a top-levelif (isDev) { … }, ADR-0117). AnExpressionis a template's source text at its span, with every identifier resolved to a binding or an allowed global; setupCodealso resolves writes, emits,nextTick,isDevas anif's test (Dev) and a handler's event members, and aFunctionCodekeeps a function's parameters, flags and body apart, for the targets that print it from its parts. A ref's value is read ascount.value, which its reference spans. - Composition (ADR-0055): a module's
importsof other.uf.tsxmodules, each with theModuleApithe compiler's resolver returned (ADR-0053), and its injectionkeys; a component'sslots,exposesandinheritAttrs, a slotrepeatedwhere the component may render it more than once at a time, andinFillwhere it renders it inside a fill a child renders, in its declaration and its API (ADR-0126, ADR-0128); the render nodesComponent,SlotOutletandDynamic(<component is>, withonly, the attributes only some of its tags take, ADR-0119), aComponentApi.rootof"dynamic"with itsrootTags, the render nodeTeleport(<Teleport to>, one element mounted elsewhere, ADR-0098), the render nodeTransition(<Transition name appear>, Vue's classes on one element or one conditional of elements, ADR-0100), theDirectiveattribute and a module'sdirectives, each with its hooks' code (v-focus-trap={open.value}, ADR-0099), a list's body that is a component, a component's attributes (Prop,Listener,ModelBinding,Class,Style,Ref) and fills, an element'sv-model(Model), the setup itemsModel,ProvideandInject, the binding kindsmodel,slots,slotScope,contextandcomponent, and a slot's presence (Slotreferences). The analyser lowers none of it yet: M3's lanes do. - Async setup (ADR-0101): the setup item
Await(const user = await loadUser(props.id)), whosevalueis the awaited expression withoutawait, its binding kindawaited, a read-only constant, andComponentApi.async, set when a component's setup awaits, so a parent in another file knows. - Styles (ADR-0063): a component's
styles, the sibling stylesheets it imports, each with its specifier, its text and its span, and itsscopeId; a child'sComponentApi.styles, its scope id and whether its sheets use:slotted(); a module'spassThroughImports, of any module that is neither relative nor absolute (a styling engine'svirtual:modules, ADR-0064), whose names a template reads throughImportreferences; and the forms ADR-0038 and ADR-0039 defer: astylebound to one value (StyleAttribute.value), aclasspart that may be an array or an object (DynamicClass.collection), a spread'sstylekey and vendor-prefixed properties, which the analyser lowers since ADR-0065. - Cross-file types, generics, composables and shared modules (ADR-0067 to ADR-0070): a
module's
typeImportsandtypeExports(type re-exports, and exported types no component reaches), itsmoduleImportsof values from relative.tsmodules and its top-levelconstants, which components read throughimportandmoduleConstbindings, and a.uf.tsmodule'scomposables(UfComposable: type parameters, parameters of avalue,getterorcallbackkind read throughargumentbindings, a setup and the members it returns); a component'stypeParameters, and a child's inComponentApi.typeParameters; a prop'sdeclaredBy, where another declaration declares the member, whose type is then that file's text at the props annotation's span; the names a slot's props, an API member and a key reference (references), and the literals of a child's prop whose alias names a union of them (ApiMember.literals), for UF3039; a.uf.tsmodule'sModuleApi.composables(ComposableApi, withhasEffects), imported byComposablenames; and theUsesetup item, a composable's call whose members areusedState,usedDerived,usedFnandusedConstbindings. Every new field is absent when empty. The analyser fills all of them, and every target emits them, each composable through the target'semitComposable. - Builders: one per type (
createModule,createComponent,createElement,createFor,createExpression,createCode,createWriteReference,createFunctionCode,createStateItem,createEventAttribute, …). They write keys in the order of the types, which is the order of everyir.jsonsnapshot, and leave an absent optional field out. - Visitors:
walk(every container, the root fragment included),childrenOf,collectFeatures(node, attribute, binding, setup item, handler, watch source and code reference kinds, which the coverage gate reads),expressionsOf(every template expression of a component with its JSON Pointer),codeOfandfunctionsOf(the setup's and the handlers' code and functions, each with the context it runs in:purefor what the setup evaluates,clientfor what runs in the browser), andspansOf(every span of a module).summarizesays what each local function does, itself and through the functions it calls (writes, emits, reads, calls,nextTick,await, template refs, setuplets, client-only globals, whether it escapes as a value): the analyser, the invariants and every target judge a call by it, and the IR holds no summary a plugin could falsify.summarizeTrackedleaves out the references markedlater(in a function client code hands to a timer,then,addEventListeneroronCleanup, ADR-0048): what awatchEffecttracks, which every target lists as its dependencies. - Schema:
irSchema(also@unframework.dev/ir/schema.json), generated from the types withpnpm --filter @unframework.dev/ir generate. A test fails when it is stale.validateModulechecks a value against it without dependencies (the compiler checks what plugins return with it): a node'skindpicks its branch of a union, and a key set toundefinedis absent, as in JSON. - Invariants:
checkInvariantschecks what the types document and the schema cannot say: component and export names every target can write; HTML elements a component can render, and SVG elements inside an<svg>; an element's own attributes, set once across every kind (a spread'sclassmerges with the element's);truefor exactly the HTML boolean attributes; a canonicalclassand astylewhose declarations do not overlap; nothing the targets render differently (portability.ts), bound or static; no code or document the compiler cannot analyse (srcdoc,javascript:URLs,data:URLs a frame loads); text where every target renders it alike, with only characters HTML keeps; props every target can declare, with static defaults, and a localPropsonly as the props type of every component its props reach; stylesheets only in a one-component module, with a scope id exactly when there are any, and pass-through imports whose names no other import binds and only a template reads; bindings in scope, and code that fills its span (a prop in the object form as the parameter's member, a ref's value asname.value); a read's narrowed paths (narrowed, ADR-0046), each the read or a member path off it, narrowed by the template only in a handler's code and across a closure only as a destructured prop's own read, and a write's narrowed target only where its operator reads it (+=,++); the shapes of conditionals, lists and fragments; and loop variables no target's rewrite captures, keyed by their own list's item or index. For the setup (ADR-0045 to ADR-0049): items in source order, each declaring one binding of its kind, named apart from the component's other names and from what the targets reserve; code that runs where it may (templates and what the setup evaluates write, emit and await nothing, read no template ref or setuplet, call only pure local functions, a getter only those that read static values, and read only what is declared before them); writes of astate, amodelor alet, emits of a declared event with as many arguments as it takes, in client code only; an immediate watcher safe on the server, and DOM reads only after the DOM updates; functions with the parameters their role takes; listeners of an event of the vocabulary, with one option at most, whose handlers use only the event members every target's event carries and list their leadingpreventDefault()andstopPropagation()calls; and each template ref attached by one element outside any list and Angular's literal region. For composition (ADR-0055): a component element names exactly one imported or local component, and each prop, listener, model and fill a declaration of its API; a slot outlet, a slot's presence and a forwarded fill name a declared slot; a model's binding is astate's or amodel's.value; an injected value is never written; aDynamicnode's candidates are all tags, whose attributes and children each tag keeps as an element, or all components; aTeleport(ADR-0098) holds exactly one element, outside SVG, and itstois not empty; aTransition(ADR-0100) holds one element, or oneIfwhose every branch holds one element or nothing, outside a list's body, and itsnamehas no whitespace; noAwaitfollows another setup item, and anAwait's value reads only props, module values, globals and earlierawaitedbindings (ADR-0101); aDirectivenames a directive the module declares or imports, once per element, in a list's body too (ADR-0108), and a directive's hooks read their own names, client globals and the bindings it lists (ADR-0099); slots, exposed functions and models are declared once, a model named apart from the props;provideandinjectname a key the module declares or imports, and a key of a ref (InjectionKey<Ref<number>>, ADR-0054) is provided, or injected with a fallback, as astate,derivedormodelbinding by its name, and its injected binding read asx.value; and av-model's control is one its element has. For M5's contract (ADR-0067 to ADR-0070): a prop another file declares is exempt from the source-slice rule; type parameters, a slot's references, and the names the type and value imports, exports and constants bind are identifiers, each once; a constant's code reads only pure globals, the other constants and the value imports; animportormoduleConstbinding is one the module declares, and only a composable declares anargument; aUseitem calls an imported composable with an argument of each parameter's kind, destructures members its API returns into bindings of their kinds and copieshasEffects; a composable's ref is read asx.value, and its function where the setup evaluates code only when pure; and a composable's setup keeps a component's rules, with its arguments, its items and the members it returns. The compiler emits from no module that breaks one, a plugin's included, and checks itself that a plugin's code is the analyser's. - Portability facts: what the targets render differently from the same markup
(
portability.ts): elements Vue does not know, template syntax, attributes a framework acts on or sets as state,contenteditablewith children, empty URLs React drops, whitespace Svelte drops, attributes Angular cannot bind, text the parser moves or drops, and the root elements tied to their parent that Angular's host breaks (CONTEXTUAL_ROOT_ELEMENTS). The analyser reports each at its source, and the invariants reject them in any IR; but for a boundvaluethat may be nullish where some targets set the property (NULLISH_VALUE_ELEMENTS), which the analyser alone can tell: the IR holds no kinds. - Vocabulary: the HTML elements, attributes and content-model facts the analyser validates
against, and the printers and targets lay out by (
html.ts), with the boolean attributes a value can be bound to and the attributes React and Qwik type as numbers; the SVG elements and attributes, case-exact (svg.ts); CSS facts: unitless properties, shorthands as Chromium expands them, and what a static value may hold (css.ts); the DOM events a listener may take with their interfaces, the window's and the unsupported ones apart, the events a listener may be passive on, and the event members every target's event object carries for each (events.ts:DOM_EVENTS,PORTABLE_EVENT_MEMBERS,PORTABLE_EVENT_INTERFACES); the globals each context may read (ALLOWED_GLOBALSin templates,PURE_GLOBALSin what the setup evaluates,CLIENT_GLOBALSin client code: every name lib.dom declares,LIB_DOM_GLOBALSinbrowser.ts, but thewindowmembers read only throughwindow,WINDOW_MEMBER_GLOBALS; with theBROWSER_GLOBALSandSCHEDULING_GLOBALSa server-safe watcher may not, andreadsDom, the reads that see what a render changes) and the reserved prop, parameter and setup binding names (names.ts). Its tables are maps, read with names from the source.ID_REFERENCE_ATTRIBUTESandidReferencesInare how the analyser and the tests' normaliser both find id references (the generated-id prefix, ADR-0031).listBoxSizereads a<select>as HTML does: a single-selection list box with an option a drop-down would select starts with none selected (thelistboxcapability), and what is known only at run time counts as one.
The IR has no dependencies and imports nothing from TypeScript: it is pure data. The tables say where each comes from; the analyser's conformance tests check them against the frameworks and parse5.
