From 71ef38b82ec692caa93eb5b9527470a270c0427f Mon Sep 17 00:00:00 2001 From: ssube Date: Wed, 11 Sep 2019 06:57:45 -0500 Subject: [PATCH] lint: flatten flow control in rule diff func --- src/rule.ts | 33 ++++++++++++++++++--------------- src/utils/index.ts | 3 +++ 2 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 src/utils/index.ts diff --git a/src/rule.ts b/src/rule.ts index 9ea44bc..80814c7 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -6,6 +6,7 @@ import { LogLevel } from 'noicejs'; import { YamlParser } from './parser/YamlParser'; import { readFileSync } from './source'; +import { isNilOrEmpty } from './utils'; import { friendlyError } from './utils/ajv'; import { Visitor } from './visitor'; import { VisitorContext } from './visitor/VisitorContext'; @@ -122,24 +123,26 @@ export async function visitRules(ctx: VisitorContext, rules: Array, data: const itemCopy = cloneDeep(item); const itemResult = await rule.visit(ctx, itemCopy); - if (itemResult.errors.length > 0) { + if (itemResult.errors.length === 0) { ctx.logger.warn({ count: itemResult.errors.length, rule }, 'rule failed'); ctx.mergeResult(itemResult); - } else { - const itemDiff = diff(item, itemCopy); - if (Array.isArray(itemDiff) && itemDiff.length > 0) { - ctx.logger.info({ - diff: itemDiff, - item, - rule: rule.name, - }, 'rule passed with modifications'); + continue; + } - if (ctx.innerOptions.mutate) { - applyDiff(item, itemCopy); - } - } else { - ctx.logger.info({ rule: rule.name }, 'rule passed'); - } + const itemDiff = diff(item, itemCopy); + if (isNilOrEmpty(itemDiff)) { + ctx.logger.info({ rule: rule.name }, 'rule passed'); + continue; + } + + ctx.logger.info({ + diff: itemDiff, + item, + rule: rule.name, + }, 'rule passed with modifications'); + + if (ctx.innerOptions.mutate) { + applyDiff(item, itemCopy); } } } diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..d2795a4 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,3 @@ +export function isNilOrEmpty(val: Array | null | undefined): val is Array { + return (Array.isArray(val) && val.length > 0); +}