histoire
Version:
A fast story book powered by Vite
81 lines (80 loc) • 3.5 kB
JavaScript
import { locateConfigObject, resolveLocalNode } from './locate.js';
import { unwrapNode } from './parse.js';
import { parseConfigPath } from './paths.js';
import { equalValue } from './update.js';
import { locateConfigPath, staticValue } from './walk.js';
/** Refuses mutating a local initializer when another config value still owns it. */
export function assertSharedInitializerOwnership(before, after, patches) {
const config = locateConfigObject(before);
if (!config.object || config.issue)
throw new Error(config.issue?.reason ?? 'Config must be an object literal');
const references = collectLocalReferences(before, config.object);
const selected = selectedReferences(before, config.object, references, patches);
for (const name of new Set(references.map(reference => reference.name))) {
if (!bindingChanged(before, after, name))
continue;
if (references.some(reference => reference.name === name && !selected.has(referenceKey(reference)))) {
throw new Error('Cannot edit a shared initializer with retained config references');
}
}
}
/** Walks config values only, excluding object keys and unrelated executable syntax. */
function collectLocalReferences(parsed, input) {
const references = [];
function visit(value) {
const node = unwrapNode(value);
if (node.type === 'Identifier') {
const binding = parsed.bindings.get(node.name);
if (binding?.kind === 'const' && binding.node && !resolveLocalNode(parsed, node).issue) {
references.push({ name: node.name, start: node.start, end: node.end });
}
return;
}
if (node.type === 'ObjectExpression') {
for (const property of node.properties) {
if (property.type === 'ObjectProperty')
visit(property.value);
}
}
else if (node.type === 'ArrayExpression') {
for (const item of node.elements) {
if (item)
visit(item);
}
}
}
visit(input);
return references;
}
/** Finds references explicitly covered by at least one source patch. */
function selectedReferences(parsed, object, references, patches) {
const selected = new Set();
for (const patch of patches) {
const located = locateConfigPath(parsed, object, parseConfigPath(patch.path));
if (!located.node)
continue;
for (const reference of references) {
if (reference.start >= located.node.start && reference.end <= located.node.end)
selected.add(referenceKey(reference));
}
}
return selected;
}
/** Detects a changed local literal initializer without executing config code. */
function bindingChanged(before, after, name) {
const original = bindingValue(before, name);
const next = bindingValue(after, name);
return original !== undefined && (next === undefined || !equalValue(original, next));
}
/** Reads a literal local initializer's static JSON value. */
function bindingValue(parsed, name) {
const binding = parsed.bindings.get(name);
if (binding?.kind !== 'const' || !binding.node)
return undefined;
const result = staticValue(parsed, binding.node);
return result.issue ? undefined : result.value;
}
/** Builds a stable set key for a source range. */
function referenceKey(reference) {
return `${reference.start}:${reference.end}`;
}