Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve output for absolute version rules #611

Merged
merged 1 commit into from
Mar 20, 2022
Merged
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
16 changes: 12 additions & 4 deletions src/rules/no-absolute-version-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PackageJson} from 'type-fest';
import {areVersionsAbsolute} from '../validators/dependency-audit';
import {auditDependenciesForAbsoluteVersion} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';
Expand All @@ -8,7 +8,6 @@ import {LintResult} from '../types/lint-result';

const lintId = 'no-absolute-version-dependencies';
const nodeName = 'dependencies';
const message = 'You are using an invalid version range. Please do not use absolute versions.';

export const ruleType = RuleType.OptionalObject;

Expand All @@ -18,8 +17,17 @@ export const lint = (
severity: Severity,
config: OptionalObjectRuleConfig
): LintResult => {
if (packageJsonData.hasOwnProperty(nodeName) && areVersionsAbsolute(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesForAbsoluteVersion(packageJsonData, nodeName, config);

if (packageJsonData.hasOwnProperty(nodeName) && auditResult.onlyAbsoluteVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use absolute versions. Invalid ${nodeName} include: ${auditResult.dependenciesWithAbsoluteVersion.join(
', '
)}`
);
}

return null;
Expand Down
16 changes: 12 additions & 4 deletions src/rules/no-absolute-version-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import {PackageJson} from 'type-fest';
import {areVersionsAbsolute} from '../validators/dependency-audit';
import {auditDependenciesForAbsoluteVersion} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-absolute-version-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using an invalid version range. Please do not use absolute versions.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
if (packageJsonData.hasOwnProperty(nodeName) && areVersionsAbsolute(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesForAbsoluteVersion(packageJsonData, nodeName, config);

if (packageJsonData.hasOwnProperty(nodeName) && auditResult.onlyAbsoluteVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use absolute versions. Invalid ${nodeName} includes: ${auditResult.dependenciesWithAbsoluteVersion.join(
', '
)}`
);
}

return null;
Expand Down
16 changes: 12 additions & 4 deletions src/rules/prefer-absolute-version-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import {PackageJson} from 'type-fest';
import {doVersContainNonAbsolute} from '../validators/dependency-audit';
import {auditDependenciesForNonAbsoluteVersion} from '../validators/dependency-audit';
import {exists} from '../validators/property';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'prefer-absolute-version-dependencies';
const nodeName = 'dependencies';
const message = 'You are using an invalid version range. Please use absolute versions.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
if (exists(packageJsonData, nodeName) && doVersContainNonAbsolute(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesForNonAbsoluteVersion(packageJsonData, nodeName, config);

if (exists(packageJsonData, nodeName) && auditResult.onlyNonAbsoluteVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please use absolute versions. Invalid ${nodeName} include: ${auditResult.dependenciesWithoutAbsoluteVersion.join(
', '
)}`
);
}

return null;
Expand Down
16 changes: 12 additions & 4 deletions src/rules/prefer-absolute-version-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import {PackageJson} from 'type-fest';
import {doVersContainNonAbsolute} from '../validators/dependency-audit';
import {auditDependenciesForNonAbsoluteVersion} from '../validators/dependency-audit';
import {exists} from '../validators/property';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'prefer-absolute-version-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using an invalid version range. Please use absolute versions.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
if (exists(packageJsonData, nodeName) && doVersContainNonAbsolute(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesForNonAbsoluteVersion(packageJsonData, nodeName, config);

if (exists(packageJsonData, nodeName) && auditResult.onlyNonAbsoluteVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please use absolute versions. Invalid ${nodeName} include: ${auditResult.dependenciesWithoutAbsoluteVersion.join(
', '
)}`
);
}

return null;
Expand Down
74 changes: 57 additions & 17 deletions src/validators/dependency-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ export interface AbsoluteVersionCheckerResult {

/**
* Determines whether or not all dependency versions are absolute
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @param config Rule configuration
* @return False if the package has an non-absolute version. True if it is not or the node is missing.
*/
const absoluteVersionChecker = (
const auditAbsoluteVersions = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJsonData: PackageJson | any,
nodeName: string,
Expand Down Expand Up @@ -256,32 +256,72 @@ const absoluteVersionChecker = (
};
};

export interface AuditDependenciesForAbsoluteVersionResponse {
onlyAbsoluteVersionsDetected: boolean;
dependenciesWithAbsoluteVersion: string[];
dependenciesWithoutAbsoluteVersion: string[];
}

/**
* Determines whether or not all dependency versions are absolut
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @param config Rule configuration
* @return False if the package has an non-absolute version. True if it is not or the node is missing.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const areVersionsAbsolute = (packageJsonData: PackageJson | any, nodeName: string, config: any): boolean => {
const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config);
export const auditDependenciesForAbsoluteVersion = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJsonData: PackageJson | any,
nodeName: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
): AuditDependenciesForAbsoluteVersionResponse => {
const {
onlyAbsoluteVersionDetected,
dependenciesChecked,
dependenciesWithAbsoluteVersion,
dependenciesWithoutAbsoluteVersion,
} = auditAbsoluteVersions(packageJsonData, nodeName, config);

return dependenciesChecked > 0 ? onlyAbsoluteVersionDetected : false;
return {
onlyAbsoluteVersionsDetected: dependenciesChecked > 0 ? onlyAbsoluteVersionDetected : false,
dependenciesWithAbsoluteVersion,
dependenciesWithoutAbsoluteVersion,
};
};

export interface AuditDependenciesForNonAbsoluteVersionResponse {
onlyNonAbsoluteVersionsDetected: boolean;
dependenciesWithAbsoluteVersion: string[];
dependenciesWithoutAbsoluteVersion: string[];
}

/**
* Determines whether or not all dependency versions are absolut
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @param config Rule configuration
* @return False if the package has an non-absolute version. True if it is not or the node is missing.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const doVersContainNonAbsolute = (packageJsonData: PackageJson | any, nodeName: string, config: any): boolean => {
const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config);
export const auditDependenciesForNonAbsoluteVersion = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJsonData: PackageJson | any,
nodeName: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
): AuditDependenciesForNonAbsoluteVersionResponse => {
const {
onlyAbsoluteVersionDetected,
dependenciesChecked,
dependenciesWithAbsoluteVersion,
dependenciesWithoutAbsoluteVersion,
} = auditAbsoluteVersions(packageJsonData, nodeName, config);

return dependenciesChecked > 0 ? !onlyAbsoluteVersionDetected : false;
return {
onlyNonAbsoluteVersionsDetected: dependenciesChecked > 0 ? !onlyAbsoluteVersionDetected : false,
dependenciesWithAbsoluteVersion,
dependenciesWithoutAbsoluteVersion,
};
};

const GITHUB_SHORTCUT_URL = /^(github:)?[^/]+\/[^/]+/;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/rules/no-absolute-version-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('no-absolute-version-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use absolute versions.'
'You are using an invalid version range. Please do not use absolute versions. Invalid dependencies include: npm-package-json-lint'
);
});
});
Expand All @@ -39,7 +39,7 @@ describe('no-absolute-version-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use absolute versions.'
'You are using an invalid version range. Please do not use absolute versions. Invalid dependencies include: npm-package-json-lint'
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/rules/no-absolute-version-devDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('no-absolute-version-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use absolute versions.'
'You are using an invalid version range. Please do not use absolute versions. Invalid devDependencies includes: npm-package-json-lint'
);
});
});
Expand All @@ -39,7 +39,7 @@ describe('no-absolute-version-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use absolute versions.'
'You are using an invalid version range. Please do not use absolute versions. Invalid devDependencies includes: npm-package-json-lint'
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('prefer-absolute-version-dependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('prefer-absolute-version-dependencies');
expect(response.severity).toStrictEqual(severity);
expect(response.node).toStrictEqual(nodeName);
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please use absolute versions.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please use absolute versions. Invalid dependencies include: npm-package-json-lint'
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('prefer-absolute-version-devDependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('prefer-absolute-version-devDependencies');
expect(response.severity).toStrictEqual(severity);
expect(response.node).toStrictEqual(nodeName);
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please use absolute versions.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please use absolute versions. Invalid devDependencies include: npm-package-json-lint'
);
});
});

Expand Down
Loading