mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 19:36:04 +00:00
* feat(inspector): query timelines and inspect the live preview * fix(inspector): align live timeline contracts Keep realm-safe target detection through the restack and align floating-point interval boundaries. Update the live group fixture and make the private runtime entry build itself before use. * fix(inspector): declare generated build inputs
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { parse } from "acorn";
|
|
import { analyze } from "eslint-scope";
|
|
|
|
export interface SourceRange {
|
|
start: number;
|
|
end: number;
|
|
}
|
|
|
|
export function sourceDependencies(text: string): (range: SourceRange) => SourceRange[] {
|
|
try {
|
|
const ast = parse(text, {
|
|
ecmaVersion: "latest",
|
|
sourceType: "module",
|
|
allowReturnOutsideFunction: true,
|
|
ranges: true,
|
|
});
|
|
const scopes = analyze(ast as Parameters<typeof analyze>[0], {
|
|
ecmaVersion: 2022,
|
|
sourceType: "module",
|
|
optimistic: false,
|
|
});
|
|
const references = scopes.scopes.flatMap((scope) =>
|
|
scope.references.flatMap((reference) => {
|
|
const location = reference.identifier.range;
|
|
if (!reference.isRead() || !location) return [];
|
|
const definitions = (reference.resolved?.defs ?? []).flatMap((definition) => {
|
|
const node =
|
|
definition.type === "Variable"
|
|
? definition.parent
|
|
: definition.type === "FunctionName"
|
|
? definition.node
|
|
: null;
|
|
return node?.range ? [{ start: node.range[0], end: node.range[1] }] : [];
|
|
});
|
|
return definitions.length ? [{ start: location[0], end: location[1], definitions }] : [];
|
|
}),
|
|
);
|
|
return (range) => {
|
|
const pending = [range];
|
|
const found = new Map<string, SourceRange>();
|
|
for (let index = 0; index < pending.length; index++) {
|
|
const current = pending[index];
|
|
for (const reference of references) {
|
|
if (reference.start < current.start || reference.end > current.end) continue;
|
|
for (const { start, end } of reference.definitions) {
|
|
if (start >= range.start && end <= range.end) continue;
|
|
const key = `${start}:${end}`;
|
|
if (found.has(key)) continue;
|
|
const dependency = { start, end };
|
|
found.set(key, dependency);
|
|
pending.push(dependency);
|
|
}
|
|
}
|
|
}
|
|
return [...found.values()].sort((a, b) => a.start - b.start);
|
|
};
|
|
} catch {
|
|
return () => [];
|
|
}
|
|
}
|