diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts index 675c17c0ae1f94..78ff9b1954f8ec 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts @@ -17,6 +17,7 @@ import { hasBannedImport, isDirectDependency, isTerminalRun, + stringifyTags, } from '@nrwl/workspace/src/utils/runtime-lint-utils'; import { AST_NODE_TYPES, @@ -397,42 +398,34 @@ export default createESLintRule({ 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; diff --git a/packages/workspace/src/utils/runtime-lint-utils.ts b/packages/workspace/src/utils/runtime-lint-utils.ts index 876ef2a42b61e8..ed601130071c44 100644 --- a/packages/workspace/src/utils/runtime-lint-utils.ts +++ b/packages/workspace/src/utils/runtime-lint-utils.ts @@ -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, + 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;