From 63b6e48cc740de889dea53c06c0ef15387a64cf3 Mon Sep 17 00:00:00 2001 From: ssube Date: Fri, 1 Nov 2019 05:46:31 -0500 Subject: [PATCH] fix(rule): log and apply mutation when rule has > 0 diffs --- src/rule.ts | 27 +++++++++++++-------------- src/utils/index.ts | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/rule.ts b/src/rule.ts index 7f09ac0..9a32787 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -6,7 +6,7 @@ import { LogLevel } from 'noicejs'; import { YamlParser } from './parser/YamlParser'; import { readFileSync } from './source'; -import { ensureArray, isNilOrEmpty } from './utils'; +import { ensureArray, hasItems } from './utils'; import { friendlyError } from './utils/ajv'; import { Visitor } from './visitor'; import { VisitorContext } from './visitor/VisitorContext'; @@ -198,21 +198,20 @@ export async function visitRules(ctx: VisitorContext, rules: Array, data: } const itemDiff = diff(item, itemCopy); - if (isNilOrEmpty(itemDiff)) { + if (hasItems(itemDiff)) { + ctx.logger.info({ + diff: itemDiff, + item, + rule: rule.name, + }, 'rule passed with modifications'); + + if (ctx.innerOptions.mutate) { + applyDiff(item, itemCopy); + } + } else { 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); - } - } + } } return ctx; diff --git a/src/utils/index.ts b/src/utils/index.ts index 1364aed..ac3804a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,6 @@ import { isNil } from 'lodash'; -export function isNilOrEmpty(val: Array | null | undefined): val is Array { +export function hasItems(val: Array | null | undefined): val is Array { return (Array.isArray(val) && val.length > 0); }