class ErrorTree {
constructor(parent, context) {
this.parent = parent;
this.context = context;
this.children = {};
this.error = null;
}
getParent() {
return this.parent;
}
getChildren() {
return Object.values(this.children);
}
name() {
return this.error ? getMessage(this.error) : this.context.type.name;
}
toJS() {
const children = this.getChildren().map(child => child.toJS());
return {
name: this.name(),
children
};
}
addError(error) {
const ctx = [...error.context];
this.context = ctx.shift();
this.addError_internal(ctx, error);
}
addError_internal(ctx, error) {
const currContext = ctx.shift();
if (currContext) {
const key = currContext.key;
if (!this.children[key]) {
this.children[key] = new ErrorTree(this, currContext);
}
this.children[key].addError_internal(ctx, error);
} else {
this.error = error;
}
}
}