Skip to content

Commit de3e504

Browse files
meeroslavFrozenPandaz
authored andcommitted
feat(linter): add support for full regexes in tag matching (#14982)
(cherry picked from commit 8165459)
1 parent c21e9fb commit de3e504

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '@nrwl/devkit';
88
import {
99
DepConstraint,
10+
findConstraintsFor,
1011
findTransitiveExternalDependencies,
1112
hasBannedDependencies,
1213
hasBannedImport,
@@ -19,6 +20,68 @@ jest.mock('nx/src/utils/workspace-root', () => ({
1920
workspaceRoot: '/root',
2021
}));
2122

23+
describe('findConstraintsFor', () => {
24+
it('should find constraints matching tag', () => {
25+
const constriants: DepConstraint[] = [
26+
{ sourceTag: 'a', onlyDependOnLibsWithTags: ['b'] },
27+
{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] },
28+
{ sourceTag: 'c', onlyDependOnLibsWithTags: ['a'] },
29+
];
30+
31+
expect(
32+
findConstraintsFor(constriants, {
33+
type: 'lib',
34+
name: 'someLib',
35+
data: { root: '.', files: [], tags: ['b'] },
36+
})
37+
).toEqual([{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] }]);
38+
});
39+
40+
it('should find constraints matching *', () => {
41+
const constriants: DepConstraint[] = [
42+
{ sourceTag: 'a', onlyDependOnLibsWithTags: ['b'] },
43+
{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] },
44+
{ sourceTag: '*', onlyDependOnLibsWithTags: ['a'] },
45+
];
46+
47+
expect(
48+
findConstraintsFor(constriants, {
49+
type: 'lib',
50+
name: 'someLib',
51+
data: { root: '.', files: [], tags: ['b'] },
52+
})
53+
).toEqual([
54+
{ sourceTag: 'b', onlyDependOnLibsWithTags: ['c'] },
55+
{ sourceTag: '*', onlyDependOnLibsWithTags: ['a'] },
56+
]);
57+
});
58+
59+
it('should find constraints matching regex', () => {
60+
const constriants: DepConstraint[] = [
61+
{ sourceTag: 'a', onlyDependOnLibsWithTags: ['a'] },
62+
{ sourceTag: '/^b$/', onlyDependOnLibsWithTags: ['c'] },
63+
{ sourceTag: '/a|b/', onlyDependOnLibsWithTags: ['c'] },
64+
];
65+
expect(
66+
findConstraintsFor(constriants, {
67+
type: 'lib',
68+
name: 'someLib',
69+
data: { root: '.', files: [], tags: ['b'] },
70+
})
71+
).toEqual([
72+
{ sourceTag: '/^b$/', onlyDependOnLibsWithTags: ['c'] },
73+
{ sourceTag: '/a|b/', onlyDependOnLibsWithTags: ['c'] },
74+
]);
75+
expect(
76+
findConstraintsFor(constriants, {
77+
type: 'lib',
78+
name: 'someLib',
79+
data: { root: '.', files: [], tags: ['baz'] },
80+
})
81+
).toEqual([{ sourceTag: '/a|b/', onlyDependOnLibsWithTags: ['c'] }]);
82+
});
83+
});
84+
2285
describe('hasBannedImport', () => {
2386
const source: ProjectGraphProjectNode = {
2487
type: 'lib',

packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,23 @@ export function findDependenciesWithTags(
8686
);
8787
}
8888

89-
function hasTag(proj: ProjectGraphProjectNode, tag: string) {
90-
return tag === '*' || (proj.data.tags || []).indexOf(tag) > -1;
89+
const regexMap = new Map<string, RegExp>();
90+
function hasTag(proj: ProjectGraphProjectNode, tag: string): boolean {
91+
if (tag === '*') return true;
92+
93+
// if the tag is a regex, check if the project matches the regex
94+
if (tag.startsWith('/') && tag.endsWith('/')) {
95+
let regex;
96+
if (regexMap.has(tag)) {
97+
regex = regexMap.get(tag);
98+
} else {
99+
regex = new RegExp(tag.substring(1, tag.length - 1));
100+
regexMap.set(tag, regex);
101+
}
102+
return (proj.data.tags || []).some((t) => regex.test(t));
103+
}
104+
105+
return (proj.data.tags || []).indexOf(tag) > -1;
91106
}
92107

93108
export function matchImportWithWildcard(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy