mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* fix(core): a sub-composition follows its host clip when the host moves without a reload * perf(studio): move live tweens in place for a sub-composition host's clip too * fix(core): keep sub-compositions nested where a fresh load nests them across rebinds and re-runs * fix(studio): match the preview's re-printed script by its tokens and check every other timeline * fix: compare preview scripts by syntax tree and keep scenes a re-run script places * fix(parsers): keep a tagged template's raw text and a __proto__ key in the script compare * fix(parsers): keep only a tagged template's raw text in the script compare * perf(studio): read each preview script once across repeat timeline drags * style(studio): format the preview script read test * perf(studio): keep only script shapes and refresh them on reuse
43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
import * as acorn from "acorn";
|
|
|
|
type Shape = { type?: unknown; key?: { name?: unknown } };
|
|
|
|
type TaggedTemplate = { type: "TaggedTemplateExpression"; quasi: { quasis: { value: object }[] } };
|
|
|
|
const isTaggedTemplate = (value: unknown): value is TaggedTemplate =>
|
|
(value as { type?: unknown } | null)?.type === "TaggedTemplateExpression";
|
|
|
|
/** What a re-print changes without changing behaviour; only a tag reads a template's raw text. */
|
|
const printOnly = (tagged: WeakSet<object>) =>
|
|
new Map<string, (holder: Shape) => boolean>([
|
|
["start", () => true],
|
|
["end", () => true],
|
|
["raw", (holder) => !tagged.has(holder)],
|
|
["shorthand", (holder) => holder.key?.name !== "__proto__"],
|
|
]);
|
|
|
|
const isNumberLiteral = (holder: Shape, key: string, value: unknown) =>
|
|
key === "value" && typeof value === "number" && holder.type === "Literal";
|
|
|
|
/**
|
|
* A script's syntax tree as one string, or null when it does not parse. Comments, layout, quote
|
|
* style, parentheses and semicolons drop out; `maskNumbers` also hides every number literal.
|
|
*/
|
|
export function scriptShape(code: string, maskNumbers = false): string | null {
|
|
let tree: acorn.Node;
|
|
try {
|
|
tree = acorn.parse(code, { ecmaVersion: "latest", allowHashBang: true });
|
|
} catch {
|
|
return null;
|
|
}
|
|
const tagged = new WeakSet<object>();
|
|
const dropped = printOnly(tagged);
|
|
return JSON.stringify(tree, function (this: Shape, key: string, value: unknown) {
|
|
if (dropped.get(key)?.(this)) return undefined;
|
|
if (isTaggedTemplate(value)) for (const quasi of value.quasi.quasis) tagged.add(quasi.value);
|
|
if (typeof value === "bigint") return `${value}n`;
|
|
if (maskNumbers && isNumberLiteral(this, key, value)) return "num";
|
|
return value;
|
|
});
|
|
}
|