feat(linter): add support for full regexes in tag matching (#14982)

This commit is contained in:
Miroslav Jonaš 2023-02-14 17:55:24 +01:00 committed by GitHub
parent b583f89095
commit 8165459568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import {
} from '@nrwl/devkit';
import {
DepConstraint,
findConstraintsFor,
findTransitiveExternalDependencies,
hasBannedDependencies,
hasBannedImport,
@ -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',

View File

@ -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(