feat(linter): add support for full regexes in tag matching (#14982)
This commit is contained in:
parent
b583f89095
commit
8165459568
@ -7,6 +7,7 @@ import {
|
|||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import {
|
import {
|
||||||
DepConstraint,
|
DepConstraint,
|
||||||
|
findConstraintsFor,
|
||||||
findTransitiveExternalDependencies,
|
findTransitiveExternalDependencies,
|
||||||
hasBannedDependencies,
|
hasBannedDependencies,
|
||||||
hasBannedImport,
|
hasBannedImport,
|
||||||
@ -19,6 +20,68 @@ jest.mock('nx/src/utils/workspace-root', () => ({
|
|||||||
workspaceRoot: '/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', () => {
|
describe('hasBannedImport', () => {
|
||||||
const source: ProjectGraphProjectNode = {
|
const source: ProjectGraphProjectNode = {
|
||||||
type: 'lib',
|
type: 'lib',
|
||||||
|
|||||||
@ -86,8 +86,23 @@ export function findDependenciesWithTags(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasTag(proj: ProjectGraphProjectNode, tag: string) {
|
const regexMap = new Map<string, RegExp>();
|
||||||
return tag === '*' || (proj.data.tags || []).indexOf(tag) > -1;
|
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(
|
export function matchImportWithWildcard(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user