Skip to content

Commit

Permalink
Improve output for prefer-no-version-zero-* rules (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclindner authored Mar 20, 2022
1 parent 0dd34e3 commit 81aa1d0
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 66 deletions.
16 changes: 12 additions & 4 deletions src/rules/prefer-no-version-zero-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import {PackageJson} from 'type-fest';
import {hasDepVersZero} from '../validators/dependency-audit';
import {auditDependenciesWithMajorVersionOfZero} 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-no-version-zero-dependencies';
const nodeName = 'dependencies';
const message = 'You have invalid version 0 dependencies. Please use modules with a major version >= 1.';

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) && hasDepVersZero(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesWithMajorVersionOfZero(packageJsonData, nodeName, config);

if (exists(packageJsonData, nodeName) && auditResult.hasDependencyWithMajorVersionOfZero) {
return new LintIssue(
lintId,
severity,
nodeName,
`You have invalid version 0 dependencies. Please use modules with a major version >= 1. Invalid ${nodeName} include: ${auditResult.dependenciesWithMajorVersionOfZero.join(
', '
)}`
);
}

return null;
Expand Down
16 changes: 12 additions & 4 deletions src/rules/prefer-no-version-zero-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import {PackageJson} from 'type-fest';
import {hasDepVersZero} from '../validators/dependency-audit';
import {auditDependenciesWithMajorVersionOfZero} 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-no-version-zero-devDependencies';
const nodeName = 'devDependencies';
const message = 'You have invalid version 0 dependencies. Please use modules with a major version >= 1.';

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) && hasDepVersZero(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesWithMajorVersionOfZero(packageJsonData, nodeName, config);

if (exists(packageJsonData, nodeName) && auditResult.hasDependencyWithMajorVersionOfZero) {
return new LintIssue(
lintId,
severity,
nodeName,
`You have invalid version 0 dependencies. Please use modules with a major version >= 1. Invalid ${nodeName} include: ${auditResult.dependenciesWithMajorVersionOfZero.join(
', '
)}`
);
}

return null;
Expand Down
38 changes: 30 additions & 8 deletions src/validators/dependency-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,30 @@ export const hasDepPrereleaseVers = (
return false;
};

export interface AuditDependenciesWithMajorVersionOfZeroResponse {
hasDependencyWithMajorVersionOfZero: boolean;
dependenciesWithMajorVersionOfZero: string[];
dependenciesWithoutMajorVersionOfZero: string[];
}

/**
* Determines whether or not the package has a dependency with a major version of 0
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
* @return {boolean} True if the package has a dependency with version 0. False if it does 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 True if the package has a dependency with version 0. False if it does not or the node is missing.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const hasDepVersZero = (packageJsonData: PackageJson | any, nodeName: string, config: any): boolean => {
export const auditDependenciesWithMajorVersionOfZero = (
// 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
): AuditDependenciesWithMajorVersionOfZeroResponse => {
let hasDependencyWithMajorVersionOfZero = false;
const dependenciesWithMajorVersionOfZero = [];
const dependenciesWithoutMajorVersionOfZero = [];

// eslint-disable-next-line no-restricted-syntax
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
Expand All @@ -98,12 +113,19 @@ export const hasDepVersZero = (packageJsonData: PackageJson | any, nodeName: str

// if first char is 0 then major version is 0
if (dependencyMjrVersion === '0') {
return true;
hasDependencyWithMajorVersionOfZero = true;
dependenciesWithMajorVersionOfZero.push(dependencyName);
} else {
dependenciesWithoutMajorVersionOfZero.push(dependencyName);
}
}
}

return false;
return {
hasDependencyWithMajorVersionOfZero,
dependenciesWithMajorVersionOfZero,
dependenciesWithoutMajorVersionOfZero,
};
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('prefer-no-version-zero-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual(nodeName);
expect(response.lintMessage).toStrictEqual(
'You have invalid version 0 dependencies. Please use modules with a major version >= 1.'
'You have invalid version 0 dependencies. Please use modules with a major version >= 1. Invalid dependencies include: npm-package-json-lint'
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('prefer-no-version-zero-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You have invalid version 0 dependencies. Please use modules with a major version >= 1.'
'You have invalid version 0 dependencies. Please use modules with a major version >= 1. Invalid devDependencies include: npm-package-json-lint'
);
});
});
Expand Down
Loading

0 comments on commit 81aa1d0

Please sign in to comment.