1
0
Fork 0

lint: extract visit item method

This commit is contained in:
ssube 2019-11-15 18:56:08 -06:00 committed by Sean Sube
parent c053da9051
commit 6c33b81610
2 changed files with 30 additions and 24 deletions

View File

@ -20,7 +20,7 @@ export class RuleVisitor implements RuleVisitorOptions, Visitor {
} }
public async pick(ctx: VisitorContext, root: any): Promise<Array<any>> { public async pick(ctx: VisitorContext, root: any): Promise<Array<any>> {
return []; // why is this part of visitor rather than rule? return []; // TODO: why is this part of visitor rather than rule?
} }
public async visit(ctx: VisitorContext, root: any): Promise<VisitorContext> { public async visit(ctx: VisitorContext, root: any): Promise<VisitorContext> {
@ -32,34 +32,38 @@ export class RuleVisitor implements RuleVisitorOptions, Visitor {
itemIndex, itemIndex,
rule, rule,
}; };
const itemResult = cloneDeep(item);
const ruleResult = await rule.visit(ctx, itemResult);
if (hasItems(ruleResult.errors)) {
ctx.logger.warn({ count: ruleResult.errors.length, rule }, 'rule failed');
ctx.mergeResult(ruleResult, ctx.visitData);
continue;
}
const itemDiff = diff(item, itemResult);
if (hasItems(itemDiff)) {
ctx.logger.info({
diff: itemDiff,
item,
rule: rule.name,
}, 'rule passed with modifications');
if (ctx.schemaOptions.mutate) {
applyDiff(item, itemResult);
}
} else {
ctx.logger.info({ rule: rule.name }, 'rule passed');
}
await this.visitItem(ctx, item, itemIndex, rule);
itemIndex += 1; itemIndex += 1;
} }
} }
return ctx; return ctx;
} }
public async visitItem(ctx: VisitorContext, item: any, itemIndex: number, rule: Rule): Promise<void> {
const itemResult = cloneDeep(item);
const ruleResult = await rule.visit(ctx, itemResult);
if (hasItems(ruleResult.errors)) {
ctx.logger.warn({ count: ruleResult.errors.length, rule }, 'rule failed');
ctx.mergeResult(ruleResult, ctx.visitData);
return;
}
const itemDiff = diff(item, itemResult);
if (hasItems(itemDiff)) {
ctx.logger.info({
diff: itemDiff,
item,
rule: rule.name,
}, 'rule passed with modifications');
if (ctx.schemaOptions.mutate) {
applyDiff(item, itemResult);
}
} else {
ctx.logger.info({ rule: rule.name }, 'rule passed');
}
}
} }

View File

@ -81,4 +81,6 @@ describeLeaks('main app', async () => {
expect(status).to.equal(STATUS_ERROR); expect(status).to.equal(STATUS_ERROR);
}); });
it('should fix up partial documents'); // TODO: test fix mode
}); });