From 6c33b8161055c0f6caee0a892f65a2c4a507453e Mon Sep 17 00:00:00 2001 From: ssube Date: Fri, 15 Nov 2019 18:56:08 -0600 Subject: [PATCH] lint: extract visit item method --- src/rule/RuleVisitor.ts | 52 ++++++++++++++++++++++------------------- test/TestApp.ts | 2 ++ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/rule/RuleVisitor.ts b/src/rule/RuleVisitor.ts index d5781b5..b9c292d 100644 --- a/src/rule/RuleVisitor.ts +++ b/src/rule/RuleVisitor.ts @@ -20,7 +20,7 @@ export class RuleVisitor implements RuleVisitorOptions, Visitor { } public async pick(ctx: VisitorContext, root: any): Promise> { - 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 { @@ -32,34 +32,38 @@ export class RuleVisitor implements RuleVisitorOptions, Visitor { itemIndex, 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; } } return ctx; } + + public async visitItem(ctx: VisitorContext, item: any, itemIndex: number, rule: Rule): Promise { + 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'); + } + } } diff --git a/test/TestApp.ts b/test/TestApp.ts index 118fffb..6ae8293 100644 --- a/test/TestApp.ts +++ b/test/TestApp.ts @@ -81,4 +81,6 @@ describeLeaks('main app', async () => { expect(status).to.equal(STATUS_ERROR); }); + + it('should fix up partial documents'); // TODO: test fix mode });