Skip to content

Commit

Permalink
fix(rule): log and apply mutation when rule has > 0 diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Nov 1, 2019
1 parent 3b2e421 commit 63b6e48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
27 changes: 13 additions & 14 deletions src/rule.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -198,21 +198,20 @@ export async function visitRules(ctx: VisitorContext, rules: Array<Rule>, 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;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
@@ -1,6 +1,6 @@
import { isNil } from 'lodash';

export function isNilOrEmpty(val: Array<unknown> | null | undefined): val is Array<unknown> {
export function hasItems(val: Array<unknown> | null | undefined): val is Array<unknown> {
return (Array.isArray(val) && val.length > 0);
}

Expand Down

0 comments on commit 63b6e48

Please sign in to comment.