Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added custom message possibility #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions packages/core/src/lib/checks/is-dependency-allowed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test, it } from 'vitest';
import { describe, expect, it, test } from 'vitest';
import {
DependencyCheckContext,
DependencyRulesConfig,
Expand All @@ -25,23 +25,26 @@ describe('check dependency rules', () => {

expect(
isDependencyAllowed(['type:feature'], 'type:ui', config, dummyContext)
).toBe(true);
).toEqual({ allowed: true });
expect(
isDependencyAllowed(['type:ui'], 'type:feature', config, dummyContext)
).toBe(false);
).toEqual({ allowed: false });
});

test('multiple string rules', () => {
const config: DependencyRulesConfig = {
'type:feature': ['type:data', 'type:ui'],
'type:feature': {
matcher: ['type:data', 'type:ui'],
message: (to) => `not allowed for ${to}`,
},
};

expect(
isDependencyAllowed(['type:feature'], 'type:ui', config, dummyContext)
).toBe(true);
).toEqual({ allowed: true });
expect(
isDependencyAllowed(['type:feature'], 'domain:abc', config, dummyContext)
).toBe(false);
).toEqual({ allowed: false, customMessage: 'not allowed for domain:abc' });
});

for (const [to, isAllowed] of [
Expand All @@ -57,7 +60,7 @@ describe('check dependency rules', () => {

expect(
isDependencyAllowed(['type:feature'], to, config, dummyContext)
).toBe(isAllowed);
).toEqual({ allowed: isAllowed });
});
}

Expand All @@ -72,7 +75,7 @@ describe('check dependency rules', () => {

expect(
isDependencyAllowed(['type:feature'], to, config, dummyContext)
).toBe(isAllowed);
).toEqual({ allowed: isAllowed });
});
}

Expand Down Expand Up @@ -118,7 +121,7 @@ describe('check dependency rules', () => {
};
expect(
isDependencyAllowed(['domain:customers'], to, config, dummyContext)
).toBe(isAllowed);
).toEqual({ allowed: isAllowed });
});
}

Expand All @@ -137,7 +140,7 @@ describe('check dependency rules', () => {
};
expect(
isDependencyAllowed(['domain:customers'], to, config, dummyContext)
).toBe(isAllowed);
).toEqual({ allowed: isAllowed });
});
}

Expand All @@ -154,7 +157,7 @@ describe('check dependency rules', () => {
config,
dummyContext
)
).toBe(true);
).toEqual({ allowed: true });
});

it('should have access to a module when of the tags allow it', () => {
Expand All @@ -170,7 +173,7 @@ describe('check dependency rules', () => {
config,
dummyContext
)
).toBe(true);
).toEqual({ allowed: true });
});

it('should allow wildcard in rule values as well', () => {
Expand All @@ -180,7 +183,7 @@ describe('check dependency rules', () => {

expect(
isDependencyAllowed(['type:feature'], 'shared:ui', config, dummyContext)
).toBe(true);
).toEqual({ allowed: true });
});

for (const [to, from, isAllowed] of [
Expand All @@ -194,9 +197,9 @@ describe('check dependency rules', () => {
'domain:*': sameTag,
};

expect(isDependencyAllowed([from], to, config, dummyContext)).toBe(
isAllowed
);
expect(isDependencyAllowed([from], to, config, dummyContext)).toEqual({
allowed: isAllowed,
});
});
}

Expand All @@ -209,7 +212,7 @@ describe('check dependency rules', () => {

expect(
isDependencyAllowed(['type:model'], toTag, config, dummyContext)
).toBe(false);
).toEqual({ allowed: false });
}
);
});
98 changes: 73 additions & 25 deletions packages/core/src/lib/checks/is-dependency-allowed.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,91 @@
import {
DependencyCheckContext,
DependencyRulesConfig,
Rule,
RuleMatcher,
RuleMatcherFn,
RuleWithCustomMessage,
} from '../config/dependency-rules-config';
import { wildcardToRegex } from '../util/wildcard-to-regex';
import toArray from '../util/to-array';

interface AllowedDependencyResponse {
allowed: boolean;
customMessage?: string;
}

export const isDependencyAllowed = (
froms: string[],
to: string,
config: DependencyRulesConfig,
context: DependencyCheckContext
): boolean => {
let isAllowed: boolean | undefined;
): AllowedDependencyResponse => {
let result: AllowedDependencyResponse | undefined = undefined;
for (const from of froms) {
isAllowed = undefined;
for (const tag in config) {
if (from.match(wildcardToRegex(tag))) {
const value = config[tag];
const matchers = Array.isArray(value) ? value : [value];
for (const matcher of matchers) {
if (
typeof matcher === 'string' &&
to.match(wildcardToRegex(matcher))
) {
return true;
} else if (
typeof matcher === 'function' &&
matcher({ from, to, ...context })
) {
return true;
}
}
isAllowed = false;
}
}
const resultForFrom = findDependencyRuleForFrom(from, to, context, config);

if (isAllowed === undefined) {
if (resultForFrom === undefined) {
throw new Error(`cannot find any dependency rule for tag ${from}`);
}
result = resultForFrom;
if (resultForFrom?.allowed) {
return resultForFrom;
}
}

return result as AllowedDependencyResponse;
};

const findDependencyRuleForFrom = (
from: string,
to: string,
context: DependencyCheckContext,
config: DependencyRulesConfig
): AllowedDependencyResponse | undefined => {
let isAllowed: AllowedDependencyResponse | undefined;

for (const [tag, rule] of Object.entries(config)) {
if (from.match(wildcardToRegex(tag))) {
const ruleWithCustomMessage = toRuleWithCustomMessage(rule);
const matchers = toArray(ruleWithCustomMessage.matcher);

const foundMatcher = matchers.find(testMatch(from, to, context));
if (foundMatcher) {
return { allowed: true };
}
// the current matcher does not allow it.
// But we may have other matchers which might fit. So dont return immediately
isAllowed = {
allowed: false,
customMessage: ruleWithCustomMessage.message(to),
};
}
}
return isAllowed;
};

return false;
const testMatch =
(from: string, to: string, context: DependencyCheckContext) =>
(matcher: RuleMatcher) =>
(isRuleMatcher(matcher) && to.match(wildcardToRegex(matcher))) ||
(isRuleMatcherFn(matcher) && matcher({ from, to, ...context }));

const isRuleMatcherFn = (matcher: Rule): matcher is RuleMatcherFn =>
typeof matcher === 'function';
const isRuleMatcher = (matcher: Rule): matcher is string =>
typeof matcher === 'string';

const isRuleWithCustomMessage = (
matcher: Rule
): matcher is RuleWithCustomMessage =>
!!(matcher as RuleWithCustomMessage).matcher;

const toRuleWithCustomMessage = (matcher: Rule): RuleWithCustomMessage => {
if (isRuleWithCustomMessage(matcher)) {
return matcher;
}
return {
matcher,
message: () => undefined,
};
};
9 changes: 8 additions & 1 deletion packages/core/src/lib/config/dependency-rules-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ export type RuleMatcherFn = (
to: string;
} & DependencyCheckContext
) => boolean;
type CustomMessageFn = (to: string) => string | undefined;

export interface RuleWithCustomMessage {
message: CustomMessageFn;
matcher: RuleMatcher | RuleMatcher[];
}
export type Rule = RuleWithCustomMessage | RuleMatcher[] | RuleMatcher;
export type RuleMatcher = string | null | RuleMatcherFn;
export type DependencyRulesConfig = Record<string, RuleMatcher | RuleMatcher[]>;
export type DependencyRulesConfig = Record<string, Rule>;
Loading