Skip to content

Commit 85a8424

Browse files
authored
Improve messaging for no-archive-* rules (#607)
1 parent 5c33064 commit 85a8424

File tree

10 files changed

+108
-50
lines changed

10 files changed

+108
-50
lines changed

src/rules/no-archive-dependencies.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import {PackageJson} from 'type-fest';
2-
import {doVersContainArchiveUrl} from '../validators/dependency-audit';
2+
import {auditDependenciesForArchiveUrlVersion} 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-archive-dependencies';
88
const nodeName = 'dependencies';
9-
const message = 'You are using dependencies via url to archive file. Please use dependencies from npm.';
109

1110
export const ruleType = RuleType.OptionalObject;
1211

1312
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1413
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
15-
if (packageJsonData.hasOwnProperty(nodeName) && doVersContainArchiveUrl(packageJsonData, nodeName, config)) {
16-
return new LintIssue(lintId, severity, nodeName, message);
14+
const auditResult = auditDependenciesForArchiveUrlVersion(packageJsonData, nodeName, config);
15+
16+
if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasArchiveUrlVersions) {
17+
return new LintIssue(
18+
lintId,
19+
severity,
20+
nodeName,
21+
`You are using ${nodeName} via url to archive file. Please use ${nodeName} from npm. Invalid ${nodeName} include: ${auditResult.dependenciesWithArchiveUrlVersion.join(
22+
', '
23+
)}`
24+
);
1725
}
1826

1927
return null;

src/rules/no-archive-devDependencies.ts

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

77
const lintId = 'no-archive-devDependencies';
88
const nodeName = 'devDependencies';
9-
const message = 'You are using devDependencies via url to archive file. Please use devDependencies from npm.';
109

1110
export const ruleType = RuleType.OptionalObject;
1211

1312
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1413
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
15-
if (packageJsonData.hasOwnProperty(nodeName) && doVersContainArchiveUrl(packageJsonData, nodeName, config)) {
16-
return new LintIssue(lintId, severity, nodeName, message);
14+
const auditResult = auditDependenciesForArchiveUrlVersion(packageJsonData, nodeName, config);
15+
16+
if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasArchiveUrlVersions) {
17+
return new LintIssue(
18+
lintId,
19+
severity,
20+
nodeName,
21+
`You are using ${nodeName} via url to archive file. Please use ${nodeName} from npm. Invalid ${nodeName} include: ${auditResult.dependenciesWithArchiveUrlVersion.join(
22+
', '
23+
)}`
24+
);
1725
}
1826

1927
return null;

src/validators/dependency-audit.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,30 @@ export const doVersContainGitRepository = (packageJsonData: PackageJson | any, n
344344
return false;
345345
};
346346

347+
export interface AuditDependenciesForArchiveUrlVersionResponse {
348+
hasArchiveUrlVersions: boolean;
349+
dependenciesWithArchiveUrlVersion: string[];
350+
dependenciesWithoutArchiveUrlVersion: string[];
351+
}
352+
347353
/**
348354
* Determines whether or not dependency versions contains archive url
349-
* @param {object} packageJsonData Valid JSON
350-
* @param {string} nodeName Name of a node in the package.json file
351-
* @param {object} config Rule configuration
352-
* @return {boolean} True if the package contain archive url.
355+
* @param packageJsonData Valid JSON
356+
* @param nodeName Name of a node in the package.json file
357+
* @param config Rule configuration
358+
* @return True if the package contain archive url.
353359
*/
354-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
355-
export const doVersContainArchiveUrl = (packageJsonData: PackageJson | any, nodeName: string, config: any): boolean => {
360+
export const auditDependenciesForArchiveUrlVersion = (
361+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
362+
packageJsonData: PackageJson | any,
363+
nodeName: string,
364+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
365+
config: any
366+
): AuditDependenciesForArchiveUrlVersionResponse => {
367+
let hasArchiveUrlVersions = false;
368+
const dependenciesWithArchiveUrlVersion = [];
369+
const dependenciesWithoutArchiveUrlVersion = [];
370+
356371
// eslint-disable-next-line no-restricted-syntax
357372
for (const dependencyName in packageJsonData[nodeName]) {
358373
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
@@ -363,11 +378,18 @@ export const doVersContainArchiveUrl = (packageJsonData: PackageJson | any, node
363378
const dependencyVersion = packageJsonData[nodeName][dependencyName];
364379

365380
if (isArchiveUrl(dependencyVersion)) {
366-
return true;
381+
hasArchiveUrlVersions = true;
382+
dependenciesWithArchiveUrlVersion.push(dependencyName);
383+
} else {
384+
dependenciesWithoutArchiveUrlVersion.push(dependencyName);
367385
}
368386
}
369387

370-
return false;
388+
return {
389+
hasArchiveUrlVersions,
390+
dependenciesWithArchiveUrlVersion,
391+
dependenciesWithoutArchiveUrlVersion,
392+
};
371393
};
372394

373395
export interface AuditDependenciesForFileUrlVersionResponse {

src/validators/property.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {PackageJson} from 'type-fest';
88
* @param nodeName Name of a node in the package.json file
99
* @return True if the node exists. False if it is not.
1010
*/
11-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1212
export const exists = (packageJsonData: PackageJson | any, nodeName: string): boolean =>
1313
packageJsonData.hasOwnProperty(nodeName);
1414

src/validators/type.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {PackageJson} from 'type-fest';
88
* @param nodeName Name of a node in the package.json file
99
* @return True if the node is an array or is missing. False if it is not.
1010
*/
11-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
12-
export const isArray = (packageJsonData: PackageJson | any, nodeName: string) => {
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12+
export const isArray = (packageJsonData: PackageJson | any, nodeName: string): boolean => {
1313
if (!packageJsonData.hasOwnProperty(nodeName)) {
1414
return true;
1515
}
@@ -24,8 +24,8 @@ export const isArray = (packageJsonData: PackageJson | any, nodeName: string) =>
2424
* @param nodeName Name of a node in the package.json file
2525
* @return True if the node is a boolean or is missing. False if it is not.
2626
*/
27-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
28-
export const isBoolean = (packageJsonData: PackageJson | any, nodeName: string) => {
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
export const isBoolean = (packageJsonData: PackageJson | any, nodeName: string): boolean => {
2929
if (!packageJsonData.hasOwnProperty(nodeName)) {
3030
return true;
3131
}
@@ -40,8 +40,8 @@ export const isBoolean = (packageJsonData: PackageJson | any, nodeName: string)
4040
* @param nodeName Name of a node in the package.json file
4141
* @return True if the node is an object or is missing. False if it is not.
4242
*/
43-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
44-
export const isObject = (packageJsonData: PackageJson | any, nodeName: string) => {
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44+
export const isObject = (packageJsonData: PackageJson | any, nodeName: string): boolean => {
4545
if (!packageJsonData.hasOwnProperty(nodeName)) {
4646
return true;
4747
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('no-archive-dependencies Unit Tests', () => {
2222
expect(response.severity).toStrictEqual('error');
2323
expect(response.node).toStrictEqual('dependencies');
2424
expect(response.lintMessage).toStrictEqual(
25-
'You are using dependencies via url to archive file. Please use dependencies from npm.'
25+
'You are using dependencies via url to archive file. Please use dependencies from npm. Invalid dependencies include: test-module'
2626
);
2727
});
2828
});
@@ -40,7 +40,7 @@ describe('no-archive-dependencies Unit Tests', () => {
4040
expect(response.severity).toStrictEqual('error');
4141
expect(response.node).toStrictEqual('dependencies');
4242
expect(response.lintMessage).toStrictEqual(
43-
'You are using dependencies via url to archive file. Please use dependencies from npm.'
43+
'You are using dependencies via url to archive file. Please use dependencies from npm. Invalid dependencies include: test-module'
4444
);
4545
});
4646
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('no-archive-devDependencies Unit Tests', () => {
2222
expect(response.severity).toStrictEqual('error');
2323
expect(response.node).toStrictEqual('devDependencies');
2424
expect(response.lintMessage).toStrictEqual(
25-
'You are using devDependencies via url to archive file. Please use devDependencies from npm.'
25+
'You are using devDependencies via url to archive file. Please use devDependencies from npm. Invalid devDependencies include: test-module'
2626
);
2727
});
2828
});
@@ -40,7 +40,7 @@ describe('no-archive-devDependencies Unit Tests', () => {
4040
expect(response.severity).toStrictEqual('error');
4141
expect(response.node).toStrictEqual('devDependencies');
4242
expect(response.lintMessage).toStrictEqual(
43-
'You are using devDependencies via url to archive file. Please use devDependencies from npm.'
43+
'You are using devDependencies via url to archive file. Please use devDependencies from npm. Invalid devDependencies include: test-module'
4444
);
4545
});
4646
});

test/unit/validators/dependency-audit.test.ts

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,23 @@ describe('dependency-audit Unit Tests', () => {
856856
expect(response).toBe(true);
857857
});
858858
});
859+
860+
describe('when the node exists in the package.json file, one absolute version but has expection', () => {
861+
test('false should be returned', () => {
862+
const packageJson = {
863+
dependencies: {
864+
'module-from-archive': 'https://github.com/miripiruni/repo/archive/v1.2.3.zip',
865+
'grunt-npm-package-json-lint': '2.0.0',
866+
'gulp-npm-package-json-lint': '^2.0.0',
867+
},
868+
};
869+
const response = dependencyAudit.doVersContainGitRepository(packageJson, 'dependencies', {
870+
exceptions: ['module-from-archive'],
871+
});
872+
873+
expect(response).toBe(false);
874+
});
875+
});
859876
});
860877

861878
describe('doVersContainNonAbsolute method', () => {
@@ -965,17 +982,21 @@ describe('dependency-audit Unit Tests', () => {
965982
});
966983
});
967984

968-
describe('doVersContainArchiveUrl method', () => {
985+
describe('auditDependenciesForArchiveUrlVersion method', () => {
969986
describe('when the node exists in the package.json file, some versions are archive url', () => {
970987
test('with tar.gz dependency true should be returned', () => {
971988
const packageJson = {
972989
dependencies: {
973990
'my-module': 'https://github.com/miripiruni/repo/archive/v1.2.3.tar.gz',
974991
},
975992
};
976-
const response = dependencyAudit.doVersContainArchiveUrl(packageJson, 'dependencies', {});
993+
const response = dependencyAudit.auditDependenciesForArchiveUrlVersion(packageJson, 'dependencies', {});
977994

978-
expect(response).toBe(true);
995+
expect(response).toStrictEqual({
996+
hasArchiveUrlVersions: true,
997+
dependenciesWithArchiveUrlVersion: ['my-module'],
998+
dependenciesWithoutArchiveUrlVersion: [],
999+
});
9791000
});
9801001

9811002
test('with zip dependency true should be returned', () => {
@@ -984,9 +1005,13 @@ describe('dependency-audit Unit Tests', () => {
9841005
'my-module': 'https://github.com/miripiruni/repo/archive/v1.2.3.zip',
9851006
},
9861007
};
987-
const response = dependencyAudit.doVersContainArchiveUrl(packageJson, 'dependencies', {});
1008+
const response = dependencyAudit.auditDependenciesForArchiveUrlVersion(packageJson, 'dependencies', {});
9881009

989-
expect(response).toBe(true);
1010+
expect(response).toStrictEqual({
1011+
hasArchiveUrlVersions: true,
1012+
dependenciesWithArchiveUrlVersion: ['my-module'],
1013+
dependenciesWithoutArchiveUrlVersion: [],
1014+
});
9901015
});
9911016
});
9921017

@@ -1001,26 +1026,19 @@ describe('dependency-audit Unit Tests', () => {
10011026
'module-from-archive': 'https://github.com/user/repo.git',
10021027
},
10031028
};
1004-
const response = dependencyAudit.doVersContainArchiveUrl(packageJson, 'dependencies', {});
1029+
const response = dependencyAudit.auditDependenciesForArchiveUrlVersion(packageJson, 'dependencies', {});
10051030

1006-
expect(response).toBe(false);
1007-
});
1008-
});
1009-
1010-
describe('when the node exists in the package.json file, one absolute version but has expection', () => {
1011-
test('false should be returned', () => {
1012-
const packageJson = {
1013-
dependencies: {
1014-
'module-from-archive': 'https://github.com/miripiruni/repo/archive/v1.2.3.zip',
1015-
'grunt-npm-package-json-lint': '2.0.0',
1016-
'gulp-npm-package-json-lint': '^2.0.0',
1017-
},
1018-
};
1019-
const response = dependencyAudit.doVersContainGitRepository(packageJson, 'dependencies', {
1020-
exceptions: ['module-from-archive'],
1031+
expect(response).toStrictEqual({
1032+
hasArchiveUrlVersions: false,
1033+
dependenciesWithArchiveUrlVersion: [],
1034+
dependenciesWithoutArchiveUrlVersion: [
1035+
'npm-package-json-lint',
1036+
'grunt-npm-package-json-lint',
1037+
'gulp-npm-package-json-lint',
1038+
'module-from-local',
1039+
'module-from-archive',
1040+
],
10211041
});
1022-
1023-
expect(response).toBe(false);
10241042
});
10251043
});
10261044
});

website/docs/rules/dependencies/no-archive-dependencies.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ With exceptions
7878

7979
## History
8080

81+
* Improved messaging when an invalid configuration is detected in version 6.3.0
8182
* Introduced in version 4.3.0

website/docs/rules/dependencies/no-archive-devDependencies.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ With exceptions
7878

7979
## History
8080

81+
* Improved messaging when an invalid configuration is detected in version 6.3.0
8182
* Introduced in version 4.3.0

0 commit comments

Comments
 (0)