Skip to content

Commit 947bcc2

Browse files
authored
Improve output for no-restricted-* rules (#615)
1 parent 81aa1d0 commit 947bcc2

14 files changed

+205
-64
lines changed

src/rules/no-restricted-dependencies.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {PackageJson} from 'type-fest';
2-
import {hasDependency} from '../validators/dependency-audit';
2+
import {auditDependenciesWithRestrictedVersion} from '../validators/dependency-audit';
33
import {LintIssue} from '../lint-issue';
44
import {RuleType} from '../types/rule-type';
55
import {Severity} from '../types/severity';
66

77
const lintId = 'no-restricted-dependencies';
88
const nodeName = 'dependencies';
9-
const message = 'You are using a restricted dependency. Please remove it.';
109

1110
export const ruleType = RuleType.Array;
1211

@@ -18,8 +17,17 @@ export const lint = (
1817
severity: Severity,
1918
invalidDependencies: string[]
2019
): LintIssue | null => {
21-
if (hasDependency(packageJsonData, nodeName, invalidDependencies)) {
22-
return new LintIssue(lintId, severity, nodeName, message);
20+
const auditResult = auditDependenciesWithRestrictedVersion(packageJsonData, nodeName, invalidDependencies);
21+
22+
if (auditResult.hasDependencyWithRestrictedVersion) {
23+
return new LintIssue(
24+
lintId,
25+
severity,
26+
nodeName,
27+
`You are using a restricted dependency. Please remove it. Invalid ${nodeName} include: ${auditResult.dependenciesWithRestrictedVersion.join(
28+
', '
29+
)}`
30+
);
2331
}
2432

2533
return null;

src/rules/no-restricted-devDependencies.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {PackageJson} from 'type-fest';
2-
import {hasDependency} from '../validators/dependency-audit';
2+
import {auditDependenciesWithRestrictedVersion} from '../validators/dependency-audit';
33
import {LintIssue} from '../lint-issue';
44
import {RuleType} from '../types/rule-type';
55
import {Severity} from '../types/severity';
66

77
const lintId = 'no-restricted-devDependencies';
88
const nodeName = 'devDependencies';
9-
const message = 'You are using a restricted dependency. Please remove it.';
109

1110
export const ruleType = RuleType.Array;
1211

@@ -18,8 +17,17 @@ export const lint = (
1817
severity: Severity,
1918
invalidDependencies: string[]
2019
): LintIssue | null => {
21-
if (hasDependency(packageJsonData, nodeName, invalidDependencies)) {
22-
return new LintIssue(lintId, severity, nodeName, message);
20+
const auditResult = auditDependenciesWithRestrictedVersion(packageJsonData, nodeName, invalidDependencies);
21+
22+
if (auditResult.hasDependencyWithRestrictedVersion) {
23+
return new LintIssue(
24+
lintId,
25+
severity,
26+
nodeName,
27+
`You are using a restricted dependency. Please remove it. Invalid ${nodeName} include: ${auditResult.dependenciesWithRestrictedVersion.join(
28+
', '
29+
)}`
30+
);
2331
}
2432

2533
return null;

src/rules/no-restricted-pre-release-dependencies.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {PackageJson} from 'type-fest';
2-
import {hasDepPrereleaseVers} from '../validators/dependency-audit';
2+
import {auditDependenciesWithRestrictedPrereleaseVersion} from '../validators/dependency-audit';
33
import {LintIssue} from '../lint-issue';
44
import {RuleType} from '../types/rule-type';
55
import {Severity} from '../types/severity';
66

77
const lintId = 'no-restricted-pre-release-dependencies';
88
const nodeName = 'dependencies';
9-
const message = 'You are using a restricted pre-release dependency. Please remove it.';
109

1110
export const ruleType = RuleType.Array;
1211

@@ -18,8 +17,17 @@ export const lint = (
1817
severity: Severity,
1918
invalidPreRelDeps: string[]
2019
): LintIssue | null => {
21-
if (hasDepPrereleaseVers(packageJsonData, nodeName, invalidPreRelDeps)) {
22-
return new LintIssue(lintId, severity, nodeName, message);
20+
const auditResult = auditDependenciesWithRestrictedPrereleaseVersion(packageJsonData, nodeName, invalidPreRelDeps);
21+
22+
if (auditResult.hasDependencyWithRestrictedPrereleaseVersion) {
23+
return new LintIssue(
24+
lintId,
25+
severity,
26+
nodeName,
27+
`You are using a restricted pre-release dependency. Please remove it. Invalid ${nodeName} include: ${auditResult.dependenciesWithRestrictedPrereleaseVersion.join(
28+
', '
29+
)}`
30+
);
2331
}
2432

2533
return null;

src/rules/no-restricted-pre-release-devDependencies.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {PackageJson} from 'type-fest';
2-
import {hasDepPrereleaseVers} from '../validators/dependency-audit';
2+
import {auditDependenciesWithRestrictedPrereleaseVersion} from '../validators/dependency-audit';
33
import {LintIssue} from '../lint-issue';
44
import {RuleType} from '../types/rule-type';
55
import {Severity} from '../types/severity';
66

77
const lintId = 'no-restricted-pre-release-devDependencies';
88
const nodeName = 'devDependencies';
9-
const message = 'You are using a restricted pre-release dependency. Please remove it.';
109

1110
export const ruleType = RuleType.Array;
1211

@@ -18,8 +17,17 @@ export const lint = (
1817
severity: Severity,
1918
invalidPreRelDeps: string[]
2019
): LintIssue | null => {
21-
if (hasDepPrereleaseVers(packageJsonData, nodeName, invalidPreRelDeps)) {
22-
return new LintIssue(lintId, severity, nodeName, message);
20+
const auditResult = auditDependenciesWithRestrictedPrereleaseVersion(packageJsonData, nodeName, invalidPreRelDeps);
21+
22+
if (auditResult.hasDependencyWithRestrictedPrereleaseVersion) {
23+
return new LintIssue(
24+
lintId,
25+
severity,
26+
nodeName,
27+
`You are using a restricted pre-release dependency. Please remove it. Invalid ${nodeName} include: ${auditResult.dependenciesWithRestrictedPrereleaseVersion.join(
28+
', '
29+
)}`
30+
);
2331
}
2432

2533
return null;

src/validators/dependency-audit.ts

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,92 @@ const semver = require('semver');
77
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88
const hasExceptions = (config: any): boolean => typeof config === 'object' && config.hasOwnProperty('exceptions');
99

10+
export interface AuditDependenciesWithRestrictedVersionResponse {
11+
hasDependencyWithRestrictedVersion: boolean;
12+
dependenciesWithRestrictedVersion: string[];
13+
dependenciesWithoutRestrictedVersion: string[];
14+
}
15+
1016
/**
1117
* Determines whether or not the package has a given dependency
12-
* @param {object} packageJsonData Valid JSON
13-
* @param {string} nodeName Name of a node in the package.json file
14-
* @param {string} depsToCheckFor An array of packages to check for
15-
* @return {boolean} True if the package has a dependency. False if it is not or the node is missing.
18+
* @param packageJsonData Valid JSON
19+
* @param nodeName Name of a node in the package.json file
20+
* @param depsToCheckFor An array of packages to check for
21+
* @return True if the package has a dependency. False if it is not or the node is missing.
1622
*/
17-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18-
export const hasDependency = (packageJsonData: PackageJson | any, nodeName: string, depsToCheckFor: string[]): boolean => {
23+
export const auditDependenciesWithRestrictedVersion = (
24+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
25+
packageJsonData: PackageJson | any,
26+
nodeName: string,
27+
depsToCheckFor: string[]
28+
): AuditDependenciesWithRestrictedVersionResponse => {
29+
let hasDependencyWithRestrictedVersion = false;
30+
const dependenciesWithRestrictedVersion = [];
31+
const dependenciesWithoutRestrictedVersion = [];
32+
1933
if (!packageJsonData.hasOwnProperty(nodeName)) {
20-
return false;
34+
return {
35+
hasDependencyWithRestrictedVersion,
36+
dependenciesWithRestrictedVersion,
37+
dependenciesWithoutRestrictedVersion,
38+
};
2139
}
2240

2341
// eslint-disable-next-line no-restricted-syntax, guard-for-in
2442
for (const dependencyName in packageJsonData[nodeName]) {
2543
// eslint-disable-next-line no-restricted-syntax
2644
for (const depToCheckFor of depsToCheckFor) {
2745
if (depToCheckFor === dependencyName) {
28-
return true;
29-
}
30-
31-
if (
46+
hasDependencyWithRestrictedVersion = true;
47+
dependenciesWithRestrictedVersion.push(dependencyName);
48+
} else if (
3249
depToCheckFor.endsWith('*') &&
3350
dependencyName.startsWith(depToCheckFor.slice(0, Math.max(0, depToCheckFor.length - 1)))
3451
) {
35-
return true;
52+
hasDependencyWithRestrictedVersion = true;
53+
dependenciesWithRestrictedVersion.push(dependencyName);
54+
} else {
55+
dependenciesWithoutRestrictedVersion.push(dependencyName);
3656
}
3757
}
3858
}
3959

40-
return false;
60+
return {
61+
hasDependencyWithRestrictedVersion,
62+
dependenciesWithRestrictedVersion,
63+
dependenciesWithoutRestrictedVersion,
64+
};
4165
};
4266

67+
export interface AuditDependenciesWithRestrictedPrereleaseVersionResponse {
68+
hasDependencyWithRestrictedPrereleaseVersion: boolean;
69+
dependenciesWithRestrictedPrereleaseVersion: string[];
70+
dependenciesWithoutRestrictedPrereleaseVersion: string[];
71+
}
72+
4373
/**
4474
* Determines whether or not the package has a pre-release version of a given dependency
45-
* @param {object} packageJsonData Valid JSON
46-
* @param {string} nodeName Name of a node in the package.json file
47-
* @param {string} depsToCheckFor An array of packages to check for
48-
* @return {boolean} True if the package has a pre-release version of a dependency. False if it is not or the node is missing.
75+
* @param packageJsonData Valid JSON
76+
* @param nodeName Name of a node in the package.json file
77+
* @param depsToCheckFor An array of packages to check for
78+
* @return True if the package has a pre-release version of a dependency. False if it is not or the node is missing.
4979
*/
50-
export const hasDepPrereleaseVers = (
80+
export const auditDependenciesWithRestrictedPrereleaseVersion = (
5181
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5282
packageJsonData: PackageJson | any,
5383
nodeName: string,
5484
depsToCheckFor: string[]
55-
): boolean => {
85+
): AuditDependenciesWithRestrictedPrereleaseVersionResponse => {
86+
let hasDependencyWithRestrictedPrereleaseVersion = false;
87+
const dependenciesWithRestrictedPrereleaseVersion = [];
88+
const dependenciesWithoutRestrictedPrereleaseVersion = [];
89+
5690
if (!packageJsonData.hasOwnProperty(nodeName)) {
57-
return false;
91+
return {
92+
hasDependencyWithRestrictedPrereleaseVersion,
93+
dependenciesWithRestrictedPrereleaseVersion,
94+
dependenciesWithoutRestrictedPrereleaseVersion,
95+
};
5896
}
5997

6098
// eslint-disable-next-line no-restricted-syntax
@@ -63,12 +101,19 @@ export const hasDepPrereleaseVers = (
63101
const dependencyVersion = packageJsonData[nodeName][dependencyName];
64102

65103
if (dependencyVersion.includes('-beta') || dependencyVersion.includes('-rc')) {
66-
return true;
104+
hasDependencyWithRestrictedPrereleaseVersion = true;
105+
dependenciesWithRestrictedPrereleaseVersion.push(dependencyName);
106+
} else {
107+
dependenciesWithoutRestrictedPrereleaseVersion.push(dependencyName);
67108
}
68109
}
69110
}
70111

71-
return false;
112+
return {
113+
hasDependencyWithRestrictedPrereleaseVersion,
114+
dependenciesWithRestrictedPrereleaseVersion,
115+
dependenciesWithoutRestrictedPrereleaseVersion,
116+
};
72117
};
73118

74119
export interface AuditDependenciesWithMajorVersionOfZeroResponse {

test/unit/rules/no-restricted-dependencies.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('no-restricted-dependencies Unit Tests', () => {
2727
expect(response.lintId).toStrictEqual('no-restricted-dependencies');
2828
expect(response.severity).toStrictEqual('error');
2929
expect(response.node).toStrictEqual('dependencies');
30-
expect(response.lintMessage).toStrictEqual('You are using a restricted dependency. Please remove it.');
30+
expect(response.lintMessage).toStrictEqual(
31+
'You are using a restricted dependency. Please remove it. Invalid dependencies include: npm-package-json-lint'
32+
);
3133
});
3234
});
3335

@@ -44,7 +46,9 @@ describe('no-restricted-dependencies Unit Tests', () => {
4446
expect(response.lintId).toStrictEqual('no-restricted-dependencies');
4547
expect(response.severity).toStrictEqual('error');
4648
expect(response.node).toStrictEqual('dependencies');
47-
expect(response.lintMessage).toStrictEqual('You are using a restricted dependency. Please remove it.');
49+
expect(response.lintMessage).toStrictEqual(
50+
'You are using a restricted dependency. Please remove it. Invalid dependencies include: @types/node'
51+
);
4852
});
4953
});
5054

test/unit/rules/no-restricted-devDependencies.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('no-restricted-devDependencies Unit Tests', () => {
2727
expect(response.lintId).toStrictEqual('no-restricted-devDependencies');
2828
expect(response.severity).toStrictEqual('error');
2929
expect(response.node).toStrictEqual('devDependencies');
30-
expect(response.lintMessage).toStrictEqual('You are using a restricted dependency. Please remove it.');
30+
expect(response.lintMessage).toStrictEqual(
31+
'You are using a restricted dependency. Please remove it. Invalid devDependencies include: npm-package-json-lint'
32+
);
3133
});
3234
});
3335

@@ -44,7 +46,9 @@ describe('no-restricted-devDependencies Unit Tests', () => {
4446
expect(response.lintId).toStrictEqual('no-restricted-devDependencies');
4547
expect(response.severity).toStrictEqual('error');
4648
expect(response.node).toStrictEqual('devDependencies');
47-
expect(response.lintMessage).toStrictEqual('You are using a restricted dependency. Please remove it.');
49+
expect(response.lintMessage).toStrictEqual(
50+
'You are using a restricted dependency. Please remove it. Invalid devDependencies include: @types/node'
51+
);
4852
});
4953
});
5054

test/unit/rules/no-restricted-pre-release-dependencies.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('no-restricted-pre-release-dependencies Unit Tests', () => {
2727
expect(response.lintId).toStrictEqual('no-restricted-pre-release-dependencies');
2828
expect(response.severity).toStrictEqual('error');
2929
expect(response.node).toStrictEqual('dependencies');
30-
expect(response.lintMessage).toStrictEqual('You are using a restricted pre-release dependency. Please remove it.');
30+
expect(response.lintMessage).toStrictEqual(
31+
'You are using a restricted pre-release dependency. Please remove it. Invalid dependencies include: npm-package-json-lint'
32+
);
3133
});
3234
});
3335

test/unit/rules/no-restricted-pre-release-devDependencies.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('no-restricted-pre-release-devDependencies Unit Tests', () => {
2727
expect(response.lintId).toStrictEqual('no-restricted-pre-release-devDependencies');
2828
expect(response.severity).toStrictEqual('error');
2929
expect(response.node).toStrictEqual('devDependencies');
30-
expect(response.lintMessage).toStrictEqual('You are using a restricted pre-release dependency. Please remove it.');
30+
expect(response.lintMessage).toStrictEqual(
31+
'You are using a restricted pre-release dependency. Please remove it. Invalid devDependencies include: npm-package-json-lint'
32+
);
3133
});
3234
});
3335

0 commit comments

Comments
 (0)