Skip to content

Commit

Permalink
feat(linter): add support for full regexes in tag matching (#14982)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8165459)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Feb 15, 2023
1 parent c21e9fb commit de3e504
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
63 changes: 63 additions & 0 deletions packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@nrwl/devkit';
import {
DepConstraint,
findConstraintsFor,
findTransitiveExternalDependencies,
hasBannedDependencies,
hasBannedImport,
Expand All @@ -19,6 +20,68 @@ jest.mock('nx/src/utils/workspace-root', () => ({
workspaceRoot: '/root',
}));

describe('findConstraintsFor', () => {
it('should find constraints matching tag', () => {
const constriants: DepConstraint[] = [
{ sourceTag: 'a', onlyDependOnLibsWithTags: ['b'] },
{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] },
{ sourceTag: 'c', onlyDependOnLibsWithTags: ['a'] },
];

expect(
findConstraintsFor(constriants, {
type: 'lib',
name: 'someLib',
data: { root: '.', files: [], tags: ['b'] },
})
).toEqual([{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] }]);
});

it('should find constraints matching *', () => {
const constriants: DepConstraint[] = [
{ sourceTag: 'a', onlyDependOnLibsWithTags: ['b'] },
{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] },
{ sourceTag: '*', onlyDependOnLibsWithTags: ['a'] },
];

expect(
findConstraintsFor(constriants, {
type: 'lib',
name: 'someLib',
data: { root: '.', files: [], tags: ['b'] },
})
).toEqual([
{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] },
{ sourceTag: '*', onlyDependOnLibsWithTags: ['a'] },
]);
});

it('should find constraints matching regex', () => {
const constriants: DepConstraint[] = [
{ sourceTag: 'a', onlyDependOnLibsWithTags: ['a'] },
{ sourceTag: '/^b$/', onlyDependOnLibsWithTags: ['c'] },
{ sourceTag: '/a|b/', onlyDependOnLibsWithTags: ['c'] },
];
expect(
findConstraintsFor(constriants, {
type: 'lib',
name: 'someLib',
data: { root: '.', files: [], tags: ['b'] },
})
).toEqual([
{ sourceTag: '/^b$/', onlyDependOnLibsWithTags: ['c'] },
{ sourceTag: '/a|b/', onlyDependOnLibsWithTags: ['c'] },
]);
expect(
findConstraintsFor(constriants, {
type: 'lib',
name: 'someLib',
data: { root: '.', files: [], tags: ['baz'] },
})
).toEqual([{ sourceTag: '/a|b/', onlyDependOnLibsWithTags: ['c'] }]);
});
});

describe('hasBannedImport', () => {
const source: ProjectGraphProjectNode = {
type: 'lib',
Expand Down
19 changes: 17 additions & 2 deletions packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,23 @@ export function findDependenciesWithTags(
);
}

function hasTag(proj: ProjectGraphProjectNode, tag: string) {
return tag === '*' || (proj.data.tags || []).indexOf(tag) > -1;
const regexMap = new Map<string, RegExp>();
function hasTag(proj: ProjectGraphProjectNode, tag: string): boolean {
if (tag === '*') return true;

// if the tag is a regex, check if the project matches the regex
if (tag.startsWith('/') && tag.endsWith('/')) {
let regex;
if (regexMap.has(tag)) {
regex = regexMap.get(tag);
} else {
regex = new RegExp(tag.substring(1, tag.length - 1));
regexMap.set(tag, regex);
}
return (proj.data.tags || []).some((t) => regex.test(t));
}

return (proj.data.tags || []).indexOf(tag) > -1;
}

export function matchImportWithWildcard(
Expand Down

0 comments on commit de3e504

Please sign in to comment.