Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions projects/commit-lint/src/lib/tasks/validate-nx-tags.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export function validateNxTagsTask(options: ValidateNxTagsOptions) {
);

try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
validateNxTags(require(nx), linterConfig, linter);
validateNxTags(linterConfig, linter);
} catch (e: any) {
errorWithExit(e.message);
}
Expand Down
89 changes: 5 additions & 84 deletions projects/commit-lint/src/lib/validators/nx-tags.validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,6 @@
import { Linter } from '../resources/enums/linter.enum';
import { validateNxTags } from './nx-tags.validator';

const nxJson = {
projects: {
'pet-shop-e2e': {
tags: [],
},
'pet-shop': {
tags: [],
},
'shared-data-access': {
tags: ['scope:shared', 'type:data-access'],
},
'shared-resource': {
tags: ['scope:shared', 'type:resource'],
},
'shared-ui-avatar': {
tags: ['scope:shared', 'type:ui'],
},
},
};
const nxJsonWithAdditionalDeclarations = {
projects: {
'pet-shop-e2e': {
tags: [],
},
'pet-shop': {
tags: [],
},
'dashboard-feature': {
tags: ['scope:dashboard', 'type:feature'],
},
'dashboard-filter-feature': {
tags: ['scope:dashboard-filter', 'type:feature'],
},
'dashboard-filter-data-access': {
tags: ['scope:dashboard-filter', 'type:data-access'],
},
'dashboard-filter-ui': {
tags: ['scope:dashboard-filter', 'type:ui'],
},
'dashboard-filter-util': {
tags: ['scope:dashboard-filter', 'type:util'],
},
'dashboard-filter-resource': {
tags: ['scope:dashboard-filter', 'type:resource'],
},
'dashboard-shared-data-access': {
tags: ['scope:dashboard-shared', 'type:data-access'],
},
'dashboard-shared-summary-resource': {
tags: ['scope:dashboard-shared', 'type:resource'],
},
'dashboard-shared-summary-ui': {
tags: ['scope:dashboard-shared', 'type:ui'],
},
'shared-data-access': {
tags: ['scope:shared', 'type:data-access'],
},
'shared-resource': {
tags: ['scope:shared', 'type:resource'],
},
'shared-ui-avatar': {
tags: ['scope:shared', 'type:ui'],
},
},
};
const tslintJson = {
rules: {
'nx-enforce-module-boundaries': [
Expand Down Expand Up @@ -291,40 +226,26 @@ const eslintjson = {

describe('validateNxTags', () => {
it('should not throw error with valid tslint config', () => {
expect(() =>
validateNxTags(nxJson, tslintJson, Linter.TsLint)
).not.toThrowError();
expect(() => validateNxTags(tslintJson, Linter.TsLint)).not.toThrowError();
});

it('should throw error with invalid tslint config', () => {
expect(() =>
validateNxTags(
nxJsonWithAdditionalDeclarations,
tslintJsonWithAdditionalDeclarations,
Linter.TsLint
)
validateNxTags(tslintJsonWithAdditionalDeclarations, Linter.TsLint)
).toThrowError();
});

it('should not throw error with valid eslintrc config', () => {
expect(() =>
validateNxTags(nxJson, eslintrc, Linter.TsLint)
).not.toThrowError();
expect(() => validateNxTags(eslintrc, Linter.TsLint)).not.toThrowError();
});

it('should not throw error with valid eslintjson config', () => {
expect(() =>
validateNxTags(nxJson, eslintjson, Linter.EsLint)
).not.toThrowError();
expect(() => validateNxTags(eslintjson, Linter.EsLint)).not.toThrowError();
});

it('should throw error with invalid eslintrc config', () => {
expect(() =>
validateNxTags(
nxJsonWithAdditionalDeclarations,
eslintrcWithAdditionalDeclarations,
Linter.EsLint
)
validateNxTags(eslintrcWithAdditionalDeclarations, Linter.EsLint)
).toThrowError();
});
});
18 changes: 9 additions & 9 deletions projects/commit-lint/src/lib/validators/nx-tags.validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Linter } from '../resources/enums/linter.enum';
import { getConfig } from '../utils/get-config.util';
import { readFileSync } from 'fs';
import { glob } from 'glob';

const tagsToIgnore = getConfig().nxTagsValidator.ignore;

Expand All @@ -22,11 +24,13 @@ function isReservedTag(tag: string): boolean {
return tagsToIgnore.indexOf(tag) !== -1;
}

function parseNxJson(nxJson: any): string[] {
function parseNxJson(): string[] {
const declaredTags: string[] = [];
glob.sync('**/project.json', { ignore: '**/node_modules/**' }).map((file) => {
const content = readFileSync(file, 'utf-8');
const projectJson = JSON.parse(content);

Object.keys(nxJson.projects).forEach((key) => {
nxJson.projects[key].tags.forEach((tag: string) => {
projectJson.tags.forEach((tag: string) => {
if (!isReservedTag(tag) && declaredTags.indexOf(tag) === -1) {
declaredTags.push(tag);
}
Expand Down Expand Up @@ -88,12 +92,8 @@ function extractTagsFromDepConstraints(constraints: any[]): string[] {
return usedTags;
}

export function validateNxTags(
nxJson: any,
lintJson: any,
linter: Linter
): void {
const declaredTags = parseNxJson(nxJson);
export function validateNxTags(lintJson: any, linter: Linter): void {
const declaredTags = parseNxJson();
let usedTags: string[] = [];

switch (linter) {
Expand Down