Skip to content

Commit

Permalink
Improve output for prefer*-version-* rules (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclindner authored Mar 20, 2022
1 parent c833dfb commit 0dd34e3
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/rules/no-absolute-version-devDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const lint = (packageJsonData: PackageJson | any, severity: Severity, con
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use absolute versions. Invalid ${nodeName} includes: ${auditResult.dependenciesWithAbsoluteVersion.join(
`You are using an invalid version range. Please do not use absolute versions. Invalid ${nodeName} include: ${auditResult.dependenciesWithAbsoluteVersion.join(
', '
)}`
);
Expand Down
15 changes: 11 additions & 4 deletions src/rules/prefer-caret-version-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import {PackageJson} from 'type-fest';
import {areVersRangesValid} from '../validators/dependency-audit';
import {auditDependenciesForValidRangeVersions} 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-caret-version-dependencies';
const nodeName = 'dependencies';
const message = 'You are using an invalid version range. Please use ^.';

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 => {
const rangeSpecifier = '^';
const auditResult = auditDependenciesForValidRangeVersions(packageJsonData, nodeName, rangeSpecifier, config);

if (exists(packageJsonData, nodeName) && !areVersRangesValid(packageJsonData, nodeName, rangeSpecifier, config)) {
return new LintIssue(lintId, severity, nodeName, message);
if (exists(packageJsonData, nodeName) && !auditResult.onlyValidVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please use ^. Invalid ${nodeName} include: ${auditResult.dependenciesWithoutValidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
15 changes: 11 additions & 4 deletions src/rules/prefer-caret-version-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import {PackageJson} from 'type-fest';
import {areVersRangesValid} from '../validators/dependency-audit';
import {auditDependenciesForValidRangeVersions} 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-caret-version-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using an invalid version range. Please use ^.';

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 => {
const rangeSpecifier = '^';
const auditResult = auditDependenciesForValidRangeVersions(packageJsonData, nodeName, rangeSpecifier, config);

if (exists(packageJsonData, nodeName) && !areVersRangesValid(packageJsonData, nodeName, rangeSpecifier, config)) {
return new LintIssue(lintId, severity, nodeName, message);
if (exists(packageJsonData, nodeName) && !auditResult.onlyValidVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please use ^. Invalid ${nodeName} include: ${auditResult.dependenciesWithoutValidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
15 changes: 11 additions & 4 deletions src/rules/prefer-tilde-version-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import {PackageJson} from 'type-fest';
import {areVersRangesValid} from '../validators/dependency-audit';
import {auditDependenciesForValidRangeVersions} 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-tilde-version-dependencies';
const nodeName = 'dependencies';
const message = 'You are using an invalid version range. Please use ~.';

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 => {
const rangeSpecifier = '~';
const auditResult = auditDependenciesForValidRangeVersions(packageJsonData, nodeName, rangeSpecifier, config);

if (exists(packageJsonData, nodeName) && !areVersRangesValid(packageJsonData, nodeName, rangeSpecifier, config)) {
return new LintIssue(lintId, severity, nodeName, message);
if (exists(packageJsonData, nodeName) && !auditResult.onlyValidVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please use ~. Invalid ${nodeName} include: ${auditResult.dependenciesWithoutValidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
15 changes: 11 additions & 4 deletions src/rules/prefer-tilde-version-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import {PackageJson} from 'type-fest';
import {areVersRangesValid} from '../validators/dependency-audit';
import {auditDependenciesForValidRangeVersions} 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-tilde-version-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using an invalid version range. Please use ~.';

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 => {
const rangeSpecifier = '~';
const auditResult = auditDependenciesForValidRangeVersions(packageJsonData, nodeName, rangeSpecifier, config);

if (exists(packageJsonData, nodeName) && !areVersRangesValid(packageJsonData, nodeName, rangeSpecifier, config)) {
return new LintIssue(lintId, severity, nodeName, message);
if (exists(packageJsonData, nodeName) && !auditResult.onlyValidVersionsDetected) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please use ~. Invalid ${nodeName} include: ${auditResult.dependenciesWithoutValidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
49 changes: 34 additions & 15 deletions src/validators/dependency-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,46 @@ export const hasDepVersZero = (packageJsonData: PackageJson | any, nodeName: str
* @param {String} rangeSpecifier A version range specifier
* @return {Boolean} True if the version starts with the range, false if it doesn't.
*/
export const doesVersStartsWithRange = (dependencyVersion: string, rangeSpecifier: string): boolean => {
export const doesVersionStartWithRange = (dependencyVersion: string, rangeSpecifier: string): boolean => {
const firstCharOfStr = 0;

return dependencyVersion.startsWith(rangeSpecifier, firstCharOfStr);
};

export interface AuditDependenciesForValidRangeResponse {
onlyValidVersionsDetected: boolean;
dependenciesWithValidVersionRange: string[];
dependenciesWithoutValidVersionRange: string[];
}

/**
* Determines whether or not all dependency version ranges match expected range
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {string} rangeSpecifier A version range specifier
* @param {object} config Rule configuration
* @return {boolean} False if the package has an invalid range. 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 rangeSpecifier A version range specifier
* @param config Rule configuration
* @return False if the package has an invalid range. True if it is not or the node is missing.
*/
export const areVersRangesValid = (
export const auditDependenciesForValidRangeVersions = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJsonData: PackageJson | any,
nodeName: string,
rangeSpecifier: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
): boolean => {
): AuditDependenciesForValidRangeResponse => {
let onlyValidVersionsDetected = true;
const dependenciesWithValidVersionRange = [];
const dependenciesWithoutValidVersionRange = [];

if (!packageJsonData.hasOwnProperty(nodeName)) {
return true;
return {
onlyValidVersionsDetected,
dependenciesWithValidVersionRange: [],
dependenciesWithoutValidVersionRange: [],
};
}

let rangesValid = true;

// eslint-disable-next-line no-restricted-syntax
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
Expand All @@ -149,12 +161,19 @@ export const areVersRangesValid = (

const dependencyVersion = packageJsonData[nodeName][dependencyName];

if (!doesVersStartsWithRange(dependencyVersion, rangeSpecifier)) {
rangesValid = false;
if (doesVersionStartWithRange(dependencyVersion, rangeSpecifier)) {
dependenciesWithValidVersionRange.push(dependencyName);
} else {
onlyValidVersionsDetected = false;
dependenciesWithoutValidVersionRange.push(dependencyName);
}
}

return rangesValid;
return {
onlyValidVersionsDetected,
dependenciesWithValidVersionRange,
dependenciesWithoutValidVersionRange,
};
};

export interface AuditDependenciesForInvalidRangeResponse {
Expand Down Expand Up @@ -200,7 +219,7 @@ export const auditDependenciesForInvalidRange = (

const dependencyVersion = packageJsonData[nodeName][dependencyName];

if (doesVersStartsWithRange(dependencyVersion, rangeSpecifier)) {
if (doesVersionStartWithRange(dependencyVersion, rangeSpecifier)) {
hasInvalidRangeVersions = true;
dependenciesWithInvalidVersionRange.push(dependencyName);
} else {
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. Invalid devDependencies includes: npm-package-json-lint'
'You are using an invalid version range. Please do not use absolute versions. Invalid devDependencies include: 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. Invalid devDependencies includes: npm-package-json-lint'
'You are using an invalid version range. Please do not use absolute versions. Invalid devDependencies include: npm-package-json-lint'
);
});
});
Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/prefer-caret-version-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('prefer-caret-version-dependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('prefer-caret-version-dependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please use ^.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please use ^. Invalid dependencies include: npm-package-json-lint'
);
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/prefer-caret-version-devDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('prefer-caret-version-devDependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('prefer-caret-version-devDependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please use ^.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please use ^. Invalid devDependencies include: npm-package-json-lint'
);
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/prefer-tilde-version-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('prefer-tilde-version-dependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('prefer-tilde-version-dependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please use ~.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please use ~. Invalid dependencies include: npm-package-json-lint'
);
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/prefer-tilde-version-devDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('prefer-tilde-version-devDependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('prefer-tilde-version-devDependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please use ~.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please use ~. Invalid devDependencies include: npm-package-json-lint'
);
});
});

Expand Down
Loading

0 comments on commit 0dd34e3

Please sign in to comment.