UNPKG

histoire

Version:

A fast story book powered by Vite

127 lines (126 loc) • 6.29 kB
import { resolveLocalNode } from './locate.js'; import { nodeIssue, unwrapNode } from './parse.js'; /** Reads ordinary property names without evaluating computed keys. */ export function propertyName(property) { if (property.type === 'SpreadElement' || property.computed) return undefined; if (property.key.type === 'Identifier') return property.key.name; if (property.key.type === 'StringLiteral' || property.key.type === 'NumericLiteral') return String(property.key.value); return undefined; } /** Finds a stable property, rejecting duplicates and later unknown overrides. */ export function findProperty(object, key) { const matches = object.properties.filter(item => propertyName(item) === key); if (matches.length > 1) return { issue: nodeIssue(matches[1], `"${key}" has duplicate properties`) }; const found = matches[0]; const foundIndex = found ? object.properties.indexOf(found) : -1; const unknown = object.properties.find((item, index) => index > foundIndex && propertyName(item) === undefined); if (unknown) return { issue: nodeIssue(unknown, unknown.type === 'SpreadElement' ? `"${key}" may come from a spread` : `"${key}" may come from a computed property`) }; if (found?.type === 'ObjectMethod') return { issue: nodeIssue(found, `"${key}" is a function value`, 'function-only') }; return { property: found }; } /** Decodes literals recursively; functions, spreads, imports and calls never run. */ export function staticValue(parsed, input, followed = false) { const original = unwrapNode(input); const resolved = resolveLocalNode(parsed, original, followed); if (resolved.issue) return resolved; const node = resolved.node; const nextFollowed = followed || original.type === 'Identifier'; if (node.type === 'NullLiteral') return { value: null }; if (node.type === 'StringLiteral' || node.type === 'NumericLiteral' || node.type === 'BooleanLiteral') return { value: node.value }; if (node.type === 'UnaryExpression' && ['-', '+'].includes(node.operator) && node.argument.type === 'NumericLiteral') { return { value: node.operator === '-' ? -node.argument.value : node.argument.value }; } if (node.type === 'ArrayExpression') { const values = []; for (const item of node.elements) { if (!item) return { issue: nodeIssue(node, 'Array contains an empty element') }; const decoded = staticValue(parsed, item, nextFollowed); if (decoded.issue) return decoded; values.push(decoded.value); } return { value: values }; } if (node.type === 'ObjectExpression') { const values = Object.create(null); for (const property of node.properties) { const key = propertyName(property); if (key === undefined) return { issue: nodeIssue(property, property.type === 'SpreadElement' ? 'Value contains a spread' : 'Value contains a computed property') }; if (property.type !== 'ObjectProperty') return { issue: nodeIssue(property, `"${key}" is a function value`, 'function-only') }; if (Object.hasOwn(values, key)) return { issue: nodeIssue(property, `"${key}" has duplicate properties`) }; const decoded = staticValue(parsed, property.value, nextFollowed); if (decoded.issue) return decoded; values[key] = decoded.value; } return { value: values }; } const constructs = { CallExpression: 'a call result', ConditionalExpression: 'a conditional expression', TemplateLiteral: 'a template literal', SpreadElement: 'a spread', ArrowFunctionExpression: 'a function value', FunctionExpression: 'a function value', ObjectMethod: 'a function value', }; const isFunction = ['ArrowFunctionExpression', 'FunctionExpression', 'ObjectMethod'].includes(node.type); return { issue: nodeIssue(node, `Value is ${constructs[node.type] ?? `a computed ${node.type}`}`, isFunction ? 'function-only' : 'computed') }; } /** Traverses only literal objects and arrays selected by a stable preset id. */ export function locateConfigPath(parsed, object, tokens) { let current = object; let result = { node: object }; for (let index = 0; index < tokens.length; index++) { const token = tokens[index]; const resolved = resolveLocalNode(parsed, current); if (resolved.issue) return resolved; const node = resolved.node; if (typeof token === 'string') { if (node.type !== 'ObjectExpression') return { issue: nodeIssue(node, 'Path crosses a value that is not an object literal') }; const found = findProperty(node, token); if (found.issue) return found; if (!found.property) return { parent: node, missing: tokens.slice(index) }; current = found.property.value; result = { node: current, parent: node, property: found.property }; } else { if (node.type !== 'ArrayExpression') return { issue: nodeIssue(node, 'Agent presets must be an array literal') }; const matching = []; for (const item of node.elements) { if (!item) return { issue: nodeIssue(node, 'Agent presets contain an empty element') }; const decoded = staticValue(parsed, item); if (decoded.issue) return decoded; if (decoded.value && !Array.isArray(decoded.value) && typeof decoded.value === 'object' && decoded.value.id === token.id) matching.push(item); } if (matching.length > 1) return { issue: nodeIssue(matching[1], `Agent preset "${token.id}" has duplicate ids`) }; if (!matching.length) return { parent: node, missing: tokens.slice(index) }; current = matching[0]; result = { node: current, parent: node }; } } return result; }