Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 14x 14x 39x 14x 14x 14x 39x 20x 15x 14x 14x 14x 67x 2x 2x 5x 2x 2x 2x 65x 39x 39x 19x 19x 20x 20x 28x 20x 20x 20x 39x 14x 26x 26x 25x 25x 25x 25x 25x 25x 3x 3x 3x 25x 25x 25x 15x 15x 15x 23x 23x 25x 25x 25x 15x 15x 15x 17x 16x 8x 6x 6x 8x 4x 4x 2x 2x 1x 15x 10x 3x 1x 18x 17x 17x 16x 16x 15x 15x 15x 15x 15x | /**
* Validates multi-base inheritance: detects cycles, enforces that conflicting fields across
* bases are explicitly redeclared with the `override` modifier, and rejects spurious `override`
* modifiers on fields not present in any base.
*
* Resolution rules:
* - Each base contributes its fully-resolved field set (recursive). Diamond inheritance is
* deduplicated — a model reachable via multiple paths only contributes its fields once.
* - Two fields with the same name conflict iff their `FieldNode` shapes differ on any of:
* type, optional, nullable, visibility, default, deprecated. `description` and `loc` are ignored.
* - When two or more bases contribute a same-named field, the model must redeclare that field
* in its own inline block with `override`.
* - A field with `override` must shadow at least one base-contributed field.
*
* Runs after `validate-refs` so we know all referenced bases resolve. Cycles short-circuit
* the recursion so a downstream conflict check on a cyclic chain emits one cycle error per cycle,
* not one per node.
*/
import type { ContractRootNode, ContractTypeNode, FieldNode, ModelNode } from './ast.js';
import type { DiagnosticCollector } from './diagnostics.js';
export function validateInheritance(contractRoots: ContractRootNode[], diag: DiagnosticCollector): void {
const modelMap = new Map<string, ModelNode>();
for (const root of contractRoots) {
for (const model of root.models) modelMap.set(model.name, model);
}
const cycleNodes = detectCycles(modelMap, diag);
for (const root of contractRoots) {
for (const model of root.models) {
if (!model.bases || model.bases.length === 0) continue;
if (cycleNodes.has(model.name)) continue;
checkModel(model, modelMap, cycleNodes, diag);
}
}
}
function detectCycles(modelMap: Map<string, ModelNode>, diag: DiagnosticCollector): Set<string> {
const onStack = new Set<string>();
const visited = new Set<string>();
const inCycle = new Set<string>();
function visit(name: string, path: string[]): void {
if (onStack.has(name)) {
const start = path.indexOf(name);
const cycle = path.slice(start).concat(name).join(' → ');
for (const n of path.slice(start)) inCycle.add(n);
const model = modelMap.get(name);
Eif (model) diag.error(model.loc.file, model.loc.line, `Inheritance cycle: ${cycle}`);
return;
}
if (visited.has(name)) return;
const model = modelMap.get(name);
if (!model || !model.bases) {
visited.add(name);
return;
}
onStack.add(name);
path.push(name);
for (const b of model.bases) visit(b, path);
path.pop();
onStack.delete(name);
visited.add(name);
}
for (const name of modelMap.keys()) visit(name, []);
return inCycle;
}
/** Resolve a base model to its **effective** field set: the merge of all its inherited
* fields with its own (own fields shadow inherited by name). Memoized per model. */
function resolveEffectiveFields(
baseName: string,
modelMap: Map<string, ModelNode>,
cycleNodes: Set<string>,
cache: Map<string, Map<string, FieldNode>>,
): Map<string, FieldNode> {
const cached = cache.get(baseName);
if (cached) return cached;
Iif (cycleNodes.has(baseName)) {
const empty = new Map<string, FieldNode>();
cache.set(baseName, empty);
return empty;
}
const base = modelMap.get(baseName);
Iif (!base) {
const empty = new Map<string, FieldNode>();
cache.set(baseName, empty);
return empty;
}
// Set early to break any unexpected recursion.
const effective = new Map<string, FieldNode>();
cache.set(baseName, effective);
if (base.bases) {
for (const grandparent of base.bases) {
const g = resolveEffectiveFields(grandparent, modelMap, cycleNodes, cache);
for (const [name, field] of g) effective.set(name, field);
}
}
for (const own of base.fields) {
effective.set(own.name, own);
}
return effective;
}
function checkModel(model: ModelNode, modelMap: Map<string, ModelNode>, cycleNodes: Set<string>, diag: DiagnosticCollector): void {
const cache = new Map<string, Map<string, FieldNode>>();
// For each direct base, resolve its effective field set. Cross-base conflicts are detected
// by comparing same-named contributions across bases (each base's own overrides already
// applied within its set).
const baseFieldsByName = new Map<string, { source: string; field: FieldNode }[]>();
for (const base of model.bases!) {
const effective = resolveEffectiveFields(base, modelMap, cycleNodes, cache);
for (const [name, field] of effective) {
const list = baseFieldsByName.get(name) ?? [];
list.push({ source: base, field });
baseFieldsByName.set(name, list);
}
}
const localFieldsByName = new Map<string, FieldNode>();
for (const f of model.fields) localFieldsByName.set(f.name, f);
// Rule: every cross-base conflict must be redeclared with override.
for (const [name, list] of baseFieldsByName) {
if (list.length < 2) continue;
const allIdentical = list.every(item => fieldsAreIdentical(item.field, list[0]!.field));
if (allIdentical) continue;
const local = localFieldsByName.get(name);
if (!local) {
const sources = list.map(l => `'${l.source}'`).join(' and ');
diag.error(
model.loc.file,
model.loc.line,
`Field '${name}' is declared by ${sources} with different shapes — redeclare in '${model.name}' with 'override'`,
);
continue;
}
if (!local.override) {
const sources = list.map(l => `'${l.source}'`).join(' and ');
diag.error(
local.loc.file,
local.loc.line,
`Field '${name}' conflicts across bases ${sources} — mark as 'override'`,
);
}
}
// Rule: `override` must shadow at least one base-contributed field.
for (const f of model.fields) {
if (!f.override) continue;
if (!baseFieldsByName.has(f.name)) {
diag.error(f.loc.file, f.loc.line, `Field '${f.name}' has 'override' but is not declared in any base of '${model.name}'`);
}
}
}
/** Structural deep-equality on a FieldNode for inheritance-conflict purposes.
* Compares: type (deep), optional, nullable, visibility, default, deprecated.
* Ignores: description, loc, override (the marker itself isn't part of "shape"). */
export function fieldsAreIdentical(a: FieldNode, b: FieldNode): boolean {
if (a.optional !== b.optional) return false;
Iif (a.nullable !== b.nullable) return false;
if (a.visibility !== b.visibility) return false;
Iif ((a.deprecated ?? false) !== (b.deprecated ?? false)) return false;
if (a.default !== b.default) return false;
return typesAreIdentical(a.type, b.type);
}
function typesAreIdentical(a: ContractTypeNode, b: ContractTypeNode): boolean {
Iif (a.kind !== b.kind) return false;
switch (a.kind) {
case 'scalar': {
const bb = b as typeof a;
return a.name === bb.name && a.min === bb.min && a.max === bb.max && a.len === bb.len && a.regex === bb.regex && a.format === bb.format;
}
case 'literal': {
const bb = b as typeof a;
return a.value === bb.value;
}
case 'enum': {
const bb = b as typeof a;
return a.values.length === bb.values.length && a.values.every((v, i) => v === bb.values[i]);
}
case 'ref': {
const bb = b as typeof a;
return a.name === bb.name;
}
case 'array': {
const bb = b as typeof a;
if (a.min !== bb.min || a.max !== bb.max) return false;
return typesAreIdentical(a.item, bb.item);
}
case 'tuple': {
const bb = b as typeof a;
return a.items.length === bb.items.length && a.items.every((it, i) => typesAreIdentical(it, bb.items[i]!));
}
case 'record': {
const bb = b as typeof a;
return typesAreIdentical(a.key, bb.key) && typesAreIdentical(a.value, bb.value);
}
case 'union':
case 'intersection': {
const bb = b as typeof a;
return a.members.length === bb.members.length && a.members.every((m, i) => typesAreIdentical(m, bb.members[i]!));
}
case 'discriminatedUnion': {
const bb = b as typeof a;
if (a.discriminator !== bb.discriminator) return false;
return a.members.length === bb.members.length && a.members.every((m, i) => typesAreIdentical(m, bb.members[i]!));
}
case 'lazy': {
const bb = b as typeof a;
return typesAreIdentical(a.inner, bb.inner);
}
case 'inlineObject': {
const bb = b as typeof a;
if (a.fields.length !== bb.fields.length) return false;
for (let i = 0; i < a.fields.length; i++) {
const af = a.fields[i]!;
const bf = bb.fields[i]!;
if (af.name !== bf.name) return false;
if (!fieldsAreIdentical(af, bf)) return false;
}
return true;
}
}
}
|