lint: flatten flow control in rule diff func
This commit is contained in:
parent
67a61f6d8a
commit
71ef38b82e
33
src/rule.ts
33
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<Rule>, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
export function isNilOrEmpty(val: Array<unknown> | null | undefined): val is Array<unknown> {
|
||||
return (Array.isArray(val) && val.length > 0);
|
||||
}
|
Loading…
Reference in New Issue