1
0
Fork 0

replace untyped error data with ref to rule and elem

This commit is contained in:
Sean Sube 2022-02-14 14:10:48 +00:00
parent eb8c665bf2
commit f69fd0a730
5 changed files with 16 additions and 30 deletions

View File

@ -1,9 +1,6 @@
import { Document, Source } from '../source';
export interface Parser {
/**
* @TODO should dump deal with Sources as well? does it care about paths?
*/
dump(...data: Array<Document>): string;
parse(data: Source): Array<Document>;
}

View File

@ -30,17 +30,12 @@ export class RuleVisitor extends EventEmitter implements RuleVisitorOptions, Vis
}
public async visit(ctx: VisitorContext, rule: Rule, elem: Element): Promise<RuleResult> {
const results = {
changes: [],
errors: [],
};
const refData = cloneDeep(elem.data);
const ruleResult = await rule.visit(ctx, elem);
const results = await rule.visit(ctx, elem);
if (hasItems(ruleResult.errors)) {
ctx.logger.warn({ count: ruleResult.errors.length, rule }, 'rule failed');
ctx.mergeResult(ruleResult, elem);
if (hasItems(results.errors)) {
ctx.logger.warn({ count: results.errors.length, rule }, 'rule failed');
ctx.mergeResult(rule, elem, results);
return results;
}

View File

@ -1,7 +1,8 @@
import Ajv, { ValidateFunction } from 'ajv';
import { Logger } from 'noicejs';
import { RuleChange, RuleError, RuleResult } from '../rule/index.js';
import { Rule, RuleChange, RuleError, RuleResult } from '../rule/index.js';
import { Element } from '../source.js';
export interface RuleOptions {
coerce: boolean;
@ -60,19 +61,9 @@ export class VisitorContext implements VisitorContextOptions, RuleResult {
return this.ajv.compile(schema);
}
/**
* @TODO what is data? should use the element
*/
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
public mergeResult(other: RuleResult, data: any = {}): this {
this.changeBuffer.push(...other.changes);
this.errorBuffer.push(...other.errors.map((err) => ({
...err,
data: {
...err.data,
...data,
},
})));
public mergeResult(rule: Rule, elem: Element, result: RuleResult): this {
this.changeBuffer.push(...result.changes);
this.errorBuffer.push(...result.errors);
return this;
}
}

View File

@ -13,14 +13,17 @@ export interface Context {
export interface Visitor extends EventEmitter {
/**
* Select nodes eligible to be visited.
* Select elements eligible to be visited.
*/
pick(ctx: Context, rule: Rule, doc: Document): Promise<Array<Element>>;
/**
* Visit a node.
* Visit an element.
*/
visit(ctx: Context, rule: Rule, elem: Element): Promise<RuleResult>;
/**
* Visit all selected elements within a document.
*/
visitAll(ctx: Context, rule: Rule, doc: Document): Promise<Array<RuleResult>>;
}

View File

@ -2,7 +2,7 @@ import { expect } from 'chai';
import { LogLevel, NullLogger } from 'noicejs';
import { VisitorContext } from '../../src/visitor/VisitorContext.js';
import { createErrorContext } from '../helpers.js';
import { createErrorContext, makeElement } from '../helpers.js';
describe('visitor context', () => {
it('should merge results', async () => {
@ -17,7 +17,7 @@ describe('visitor context', () => {
},
});
const nextCtx = firstCtx.mergeResult({
const nextCtx = firstCtx.mergeResult(rule, makeElement({}), {
changes: [{
data: {
data: {},