marko
Version:
Optimized runtime for Marko templates.
139 lines (138 loc) • 7.01 kB
TypeScript
import { types as t } from "@marko/compiler";
import type { AccessorPrefix } from "../../common/accessor.debug";
import type { WalkCode } from "../../common/types";
import * as ContentType from "./constants/content-type";
import type * as Step from "./constants/step";
import * as StructureKind from "./constants/structure-kind";
import { type OneMany, type Opt, Sorted } from "./optional";
import { type Binding, type InputBinding, type KnownExprs, type ParamBinding, type ReferencedBindings, type Sources } from "./references";
import { type SerializeKey, type SerializeReason, type SerializeReasons } from "./serialize-reasons";
export interface ParamSerializeReasonGroup {
id: symbol;
reason: NonNullable<Sources["param"]>;
}
export type ParamSerializeReasonGroups = [
ParamSerializeReasonGroup,
...ParamSerializeReasonGroup[]
];
type ContentType = ContentType.Value;
export { ContentType, StructureKind };
export type StructureRef = StructureSectionRef | StructureExportRef;
export interface StructureSectionRef {
kind: typeof StructureKind.SectionRef;
section: Section;
}
export interface StructureExportRef {
kind: typeof StructureKind.ExportRef;
program: t.ProgramExtra;
path: string;
hint: string;
}
export type StructureOp = string | Step.Value | StructureText | StructureVisit | StructureChild;
export interface StructureText {
kind: typeof StructureKind.Text;
value: string;
}
export interface StructureVisit {
kind: typeof StructureKind.Visit;
code: typeof WalkCode.Get | typeof WalkCode.Replace | typeof WalkCode.DynamicTagWithVar;
claimed: boolean;
}
export interface StructureChild {
kind: typeof StructureKind.Child;
name: string;
hasVar: boolean;
renderer?: StructureRef;
}
export interface Section {
id: number;
name: string;
loc: t.SourceLocation | undefined;
depth: number;
parent: Section | undefined;
program: Section;
sectionAccessor: {
binding: Binding;
prefix: AccessorPrefix;
} | undefined;
params: undefined | ParamBinding | InputBinding;
referencedLocalClosures: ReferencedBindings;
referencedClosures: ReferencedBindings;
referencedHoists: ReferencedBindings;
bindings: ReferencedBindings;
hoisted: ReferencedBindings;
hoistedTo: ReferencedBindings;
serializeReason: undefined | SerializeReason;
serializeReasons: Map<symbol, SerializeReason>;
/** Reasons any of the section's dom nodes resumes, as the analyzed reasons
* (not merged) so each one's guard stays buildable. */
domSerializeReasons: undefined | SerializeReasons;
/** Pending serialize exprs, resolved into the reasons (and provenance)
* once references finalize. */
serializeExprs: Opt<t.NodeExtra>;
propSerializeExprs: Map<SerializeKey, OneMany<t.NodeExtra>> | undefined;
/** Whose values feed each serialization decision — survives force-`true`
* and counts function-body reads; complete after reference finalize. */
serializeProvenance: Sources | undefined;
propSerializeProvenance: Map<SerializeKey, Sources> | undefined;
/** Interned per-prop reason keys for string/symbol props. */
serializePropKeys: Map<string | symbol, SerializeKey> | undefined;
paramReasonGroups: ParamSerializeReasonGroups | undefined;
returnValueExpr: t.NodeExtra | undefined;
returnSerializeReason: SerializeReason | undefined;
isHoistThrough: true | undefined;
upstreamExpression: t.NodeExtra | undefined;
/** The content's rendering tag (its extra), and the child binding the
* content feeds when the child can serialize it. */
downstream: {
tag: t.MarkoTagExtra;
binding: Binding | undefined;
properties: Opt<string>;
exprs: KnownExprs | undefined;
} | undefined;
hasAbortSignal: boolean;
/** Count of distinct `$signal` expression roots; analyze allocates each
* root's `abortId` from this so translates read, never re-derive. */
abortSignalExprs: number;
readsOwner: boolean;
isBranch: boolean;
content: null | {
startType: ContentType;
endType: ContentType;
singleChild: boolean;
};
structure: StructureOp[] | null;
}
declare module "@marko/compiler/dist/types" {
interface ProgramExtra {
section?: Section;
sections?: Section[];
}
interface MarkoTagBodyExtra {
section?: Section;
}
}
export declare const sectionUtil: Sorted<Section>;
export declare function startSection(path: t.NodePath<t.MarkoTagBody | t.Program>): Section | undefined;
export declare function getOrCreateSection(path: t.NodePath<any>): Section;
export declare function getSectionForBody(body: t.NodePath<t.MarkoTagBody | t.Program>): Section | undefined;
export declare function getSection(path: t.NodePath): Section;
export declare const getScopeIdIdentifier: (section: Section) => t.Identifier;
export declare const getSectionParentIsOwner: (section: Section) => boolean, setSectionParentIsOwner: (section: Section, value: boolean) => void;
export declare const getBranchRendererArgs: (section: Section) => [template?: t.Expression | undefined, walks?: t.Expression | undefined, setup?: t.Expression | undefined, params?: t.Expression | undefined], setBranchRendererArgs: (section: Section, value: [template?: t.Expression | undefined, walks?: t.Expression | undefined, setup?: t.Expression | undefined, params?: t.Expression | undefined]) => void;
export declare function forEachSection(fn: (section: Section) => void): void;
export declare function getChildSections(section: Section): Section[];
export declare function forEachSectionReverse(fn: (section: Section) => void): void;
export declare function getNodeContentType(path: t.NodePath<t.Statement>, extraMember: "startType" | "endType", contentInfo?: Section["content"]): ContentType.Value | null;
export declare function getSectionRegisterReasons(section: Section): false | SerializeReason;
export declare function isImmediateOwner(section: Section, binding: Binding): boolean;
export declare function isDirectClosure(section: Section, closure: Binding): boolean;
export declare function isDynamicClosure(section: Section, closure: Binding): boolean;
export declare function getDynamicClosureIndex(closure: Binding, closureSection: Section): number;
export declare function getDirectClosures(section: Section): Opt<Binding>;
export declare function isSameOrChildSection(section: Section, other: Section): boolean;
export declare function getCommonSection(section: Section, other: Section): Section;
export declare function finalizeParamSerializeReasonGroups(section: Section): void;
export declare function ensureReasonGroups(reason: Section["serializeReason"]): void;
export declare function getParamReasonGroupIndex(section: Section, reason: ParamSerializeReasonGroup["reason"]): number;
export declare function groupParamsBySection(params: Sources["param"]): Map<Section, OneMany<InputBinding | ParamBinding>>;