Skip to content

Commit

Permalink
chore(linter): minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 4, 2022
1 parent 3b8802b commit c08b25b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
13 changes: 3 additions & 10 deletions packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts
Expand Up @@ -17,6 +17,7 @@ import {
hasBannedImport,
isDirectDependency,
isTerminalRun,
stringifyTags,
} from '@nrwl/workspace/src/utils/runtime-lint-utils';
import {
AST_NODE_TYPES,
Expand Down Expand Up @@ -403,42 +404,34 @@ export default createESLintRule<Options, MessageIds>({

for (let constraint of constraints) {
if (
constraint.onlyDependOnLibsWithTags &&
hasNoneOfTheseTags(
targetProject,
constraint.onlyDependOnLibsWithTags
)
) {
const tags = constraint.onlyDependOnLibsWithTags
.map((s) => `"${s}"`)
.join(', ');
context.report({
node,
messageId: 'onlyTagsConstraintViolation',
data: {
sourceTag: constraint.sourceTag,
tags,
tags: stringifyTags(constraint.onlyDependOnLibsWithTags),
},
});
return;
}
if (
constraint.notDependOnLibsWithTags &&
hasAnyOfTheseTags(
projectGraph,
targetProject.name,
constraint.notDependOnLibsWithTags
)
) {
const tags = constraint.notDependOnLibsWithTags
.map((s) => `"${s}"`)
.join(', ');
context.report({
node,
messageId: 'notTagsConstraintViolation',
data: {
sourceTag: constraint.sourceTag,
tags,
tags: stringifyTags(constraint.notDependOnLibsWithTags),
},
});
return;
Expand Down
38 changes: 27 additions & 11 deletions packages/workspace/src/utils/runtime-lint-utils.ts
Expand Up @@ -30,30 +30,46 @@ export type DepConstraint = {
bannedExternalImports?: string[];
};

export function stringifyTags(tags: string[]): string {
return tags.map((t) => `"${t}"`).join(', ');
}

export function hasNoneOfTheseTags(
proj: ProjectGraphProjectNode,
tags: string[]
) {
proj: ProjectGraphProjectNode<any>,
tags?: string[]
): boolean {
if (!tags) {
return false;
}
return tags.filter((tag) => hasTag(proj, tag)).length === 0;
}

export function hasAnyOfTheseTags(
graph: ProjectGraph,
projectName: string,
tags: string[],
tags?: string[],
visited?: string[]
) {
visited = visited ?? [];

let found =
): boolean {
if (!tags) {
return false;
}
const found =
tags.filter((tag) => hasTag(graph.nodes[projectName], tag)).length !== 0;

if (found) return true;
if (found) {
return true;
}

visited = visited ?? [];

for (let d of graph.dependencies[projectName] || []) {
if (visited.indexOf(d.target) > -1) continue;
if (visited.indexOf(d.target) > -1) {
continue;
}
visited.push(d.target);
if (hasAnyOfTheseTags(graph, d.target, tags, visited)) return true;
if (hasAnyOfTheseTags(graph, d.target, tags, visited)) {
return true;
}
}

return false;
Expand Down

0 comments on commit c08b25b

Please sign in to comment.